在本文中說明了pom依賴能夠支持什麼功能,以及支持什麼註解,引入該依賴能夠在application.properties中添加什麼配置。java
一、SpringCloud 的pom依賴git
序號 | pom依賴 | 說明 | 支持註解 | 支持配置application.properties |
1 | <parent> |
spring-boot-starter-parent是Spring Boot的核心啓動器, |
@SpringBootApplication |
server.port=1111 |
2 | <dependencyManagement> |
使用dependencyManagement進行版本管理 |
||
3 | <dependency> |
支持HTTP調用方式,包含了Spring Boot預約義的一些Web開發的經常使用依賴包 |
||
4 | <dependency> |
@SpringCloudApplication | spring.application.name=eureka-service | |
5 | <dependency> |
eureka註冊中心依賴 | @EnableEurekaServer | eureka.instance.hostname=localhost |
6 | <dependency> |
引入eureka 客戶端依賴 | @EnableDiscoveryClient |
|
7 | <dependency> |
引入ribbon 依賴 ,用來實現客戶端的負載均衡,用在client客戶端項目 | ribbon.ConnectTimeout=500 |
|
8 | <dependency> |
引入hystrix 依賴 ,用來實現服務容錯保護。當發現請求的服務端崩潰,就採用容錯機制 | @EnableCircuitBreaker | hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=5000 |
9 | <dependency> |
引入feign 依賴,包含ribbon負載均衡,也包含Hystrix服務容錯。 Spring Cloud Feign在構建被@FeignClient註解修飾的服務客戶端是,會爲每個客戶端都建立一個feign.Logger實例,咱們能夠利用該日誌對象進行Log分析。 |
@EnableFeignClients | feign.compression.request.enabled=true; |
10 | <dependency> |
監控模塊,實時和定時監控服務的各項狀態和可用性 | ||
11 | <dependency> |
引入zuul依賴 , 它依賴了spring-boot-starter-actuator/spring-boot-starter-hystrix/spring-boot-starter-ribbon | @EnableZuulProxy | zuul.routes.api-a.path=/api-a/** |
12 | <dependency> |
爲分佈式系統中的基礎設施和微服務提供集中化的外部配置支持,分爲服務端和客戶端兩個部分。 |
@EnableConfigServer | #配置Git倉庫的地址 |
13 | <dependency> |
分佈式服務中管理配置文件的客戶端,服務端是spring-cloud-config-server | @RefreshScope | bootstrap.properties: |
14 | <dependency> |
在全部須要鏈路跟蹤的項目中都加上這個依賴。 | @EnableZipkinServer | 除了sleuth自己是鏈路中心的除外,其他參與鏈路追蹤的分佈式系統都須要添加以下配置: |
15 | <dependency> |
修改源文件後系統自動重啓 | ||
16 | <plugin> |
告訴Maven包含Spring特定的Maven插件,用於構建和部署SpringBoot應用程序。 |
|
|
17 | <plugin> |
部署以及持續集成。 |
||
18 | <dependency> |
使用Java Persistence API ( JPA ) | @Entity // 這是一個JPA類 |
|
19 | <dependency> |
Postgres JDBC驅動程序 | ||
20 | <dependency> |
加密解密相關包 |
spring.datasource.password: "{cipher}d495ce8603af9676450736e119" |
二、SpringCloud相關注解
序號 | 註解 | 說明 |
1 | @SpringBootApplication | SpringBoot啓動類註解,啓動類需有main方法 |
2 | @EnableEurekaServer | 用來指定該項目爲Eureka的服務註冊中心 |
3 | @EnableCircuitBreaker | 開啓斷路器,實現服務容錯保護 |
4 | @EnableDiscoveryClient | 讓服務使用eureka服務器 實現服務註冊和發現 |
5 | @SpringCloudApplication | 至關於3個註解:@EnableDiscoveryClient |
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 | 開啓網關路由服務功能。 |
12 | @Bean | 向spring容器註冊自定義類 |
13 | @EnableConfigServer | 開啓Spring Cloud Config 的服務端功能。爲分佈式系統中的基礎設施和微服務提供集中化的外部配置支持,分爲服務端和客戶端兩個部分。 |
14 | @EnableZipkinServer | 用於開啓Zipkin Server功能:分佈式框架中的若是發生異常可鏈路追蹤. |
15 | @RequestMapping(value="/url",method=RequestMethod.POST) |
如何動態配置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; |
請求壓縮配置,支持對請求和響應進行GZIP壓縮,以減小通訊過程當中的性能損耗。 |
13 | zuul.routes.api-a.path=/api-a/** |
請求示例:http://localhost:5555/api-a/feign-consumer |
14 | #配置Git倉庫的地址 |
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} |