Spring Cloud入門看這一篇就夠了
- 2022 年 6 月 11 日
- 筆記
- Spring Boot, Spring Cloud, 編程基礎
SpringCloud微服務
架構演進
- 單體架構
- 垂直拆分
- 分佈式SOA面向服務架構
- 微服務架構
服務調用方式:
- RPC,早期的webservice,現在熱門的dubbo,都是RPC的典型代表
- HTTP,HttpClient,OKHttp,URLConnection,RestTemplate
Euraka服務註冊中心
註冊中心
- 添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
- 使用@EnableEurekaServer註解開啟註冊服務
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 編寫配置文件application.yml
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: //${eureka.instance.hostname}:${server.port}/eureka/
#是否將自己註冊到Eureka服務中,默認為true(由於當前就是eureka服務,固設置為false)
registerWithEureka: false
#設置是否從eureka服務上獲取註冊信息,默認為true(由於當前就是eureka服務,固設置為false)
fetchRegistry: false
server:
# 是否開啟自我保護模式(自我保護模式,當eureka在一定時間內沒有接受到某個微服務實例的心跳包,默認90S會註銷該實例),
# 一旦進入自我保護模式,若短時間內丟失大量客戶端,eureka也會保護註冊表的信息,不再註銷
enable-self-preservation: false
# 清理間隔。默認為60000ms
eviction-interval-timer-in-ms: 5000
服務提供者(服務註冊)
- 添加依賴
<!-- Eureka客戶端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency
2.添加 @EnableDiscoveryClient 來開啟Eureka客戶端功能
@SpringBootApplication
@EnableDiscoveryClient
public class SpringDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDemoApplication.class, args);
}
}
3.編寫配置文件
eureka:
instance:
#在調用服務的時候就已經變成ip地址;需要注意的是:不是在eureka中的控制台服務實例狀態顯示。
ip-address: 127.0.0.1
prefer-ip-address: true #使用ip註冊
client:
# 是否開啟健康檢查
healthcheck:
enabled: true
# 集群url
service-url:
defaultZone: //127.0.0.1:8761/eureka
服務消費者(服務發現)
消費應用從註冊中心獲取服務列表,從而得知每個服務方的信息,知道去哪裡調用服務方
服務續約
在註冊服務完成以後,服務提供者會維持一個心跳(定時向EurekaServer發起Rest請求),有兩個重要參數可以修改服務續約的行為
eureka:
instance:
#服務失效時間,默認值90秒
lease-expiration-duration-in-seconds: 90
#服務續約(renew)的間隔,默認為30秒
lease-renewal-interval-in-seconds: 30
也就是說,默認情況下每隔30秒服務會向註冊中心發送一次心跳,證明自己還活着。如果超過90秒沒有發送心跳,EurekaServer就會認為該服務宕機,會定時(eureka.server.eviction-interval-timer-in-ms設定的時間)從服務列表中剔除
失效剔除和自我保護
服務註冊中心在啟動時會創建一個定時任務,默認每隔一段時間(默認為60秒)將當前清單中超時(默認為90秒)沒有續約的服務剔除。
Eureka Server在運行期間,會統計心跳失敗的比例在15分鐘之內是否低於85%,如果出現低於的情況,Eureka Server會將當前的實例註冊信息保護起來,不再刪除服務註冊表中的數據(也就是不會註銷任何微服務)
Consul
特性
- 高效的Raft一致性算法
- 支持多數據中心
- 支持健康檢查
- HTTP和DNS支持
Consul和Eureka的區別:
-
Consul強一致性(CP),Eureka保證高可用和最終一致性(AP)
-
Consul使用Go語言,Eureka使用Java語言
-
Consul 不同於 Eureka 需要單獨安裝,官網://www.consul.io/downloads
Consul架構圖

Consul基本使用
服務提供者
- pom.xml添加依賴
<!--SpringCloud提供的基於Consul的服務發現-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!--actuator用於心跳檢查-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 通過@EnableDiscoveryClient註解開啟對服務發現的支持
- 編寫配置文件application.yml
spring:
application:
name: consul-provider
####consul註冊中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name} #註冊中心名字與spring的應用名稱一致
#-----------------------------相關配置----------------------------|
#是否註冊
register: true
#實例ID
instance-id: ${spring.application.name}-1
#服務實例名稱
service-name: ${spring.application.name}
#服務實例端口
port: ${server.port}
#健康檢查路徑
healthCheckPath: /actuator/health
#健康檢查時間間隔
healthCheckInterval: 15s
#開啟ip地址註冊
prefer-ip-address: true
#實例的請求ip
ip-address: ${spring.cloud.client.ip-address}
服務消費者
- pom.xml添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
- 編寫配置文件application.yml
spring:
application:
name: consul-provider
####consul註冊中心地址
cloud:
consul:
host: localhost
port: 8500
discovery:
#設置不需要註冊到Consul中
register: false
負載均衡Ribbon
Ribbon是 Netflix 發佈的一個負載均衡器,有助於控制 HTTP 和 TCP客戶端行為。Ribbon自動的從註冊中心中獲取服務提供者的
列表信息,並基於內置的負載均衡算法,請求服務。
搭建過程
- 啟動兩個服務實例
先編寫請求的接口
@Value("${spring.cloud.client.ip-address}")
private String ip;
@Value("${server.port}")
private String port;
@ResponseBody
@RequestMapping(value = "backInfo", method = RequestMethod.GET)
public String backInfo() {
return "調用的是" + ip + "端口是:" + port;
}
我們假設搭建兩台服務提供者,端口分別為:8090和8091,將已有服務的配置更改為
server:
#的${}表示在jvm啟動時候若能找到對應port則使用,若無則使用後面的默認值
port: ${port:8090}
另外一台在啟動的時候可以指定端口port,在如下界面中的 VM options 中設置 -Dport=10087

- 啟動負載均衡
因為Eureka中已經集成了Ribbon,所以我們無需引入新的依賴。直接修改消費者的啟動類
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
- 調取服務提供者接口,頁面進行測試,可以分別請求到8090和8091兩個端口
@Autowired
private RestTemplate restTemplate;
@ResponseBody
@RequestMapping(value = "getIpAndPort", method = RequestMethod.GET)
public String printInfo() {
String url = "//springbootdemo/backInfo";
return restTemplate.getForObject(url, String.class);
}
負載均衡策略
Ribbon默認的負載均衡策略是輪詢,Ribbon內置了多種負載均衡策略
修改負載均衡規則的配置:
springbootdemo:
ribbon:
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule
#格式是: {服務名稱}.ribbon.NFLoadBalancerRuleClassName
- com.netflix.loadbalancer.RoundRobinRule :以輪詢的方式進行負載均衡。
- com.netflix.loadbalancer.RandomRule :隨機策略
- com.netflix.loadbalancer.RetryRule :重試策略
- com.netflix.loadbalancer.WeightedResponseTimeRule :權重策略
- com.netflix.loadbalancer.BestAvailableRule :最佳策略
- com.netflix.loadbalancer.AvailabilityFilteringRule :可用過濾策略
策略選擇:
-
如果每個機器配置一樣,則建議不修改策略 (推薦)
-
如果部分機器配置強,則可以改為 WeightedResponseTimeRule
源碼跟蹤
RibbonAutoConfifiguration->LoadBalancerAutoConfiguration->LoadBalancerInterceptor
服務調用Feign
Feign是一種聲明式、模板化的HTTP客戶端。
基於Feign的服務調用
1.在消費者引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.啟動類開啟feign支持
@SpringBootApplication
@EnableDiscoveryClient
@ComponentScan(basePackages = "com.jyd0124.consume_server.*")
@EnableFeignClients
public class ConsumeServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeServerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate returnBean() {
return new RestTemplate();
}
}
3.創建接口和定義需要遠程調用的方法
@FeignClient("springbootdemo")
public interface SpringBootFeign {
@RequestMapping(value = "/person", method = RequestMethod.GET)
public Map<String, Object> getPerson(@RequestParam(value = "id") int id);
}
4.調用服務。
@Autowired
private SpringBootFeign springBootFeign;
@ResponseBody
@RequestMapping(value = "/person/{id}", method = RequestMethod.GET)
public Map<String,Object> printInfo(@PathVariable("id") int id) {
return springBootFeign.getPerson(id);
}
5.訪問,測試結果

負載均衡
Feign中本身已經集成了Ribbon依賴和自動配置,因此我們不需要額外引入依賴,也不需要再註冊RestTemplate 對象,可以通過 ribbon.xx 來進行全局配置。也可以通過 服務名.ribbon.xx 來對指定服務配置。
Feign的配置
feign:
client:
config:
feignName: ##定義FeginClient的名稱
connectTimeout: 5000 # 建立鏈接的超時時長
readTimeout: 5000 # 讀取超時時長
# 配置Feign的日誌級別,相當於代碼配置方式中的Logger
loggerLevel: full
# Feign的錯誤解碼器,相當於代碼配置方式中的ErrorDecoder
errorDecoder: com.example.SimpleErrorDecoder
# 配置重試,相當於代碼配置方式中的Retryer
retryer: com.example.SimpleRetryer
# 配置攔截器,相當於代碼配置方式中的
RequestInterceptor requestInterceptors:
- com.example.FooRequestInterceptor
- com.example.BarRequestInterceptor
#配置熔斷不處理404異常
decode404: false
請求壓縮: 支持對請求和響應進行GZIP壓縮,以減少通信過程中的性能損耗
#請求壓縮
feign:
compression:
request:
enabled: true # 開啟請求壓縮
response:
enabled: true # 開啟響應壓縮
Hystrix支持
Feign默認也有對Hystrix的集成
feign:
hystrix:
enabled: true # 開啟Feign的熔斷功能
日誌級別
默認情況下Feign的日誌是沒有開啟的。
feign:
client:
config:
feignName:
loggerLevel: full
#NONE【性能最佳,適用於生產】:不記錄任何日誌(默認值)
#BASIC【適用於生產環境追蹤問題】:僅記錄請求方法、URL、響應狀態代碼以及執行時間
#HEADERS:記錄BASIC級別的基礎上,記錄請求和響應的header。
#FULL【比較適用於開發及測試環境定位問題】:記錄請求和響應的header、body和元數據
熔斷器Hystrix
雪崩效應
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證其高可用,單個服務通常會集群部署。由於網絡原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的「雪崩」效應。
Hystrix的特性
- 服務熔斷
Hystrix的熔斷狀態機模型:

hystrix:
command:
default:
execution.isolation.thread.timeoutInMilliseconds: 2000
circuitBreaker:
errorThresholdPercentage: 50 # 觸發熔斷錯誤比例閾值,默認值50%
sleepWindowInMilliseconds: 10000 # 熔斷後休眠時長,默認值5秒
requestVolumeThreshold: 10 # 觸發熔斷的最小請求次數,默認20
#配置項可以參考 HystrixCommandProperties 類
-
服務降級
通過HystrixdeCommand的fallback實現服務降級。
-
服務隔離
-線程池隔離策略
– 信號量隔離策略
Ribbon中使用斷路器
- 在pom.xml中添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
- 在啟動類上添加註解開啟Hystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
public class ConsumeServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeServerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate returnBean() {
return new RestTemplate();
}
}
- 編寫熔斷降級邏輯。在方法上加上@HystrixCommand註解,並指定了fallbackMethod熔斷方法
@ResponseBody
@RequestMapping(value = "/info", method = RequestMethod.GET)
@HystrixCommand(fallbackMethod = "backErrorInfo")
public String printInfo() {
String url = "//springbootdemo/backInfo";
return restTemplate.getForObject(url, String.class);
}
public String backErrorInfo(){
return "sorry,error";
}
- 測試,如果springbootdemo服務關閉,會發現頁面返回了降級處理信息

Feign中使用斷路器
SpringCloud Fegin默認已為Feign整合了hystrix
- 添加配置
feign:
hystrix:
enabled: true # 開啟Feign的熔斷功能
- 配置FeignClient接口的實現類,並通過@Component注入到Ioc容器中
@Component
public class HystrixMethod implements SpringBootFeign {
@Override
public Map<String, Object> getPerson(int id) {
Map<String, Object> map = new HashMap<>();
map.put("code", 500);
map.put("msg", "sorry,error");
return map;
}
}
- 在@FeignClient註解中添加降級方法
@FeignClient(name = "springbootdemo", fallback = HystrixMethod.class)
public interface SpringBootFeign {
@RequestMapping(value = "/person", method = RequestMethod.GET)
public Map<String, Object> getPerson(@RequestParam(value = "id") int id);
}
- 關閉 springbootdemo 服務,訪問測試

Hystrix Dashboard
- 添加依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
- 在啟動類使用@EnableHystrixDashboard註解激活儀錶盤
@SpringBootApplication
@EnableDiscoveryClient
@EnableHystrix
@EnableHystrixDashboard
public class ConsumeServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumeServerApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate returnBean() {
return new RestTemplate();
}
}
- 訪問頁面
遇到問題://blog.csdn.net/ghyghost/article/details/108053206

斷路器聚合監控Turbine,Turbine是一個聚合Hystrix 監控數據的工具。
網關Gateway
介紹
Spring Cloud Gateway 是 Spring 官方基於 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開發的網關,旨在為微服務架構提供一種簡單而有效的統一的 API 路由管理方式。Spring Cloud Gateway 作為 Spring Cloud 生態系中的網關,目標是替代 Netflflix ZUUL,其不僅提供統一的路由方式,並且基於 Filter 鏈的方式提供了網關基本的功能,例如:安全,監控/埋點,和限流等。
快速入門
- 創建gateway-server模塊,引入依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 編寫啟動類
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
- 編寫路由規則
server:
port: 8765
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
#由一個ID、一個目的URL、一組斷言工廠、一組Filter組成
routes:
# 路由id,可以隨意寫
- id: consume-server-route
# 代理的服務地址
uri: //127.0.0.1:8081
# 路由斷言,可以配置映射路徑
predicates:
- Path=/person/**
eureka:
client:
# 集群url
service-url:
defaultZone: //127.0.0.1:8761/eureka
instance:
prefer-ip-address: true #使用ip註冊
ip-address: 127.0.0.1
instance-id: ${spring.cloud.client.ip-address}:${server.port}
- 啟動測試

路由
- 添加前綴PrefixPath
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
#由一個ID、一個目的URL、一組斷言工廠、一組Filter組成
routes:
# 路由id,可以隨意寫
- id: consume-server-route
# 代理的服務地址
uri: //127.0.0.1:8081
# 路由斷言,可以配置映射路徑
predicates:
- Path=/**
filters:
# 添加請求路徑的前綴
- PrefixPath=/person
相當於PrefixPath=/person//localhost:8765/180868 –》//localhost:8765/person/180868 –》//localhost:8081/person/180868
- 去除前綴StripPrefifix
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
#由一個ID、一個目的URL、一組斷言工廠、一組Filter組成
routes:
# 路由id,可以隨意寫
- id: consume-server-route
# 代理的服務地址
uri: //127.0.0.1:8081
# 路由斷言,可以配置映射路徑
predicates:
- Path=/api/person/**
filters:
# 表示過濾1個路徑,2表示兩個路徑,以此類推
- StripPrefix=1
StripPrefix=1 //localhost:8765/api/person/180868–》//localhost:8765/person/180868–》//localhost:8081/person/180868
- 動態路由
uri以 lb: //開頭(lb代表從註冊中心獲取服務),後面接的就是你需要轉發到的服務名稱
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
routes:
# 路由id,可以隨意寫
- id: consume-server-route
# 代理的服務地址
uri: lb://springbootdemo
# 路由斷言,可以配置映射路徑
predicates:
- Path=/person/**
- 重寫轉發路徑
路由轉發是直接將匹配的路由path直接拼接到映射路徑(URI)之後,那麼往往沒有那麼便利。修改application.yaml
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
routes:
# 路由id,可以隨意寫
- id: consume-server-route
# 代理的服務地址
uri: lb://springbootdemo
# 路由斷言,可以配置映射路徑
predicates:
- Path=/springbootdemo/**
filters:
- RewritePath=/springbootdemo/(?<segment>.*), /$\{segment}
請求//localhost:8765/springbootdemo/person/180868 –》//localhost:8765/person/180868–》//localhost:8081/person/180868( 值得注意的是在yml文檔中 $ 要寫成 $\ )
- 基於服務發現的路由規則
spring:
application:
# 註冊到eureka上的應用名稱
name: gateway_server
cloud:
gateway:
discovery:
locator:
#設置路由訪問方式://Gateway_HOST:Gateway_PORT/大寫的serviceId/**,其中微服務應用名默認大寫訪問。
enabled: true
# 該設置可以將服務名改成小寫(默認為大寫)
lower-case-service-id: true
這樣,就可以通過//localhost:8765/springbootdemo/person?id=180868訪問
過濾器
- 生命周期
- pre:: 這種過濾器在請求被路由之前調用。
- post : 這種過濾器在路由到微服務以後執行。
- 過濾器類型
- 局部過濾器(GatewayFilter),應用到單個路由或者一個分組的路由上,通過 spring.cloud.gateway.routes.filters 配置在具體路由下,只作用在當前路由上;
- 全部過濾器(GlobalFilter),應用到所有的路由上,實現 GlobalFilter 接口即可
- 常見過濾器
| 過濾器名稱 | 說明 |
|---|---|
| AddRequestHeader | 對匹配上的請求加上Header |
| AddRequestParameters | 對匹配上的請求路由添加參數 |
| AddResponseHeader | 對從網關返回的響應添加Header |
| StripPrefifix | 對匹配上的請求路徑去除前綴 |
配置全局默認過濾器
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-MyName, jyd0124
其他過濾器具體可參考GatewayFilterFactory類
-
自定義過濾器
需求:模擬一個登錄的校驗。基本邏輯:如果請求中有token參數,則認為請求有效,放行。
@Component
public class TokenFilter implements GlobalFilter, Ordered {
private final Logger logger = LoggerFactory.getLogger(TokenFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//request,response都可以通過 ServerWebExchange 獲取
String token = exchange.getRequest().getQueryParams().getFirst("token");
if (StringUtils.isBlank(token)) {
logger.info("token is empty ...");
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();
}
//chain.filter 繼續向下游執行
return chain.filter(exchange);
}
@Override
public int getOrder() {
//過濾器的優先級,返回值越大級別越低
return 0;
}
}
跨域問題
@Configuration
public class GwCorsFilter {
/**
* 以下CorsWebFilter跨域處理也可以通過配置文件進行處理
* spring:
* cloud:
* gateway:
* globalcors:
* cors-configurations:
*/
@Bean
public CorsWebFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); // 允許cookies跨域
config.addAllowedOrigin("*");// #允許向該服務器提交請求的URI,*表示全部允許,在SpringMVC中,如果設成*,會自動轉成當前請求頭中的Origin
config.addAllowedHeader("*");// #允許訪問的頭信息,*表示全部
config.setMaxAge(18000L);// 預檢請求的緩存時間(秒),即在這個時間段里,對於相同的跨域請求不會再預檢了
config.addAllowedMethod("OPTIONS");// 允許提交請求的方法類型,*表示全部允許
config.addAllowedMethod("HEAD");
config.addAllowedMethod("GET");
config.addAllowedMethod("PUT");
config.addAllowedMethod("POST");
config.addAllowedMethod("DELETE");
config.addAllowedMethod("PATCH");
UrlBasedCorsConfigurationSource source =
new UrlBasedCorsConfigurationSource(new PathPatternParser());
source.registerCorsConfiguration("/**", config);
return new CorsWebFilter(source);
}
/**
*
*如果使用了註冊中心(如:Eureka),進行控制則需要增加如下配置
*/
@Bean
public RouteDefinitionLocator discoveryClientRouteDefinitionLocator(DiscoveryClient discoveryClient) {
return new DiscoveryClientRouteDefinitionLocator(discoveryClient,new DiscoveryLocatorProperties());
}
/**
* 以下是springboot2.0.5出現only one connection 的解決辦法
* @return
*/
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new HiddenHttpMethodFilter() {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange);
}
};
}
}
負載均衡和熔斷
Gateway中默認已經集成了Ribbon負載均衡和Hystrix熔斷機制。但是所有的策略都是走的默認值,建議根據Ribbon和Hystrix手動進行配置。
網關限流
- 計數器限流算法
- 漏桶算法
- 令牌桶算法
- 基於Filter的限流
- 基於Sentinel的限流
SpringCloud Config配置中心
在gitee上創建遠程配置文件

構建config配置中心服務
- 構建Config-server服務,添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 在config-server的啟動類添加@EnableConfigServer
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 編寫config-server配置文件
server:
port: 8888
spring:
application:
name: config_server
cloud:
config:
server:
git:
#配置文件所在的git倉庫
uri: //gitee.com/jyd0124/springcloudconfig.git
#配置文件分支
default-label: master
#配置文件所在根目錄
search-paths: config
#如果Git倉庫為公開倉庫,可以不填寫用戶名和密碼,如果是私有倉庫需要填寫
username: xxx
password: xxx
eureka:
client:
service-url:
defaultZone: //127.0.0.1:8761/eureka #服務註冊地址
- 可通過//localhost:8888/consume-server.yml測試是否成功
獲取配置中心配置
- 改造其他隨便一個服務,比如我使用我自己的consume_server,添加依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 修改配置
刪除consume_server的application.yml 文件(因為該文件從配置中心獲取)
創建consume_server 的bootstrap.yml 配置文件,其內容如下
spring:
cloud:
config:
name: consume-server
# 遠程倉庫中的版本保持一致
label: master
#profile: dev
#通過ip直接訪問配置中心
#uri: //localhost:8888/
#通過eurka訪問配置中心
discovery:
#配置中心服務
service-id: config-server
enabled: true
eureka:
client:
service-url:
defaultZone: //127.0.0.1:8761/eureka #服務註冊地址
3.啟動consume_server項目發現獲取到配置文件將服務暴露在8081端口,測試接口可成功返回數據
補充:bootstrap.yml文件也是Spring Boot的默認配置文件,而且其加載的時間相比於application.yml更早。bootstrap.yml文件相當於項目啟動時的引導文件,內容相對固定。application.yml文件是微服務的一些常規配置參數,變化比較頻繁。
Spring Cloud Bus服務總線
問題:
前面已經完成了將微服務中的配置文件集中存儲在遠程Git倉庫,如果我們更新Git倉庫中的配置文件,那用戶微服務是否可以及時接收到新的配置信息並更新呢?經過測試,對於Git倉庫中配置文件的修改並沒有及時更新到用戶微服務,只有重啟用戶微服務才能生效。如果想在不重啟微服務的情況下更新配置該如何實現呢? 可以使用Spring Cloud Bus來實現配置的自動更新。
介紹
Spring Cloud Bus是用輕量的消息代理將分佈式的節點連接起來,可以用於廣播配置文件的更改或者服務的監控管理。也就是消息總線可以為微服務做監控,也可以實現應用程序之間相互通信。 Spring Cloud Bus可選的消息代理有兩種:RabbitMQ和Kafka。
具體實現可參考://www.fangzhipeng.com/springcloud/2018/08/08/sc-f8-bus.html
//www.cnblogs.com/babycomeon/p/11141160.html
Spring Cloud 體系技術綜合應用概覽



