在本文中說明了pom依賴可以支持什么功能,以及支持什么注解,引入該依賴可以在application.properties中添加什么配置。
1、SpringCloud 的pom依賴
序號 | 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" |
2、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注解進行標記 |
3、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} |