SpringCloud註解和配置以及pom依賴說明

在本文中說明了pom依賴能夠支持什麼功能,以及支持什麼註解,引入該依賴能夠在application.properties中添加什麼配置。java

一、SpringCloud 的pom依賴git

序號 pom依賴 說明 支持註解 支持配置application.properties
1

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.16.RELEASE</version>
<relativePath/> 
</parent>web

spring-boot-starter-parent是Spring Boot的核心啓動器,
包含了自動配置、日誌和YAML等大量默認的配置。
引入以後相關的starter引入就不須要添加version配置,
spring boot會自動選擇最合適的版本進行添加。spring

@SpringBootApplication
@Configuration
@RequestBody
@RestController
@ComponentScan(basePackages={"com.xx","com.yy"})sql

server.port=1111
2

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Edgware.SR4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>docker

使用dependencyManagement進行版本管理
注意:這裏的高版本Edgware.SR4不兼容spring-boot-starter-parent的高版本2.x,只能是1.x的高版本,好比1.5.16.RELEASEbootstrap

   
3

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>api

支持HTTP調用方式,包含了Spring Boot預約義的一些Web開發的經常使用依賴包
如: spring-webmvc,Tomcat....服務器

   
4

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>mvc

   @SpringCloudApplication spring.application.name=eureka-service
5

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>

eureka註冊中心依賴  @EnableEurekaServer

eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:1111/eureka
eureka.client.fetch-registry=false
eureka.client.fetch-registry=false

6

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>

引入eureka 客戶端依賴

@EnableDiscoveryClient
@EnableZuulProxy

 
7

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>

引入ribbon 依賴 ,用來實現客戶端的負載均衡,用在client客戶端項目  

ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000
hello-service.ribbon.ConnectTimeout=500

8

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

引入hystrix 依賴 ,用來實現服務容錯保護。當發現請求的服務端崩潰,就採用容錯機制  @EnableCircuitBreaker hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000
9

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

引入feign 依賴,包含ribbon負載均衡,也包含Hystrix服務容錯。

Spring Cloud Feign在構建被@FeignClient註解修飾的服務客戶端是,會爲每個客戶端都建立一個feign.Logger實例,咱們能夠利用該日誌對象進行Log分析。

 @EnableFeignClients

feign.compression.request.enabled=true;
feigan.compression.response.enabled=true;

10

<dependency> 
<groupId>org.springframework.boot</groupId> 
<artifactId>spring-boot-starter-actuator</artifactId> 
</dependency>

監控模塊,實時和定時監控服務的各項狀態和可用性    
11

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>

引入zuul依賴 , 它依賴了spring-boot-starter-actuator/spring-boot-starter-hystrix/spring-boot-starter-ribbon  @EnableZuulProxy

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.url=http://localhost:9001

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=hello-service

12

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

 

爲分佈式系統中的基礎設施和微服務提供集中化的外部配置支持,分爲服務端和客戶端兩個部分。
好比要訪問my_test分支下的spring-cloud-config-file目錄下的sam-dev.properties配置文件,訪問url:
http://localhost:7001/sam/dev/my_test

 @EnableConfigServer  

#配置Git倉庫的地址
spring.cloud.config.server.git.uri=https://gitlab.com/xxx/xxx.git
#配置倉庫路徑下的相對搜索位置,能夠配置多個
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#這裏配置你的Git倉庫的用戶名
spring.cloud.config.server.git.username=xxx
#這裏配置你的Git倉庫的密碼
spring.cloud.config.server.git.password=123

13

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>

 分佈式服務中管理配置文件的客戶端,服務端是spring-cloud-config-server  @RefreshScope

bootstrap.properties:
#{profile}
spring.cloud.config.profile=dev
#{label} git分支名字
spring.cloud.config.label=master
#config server uri
spring.cloud.config.uri=http://localhost:7001

14  

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>

 在全部須要鏈路跟蹤的項目中都加上這個依賴。   @EnableZipkinServer  

除了sleuth自己是鏈路中心的除外,其他參與鏈路追蹤的分佈式系統都須要添加以下配置:
#指定zipkin服務端的url
spring.zipkin.base-url=http://localhost:9411
#設定樣本收集的比率爲100%
spring.sleuth.sampler.percentage=1.0

15

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.RELEASE</version>
<scope>provided</scope>
<optional>true</optional>
</dependency>

修改源文件後系統自動重啓    
16

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

 

告訴Maven包含Spring特定的Maven插件,用於構建和部署SpringBoot應用程序。
注意,這個要放在build->plugins節點下。

   

17

<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.10</version>
<configuration>
<baseImage>java</baseImage>
<imageName>example</imageName>
</configuration>
</plugin>

 

部署以及持續集成。
注意,這個要放在build->plugins節點下。

   
18

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

使用Java Persistence API ( JPA )

@Entity // 這是一個JPA類
@Table(name="t_user") //映射到哪張表
@Id //主鍵
@Column(name="user_id",nullable=false) //對應表裏哪一個字段,不容許爲空
@Repository //這是一個DAO類

 
19

<dependency>
  <groupId>postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <version>9.1-901.jdbc4</version>
</dependency>

Postgres JDBC驅動程序    
 20

<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-rsa</artifactId>
</dependency>

 加密解密相關包  

 

spring.datasource.password: "{cipher}d495ce8603af9676450736e119"
spring.cloud.config.server.encrypt.enabled=false

 

二、SpringCloud相關注解

序號 註解 說明
1  @SpringBootApplication SpringBoot啓動類註解,啓動類需有main方法
2  @EnableEurekaServer 用來指定該項目爲Eureka的服務註冊中心
3  @EnableCircuitBreaker 開啓斷路器,實現服務容錯保護
4  @EnableDiscoveryClient 讓服務使用eureka服務器 實現服務註冊和發現
5  @SpringCloudApplication

至關於3個註解:@EnableDiscoveryClient
@SpringBootApplication
@EnableCircuitBreaker

6  @Configuration 至關於定義spring配置文件的xmlns域,以及支持@Bean在本類中的註冊。
7  @EnableFeignClients Spring Cloud Feign 經過@EnableFeignClients來開啓spring cloud feign的支持功能 不只包含Spring Cloud ribbon負責均衡功能,也包含Spring Cloud Hystrix服務容錯功能,還提供了一種聲明式的Web服務客戶端定義方式。
8  @RequestBody 使接收到的字段值自動映射到註解聲明的複合對象中
9  @RestController  @RestController = @Controller + @ResponseBody
10  @ComponentScan(basePackages={"com.xx","com.yy"}) 指定掃描包
11  @EnableZuulProxy

開啓網關路由服務功能。
注意:一旦要請求的controller類實現了某個接口,剛好這個接口有自定義的@RequestMapping("/xxx")值,那麼在發起請求時,完整的url不能省略這個@RequestMapping值:http://localhost:5555/hello-service/xxx/myHello

12  @Bean 向spring容器註冊自定義類
13  @EnableConfigServer 開啓Spring Cloud Config 的服務端功能。爲分佈式系統中的基礎設施和微服務提供集中化的外部配置支持,分爲服務端和客戶端兩個部分。
14  @EnableZipkinServer 用於開啓Zipkin Server功能:分佈式框架中的若是發生異常可鏈路追蹤.
15  

@RequestMapping(value="/url",method=RequestMethod.POST)
public void getUrl(@PathVariable("name") String name){ … }

如何動態配置url路徑,以及從路徑中取值
16  @RefreshScope 對須要刷新的屬性使用@Value註解,同時將類使用@RefreshScope註解進行標記

 

三、SpringCloud的application.properties相關設置

序號 application.properties配置 說明
1 server.port=1111 設置web項目服務端口號
2 spring.application.name=eureka-service 設置服務名稱
3 eureka.instance.hostname=localhost 設置服務主機IP
4 eureka.client.register-with-eureka=false false: 註冊中心不須要註冊本身。true(默認): 須要註冊本身
5 eureka.client.fetch-registry=false false: 註冊中心不須要去發現服務。true(默認): 須要發現服務
6 eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka 設置服務註冊中心的URL,這裏的${}是佔位符。最終顯示例如:http://localhost:1111/eureka
7 ribbon.ConnectTimeout=500 全局配置負載均衡超時設置 ribbon.<key>=<value>
8 ribbon.ReadTimeout=5000 全局配置負載均衡讀超時設置
9 hello-service.ribbon.ConnectTimeout=500 爲某服務指定的負載均衡超時設置 @FeignClient(value="hello-service")
10 hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 hystrix.command.default.xxx進行全局配置
11 hystrix.command.hello.execution.isolation.thread.timeoutInMilliseconds=5000 hystrix.command.<commandKey>.xxx進行指定配置,這裏的<commandKey>能夠爲方法名
12

feign.compression.request.enabled=true;
feigan.compression.response.enabled=true;

請求壓縮配置,支持對請求和響應進行GZIP壓縮,以減小通訊過程當中的性能損耗。
13

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.url=http://localhost:9001

zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=hello-service

zuul.routes.hello-service.path=/hello-service/**
zuul.routes.hello-service.serviceId=hello-service

請求示例:http://localhost:5555/api-a/feign-consumer
請到http://localhost:9001這個url地址找**(/feign-consumer)匹配的http接口,由於我把這個url的服務命名爲api-a了。
推薦使用serviceId來代替url地址。
注意:zuul.routes.api-a.url=hello-service也能實現功能,可是它不能進行正常的負載均衡和容錯保護。
不配置默認路由規則。當zuul.ignored-services=*的時候,全部的服務都不會自動建立路由規則,這個時候須要經過前面的配置進行相關路由配置了。

14

#配置Git倉庫的地址
spring.cloud.config.server.git.uri=https://gitlab.com/xxx/xxx.git
#配置倉庫路徑下的相對搜索位置,能夠配置多個
spring.cloud.config.server.git.search-paths=spring-cloud-config-file
#這裏配置你的Git倉庫的用戶名
spring.cloud.config.server.git.username=xxx
#這裏配置你的Git倉庫的密碼
spring.cloud.config.server.git.password=123

 SpringCloud本身建立的管理配置中心的服務端配置
15

spring.profiles.active=default

指定服務運行什麼配置,好比application-dev.properties,就設置值爲dev
16

spring.cloud.config.server.encrypt.enabled=false

顯式關閉輸出屬性的解密。
17

spring.datasource.password: "{cipher}d495ce8603af9676450736e119"

SpringCloud配置服務器要求全部已加密的屬性前面加上{cipher}
相關文章
相關標籤/搜索