當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringCloud:Hystrix 熔断机制(基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
生活随笔
收集整理的這篇文章主要介紹了
SpringCloud:Hystrix 熔断机制(基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
所謂的熔斷機(jī)制和日常生活中見到電路保險(xiǎn)絲是非常相似的,當(dāng)出現(xiàn)了問題之后,保險(xiǎn)絲會(huì)自動(dòng)燒斷,以保護(hù)我們的電器, 那么如果換到了程序之中呢?當(dāng)現(xiàn)在服務(wù)的提供方出現(xiàn)了問題之后整個(gè)的程序?qū)⒊霈F(xiàn)錯(cuò)誤的信息顯示,而這個(gè)時(shí)候如果不想出現(xiàn)這樣的錯(cuò)誤信息,而希望替換為一個(gè)錯(cuò)誤時(shí)的內(nèi)容。
一個(gè)服務(wù)掛了后續(xù)的服務(wù)跟著不能用了,這就是雪崩效應(yīng)對(duì)于熔斷技術(shù)的實(shí)現(xiàn)需要考慮以下幾種情況:· 出現(xiàn)錯(cuò)誤之后可以 fallback 錯(cuò)誤的處理信息;· 如果要結(jié)合 Feign 一起使用的時(shí)候還需要在 Feign(客戶端)進(jìn)行熔斷的配置。
2.1、Hystrix 基本配置1、 【microcloud-provider-dept-hystrix-8001】修改 pom.xml 配置文件,追加 Hystrix 配置類:<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>
2、 【microcloud-provider-dept-hystrix-8001】修改 DeptRest 程序package cn.study.microcloud.rest;import javax.annotation.Resource;import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import cn.study.microcloud.service.IDeptService;
import cn.study.vo.Dept;@RestController
public class DeptRest {@Resourceprivate IDeptService deptService;@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)@HystrixCommand(fallbackMethod="getFallback") // 如果當(dāng)前調(diào)用的get()方法出現(xiàn)了錯(cuò)誤,則執(zhí)行fallbackpublic Object get(@PathVariable("id") long id) {Dept vo = this.deptService.get(id) ; // 接收數(shù)據(jù)庫的查詢結(jié)果if (vo == null) { // 數(shù)據(jù)不存在,假設(shè)讓它拋出個(gè)錯(cuò)誤throw new RuntimeException("部門信息不存在!") ;}return vo ;}public Object getFallback(@PathVariable("id") long id) { // 此時(shí)方法的參數(shù) 與get()一致Dept vo = new Dept() ;vo.setDeptno(999999L);vo.setDname("【ERROR】Microcloud-Dept-Hystrix"); // 錯(cuò)誤的提示vo.setLoc("DEPT-Provider");return vo ;}}一旦 get()方法上拋出了錯(cuò)誤的信息,那么就認(rèn)為該服務(wù)有問題,會(huì)默認(rèn)使用“@HystrixCommand”注解之中配置好的 fallbackMethod 調(diào)用類中的指定方法,返回相應(yīng)數(shù)據(jù)。
3、 【microcloud-provider-dept-hystrix-8001】在主類之中啟動(dòng)熔斷處理package cn.study.microcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker
@EnableDiscoveryClient
public class Hystrix_Dept_8001_StartSpringCloudApplication {public static void main(String[] args) {SpringApplication.run(Hystrix_Dept_8001_StartSpringCloudApplication.class, args);}
}現(xiàn)在的處理情況是:服務(wù)器出現(xiàn)了錯(cuò)誤(但并不表示提供方關(guān)閉),那么此時(shí)會(huì)調(diào)用指定方法的 fallback處理。
2.2、服務(wù)降級(jí)(服務(wù)回退)所有的 RPC 技術(shù)里面服務(wù)降級(jí)是一個(gè)最為重要的話題,所謂的降級(jí)指的是當(dāng)服務(wù)的提供方不可使用的時(shí)候,程序不會(huì)出現(xiàn)異常,而會(huì)出現(xiàn)本地的操作調(diào)用。例如:在每年年底 12306 都是最繁忙的時(shí)候,那么在這個(gè)情況會(huì)發(fā)現(xiàn)有一些神奇的情況:當(dāng)?shù)搅酥付ǖ臅r(shí)間
大家開始搶票的 時(shí)候,如果你不搶,而后查詢一些冷門的車次,票有可能查詢不出來。因?yàn)檫@個(gè)時(shí)候會(huì)將所有
的系統(tǒng)資源給搶票調(diào)度了,而其它的 服務(wù)由于其暫時(shí)不受到過多的關(guān)注,這個(gè)時(shí)候可以考慮將服務(wù)降級(jí)
(服務(wù)暫停)。服務(wù)的降級(jí)處理是在客戶端實(shí)現(xiàn)的,與你的服務(wù)器端沒有關(guān)系。
1、 【microcloud-service】擴(kuò)充一個(gè) IDeptService 的失敗調(diào)用(服務(wù)降級(jí))處理:package cn.study.service.fallback;import java.util.List;import org.springframework.stereotype.Component;import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;
import feign.hystrix.FallbackFactory;@Component
public class IDeptClientServiceFallbackFactoryimplementsFallbackFactory<IDeptClientService> {@Overridepublic IDeptClientService create(Throwable cause) {return new IDeptClientService() {@Overridepublic Dept get(long id) {Dept vo = new Dept();vo.setDeptno(888888L);vo.setDname("【ERROR】Feign-Hystrix"); // 錯(cuò)誤的提示vo.setLoc("Consumer客戶端提供");return vo;}@Overridepublic List<Dept> list() {return null;}@Overridepublic boolean add(Dept dept) {return false;}};}}
2、 【microcloud-service】修改 IDeptClientService 接口,追加本地的 Fallback 配置。package cn.study.service;import java.util.List;import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import cn.study.commons.config.FeignClientConfig;
import cn.study.service.fallback.IDeptClientServiceFallbackFactory;
import cn.study.vo.Dept;@FeignClient(value = "MICROCLOUD-PROVIDER-DEPT", configuration
= FeignClientConfig.class, fallbackFactory = IDeptClientServiceFallbackFactory.class)
public interface IDeptClientService {@RequestMapping(method = RequestMethod.GET, value = "/dept/get/{id}")public Dept get(@PathVariable("id") long id);@RequestMapping(method = RequestMethod.GET, value = "/dept/list")public List<Dept> list();@RequestMapping(method = RequestMethod.POST, value = "/dept/add")public boolean add(Dept dept);
}
3、 【microcloud-consumer-hystrix】修改 application.properties 配置文件,追加 feign 配置啟用。feign.hystrix.enabled=true
package cn.study.microcloud.controller;import javax.annotation.Resource;import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import cn.study.service.IDeptClientService;
import cn.study.vo.Dept;@RestController
public class ConsumerDeptController {@Resourceprivate IDeptClientService deptService ;@RequestMapping(value = "/consumer/dept/get")public Object getDept(long id) {return this.deptService.get(id);}@RequestMapping(value = "/consumer/dept/list")public Object listDept() {return this.deptService.list();}@RequestMapping(value = "/consumer/dept/add")public Object addDept(Dept dept) throws Exception {return this.deptService.add(dept);}
}
?
4、 【microcloud-consumer-hystrix】修改程序啟動(dòng)主類:package cn.study.microcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; import org.springframework.context.annotation.ComponentScan;@SpringBootApplication @EnableEurekaClient @ComponentScan("cn.study.service,cn.study.microcloud") @EnableFeignClients(basePackages={"cn.study.service"}) public class Hystrix_Consumer_80_StartSpringCloudApplication {public static void main(String[] args) {SpringApplication.run(Hystrix_Consumer_80_StartSpringCloudApplication.class,args);} }當(dāng)追加上了“@ComponentScan("cn.mldn.service")”注解之后才可以進(jìn)行包的掃描配置。此時(shí)即使服務(wù)端無法繼續(xù)提供服務(wù)了,由于存在有服務(wù)降級(jí)機(jī)制,也會(huì)保證服務(wù)不可用時(shí)可以得到一些固定的 提示信息。 2.3、HystrixDashboard服務(wù)監(jiān)控在 Hystrix 里面提供有一種監(jiān)控的功能,那么這個(gè)功能就是“Hystrix Dashboard”,可以利用它來進(jìn)行整體微服務(wù)的監(jiān)控操作。 1、 首先為了方便監(jiān)控,將建立一個(gè)新的監(jiān)控項(xiàng)目:microcloud-consumer-hystrix-dashboard; 2、 【microcloud-consumer-hystrix-dashboard】修改項(xiàng)目中的 pom.xml 配置文件:<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency> 3、 【microcloud-provider-*】所有的服務(wù)提供者之中都一定要提供有監(jiān)控服務(wù)依賴庫:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency> 4、 【microcloud-consumer-hystrix-dashboard】修改 application.properties配置文件,主要進(jìn)行端口的配置:application.propertiesserver.port = 9001 5、 【microcloud-consumer-hystrix-dashboard】創(chuàng)建一個(gè)監(jiān)控的主類:package cn.study.microcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@SpringBootApplication @EnableHystrixDashboard public class HystrixDashboardApplication_9001 {public static void main(String[] args) {SpringApplication.run(HystrixDashboardApplication_9001.class, args);} } 6、 修改 hosts 主機(jī)文件,增加主機(jī)列表:127.0.0.1 dashboard.com服務(wù)運(yùn)行地址:http://dashboard.com:9001/hystrix 7、 得到 microcloud-provider-dept 的監(jiān)控信息:http://studyjava:hello@dept-8001.com:8001/hystrix.stream· 如果此時(shí)要想獲取監(jiān)控信息必須去運(yùn)行微服務(wù); 8、 將之前的監(jiān)控的路徑 http://studyjava:hello@dept-8001.com:8001/hystrix.stream填寫到之前啟動(dòng)好的 dashboard 程序頁面之中http://localhost/consumer/dept/get?id=1 2.4、Turbine 聚合監(jiān)控HystrixDashboard 主要的功能是可以針對(duì)于某一項(xiàng)微服務(wù)進(jìn)行監(jiān)控,但是如果說現(xiàn)在有許多的微服務(wù)需要進(jìn)行整體的監(jiān)控,那 么這種情況下就可以利用 turbine 技術(shù)來實(shí)現(xiàn)。1、 下面準(zhǔn)備出一個(gè)新的微服務(wù):Company,這個(gè)微服務(wù)不打算使用 SpringSecurity 安全處理以及 Mybatis 數(shù)據(jù)庫連接,只是做一 個(gè)簡單的數(shù)據(jù)信息。通過一個(gè)已有的 microcloud-provider-hystrix-8001 復(fù)制一個(gè)新的項(xiàng)目:microcloud-provider-company-8101; 2、 【microcloud-provider-company-8101】修改項(xiàng)目中的 pom.xml 配置文件,將與安全有關(guān)的依賴包 刪除掉以及與數(shù)據(jù)庫連接池、MyBatis 的相關(guān)的程序類或接口全部刪除掉,只保留有用的包;<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>microcloud-provider-company-8101</groupId><artifactId>microcloud-provider-company-8101</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>cn.study</groupId><artifactId>microcloud</artifactId><version>0.0.1</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>cn.study</groupId><artifactId>microcloud-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jetty</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>springloaded</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId></dependency></dependencies></project> 3、 【microcloud-api】追加一個(gè)新的 VO 類:Company。package cn.study.vo;import java.io.Serializable;@SuppressWarnings("serial") public class Company implements Serializable {private String title ;private String note ;public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getNote() {return note;}public void setNote(String note) {this.note = note;}@Overridepublic String toString() {return "Company [title=" + title + ", note=" + note + "]";} } 4、 【microcloud-provider-company-8101】建立一個(gè)新的微服務(wù)的程序類:CompanyRestpackage cn.study.microcloud.rest;import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import cn.study.vo.Company;@RestController public class CompanyRest {@RequestMapping(value = "/company/get/{title}", method = RequestMethod.GET)@HystrixCommand // 如果需要進(jìn)行性能監(jiān)控,那么必須要有此注解public Object get(@PathVariable("title") String title) {Company vo = new Company() ;vo.setTitle(title);vo.setNote("www.study.cn");return vo ;} } 5、 【microcloud-provider-company-8101】修改程序的啟動(dòng)主類:package cn.study.microcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication @EnableEurekaClient @EnableCircuitBreaker @EnableDiscoveryClient public class Company_8101_StartSpringCloudApplication {public static void main(String[] args) {SpringApplication.run(Company_8101_StartSpringCloudApplication.class, args);} } 6、 【microcloud-provider-company-8101】修改 application.properties 配置文件:server.port = 8101eureka.client.serviceUrl.defaultZone= http://edmin:studyjava@eureka1:7001/eureka, http://edmin:studyjava@eureka2:7002/eureka, http://edmin:studyjava@eureka3:7003/eurekaspring.application.name=microcloud-provider-companyeureka.instance.instance-id=dept-8001.comeureka.instance.prefer-ip-address=trueinfo.app.name=study-microcloud info.company.name=www.study.cn info.pom.artifactId=$project.artifactId$ info.pom.version=$project.version$ 7、 【microcloud-provider-company-8101】啟動(dòng)微服務(wù),隨后取得監(jiān)控信息:· 在 hosts 配置文件之中追加有一個(gè)映射路徑:127.0.0.1 company-8101.com · 訪問地址:http://company-8101.com:8101/company/get/hello{"title":"hello","note":"www.study.cn"}· hystrix 監(jiān)控地址:http://company-8101.com:8101/hystrix.stream 8、 如 果 要 想 實(shí) 現(xiàn) trubine 的 配 置 , 則需要建立一個(gè) turbine項(xiàng)目模塊 , 這個(gè)項(xiàng)目可以直接 通過之前的 microcloud-consumer-hystrix-dashboard 模塊進(jìn)行復(fù)制為 “microcloud-consumer-turbine”模塊; 9、 【microcloud-consumer-turbine】修改 pom.xml 配置文件,追加 turbine 的依賴程序包:<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>microcloud-consumer-turbine</groupId><artifactId>microcloud-consumer-turbine</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>cn.study</groupId><artifactId>microcloud</artifactId><version>0.0.1</version></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><thymeleaf.version>3.0.9.RELEASE</thymeleaf.version><thymeleaf-layout-dialect.version>2.3.0</thymeleaf-layout-dialect.version><!-- 布局功能的支持程序 thymeleaf3主程序 layout2以上版本 --><!-- thymeleaf2 layout1 --><thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version></properties> <dependencies><dependency><!-- 引入web模塊 --><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><dependency><groupId>microboot-restful-api</groupId><artifactId>microboot-restful-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>cn.study</groupId><artifactId>microcloud-api</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-ribbon</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-feign</artifactId></dependency><dependency><groupId>microcloud-service</groupId><artifactId>microcloud-service</artifactId><version>0.0.1-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix-dashboard</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-turbine</artifactId></dependency></dependencies> </project> 10、 【microcloud-consumer-turbine】修改 application.properties 配置文件:server.port = 9101eureka.client.serviceUrl.defaultZone= http://edmin:studyjava@eureka1:7001/eureka, http://edmin:studyjava@eureka2:7002/eureka, http://edmin:studyjava@eureka3:7003/eureka spring.application.name=microcloud-provider-company eureka.instance.instance-id=dept-8001.com eureka.instance.prefer-ip-address=trueturbine.appConfig=MICROCLOUD-PROVIDER-COMPANY,MICROCLOUD-PROVIDER-DEPT turbine.clusterNameExpression=new String("default") 11、 【microcloud-consumer-turbine】建立一個(gè) turbine 的使用主類信息package cn.study.microcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.cloud.netflix.turbine.EnableTurbine;@SpringBootApplication @EnableHystrixDashboard @EnableTurbine public class TurbineApplication_9101 {public static void main(String[] args) {SpringApplication.run(TurbineApplication_9101.class, args);} } 12、 【microcloud-consumer-hystrix-dashboard】運(yùn)行 hystrix-dashboard 監(jiān)控程序; 13、 【microcloud-consumer-turbine】運(yùn)行 trubine 聚合監(jiān)控程序;· 但是在正常啟動(dòng) trubine 的時(shí)候出現(xiàn)了以下的錯(cuò)誤提示信息,這是因?yàn)闆]有對(duì)有安全認(rèn)證的微服務(wù)MICROCLOUD-PROVIDER-DEPT進(jìn)行安全認(rèn)證· 修改 hosts 配置文件,追加一個(gè)映射路徑:127.0.0.1 turbine.com· trubine 訪問路徑:http://turbine.com:9101/turbine.stream 14、 運(yùn)行 HystrixDashboard 監(jiān)控程序:http://dashboard.com:9001/hystrix.stream· 在監(jiān)控的位置上填寫之前設(shè)置好的 turbine 的訪問地址看到的效果如下: 15、 【microcloud-security】如果現(xiàn)在需要 turbine 進(jìn)行加密的微服務(wù)的訪問操作,只能夠采取一種折衷方案,就是要去修改整個(gè)項(xiàng)目中的安全策略,追加 WEB 安全策略配置:package cn.study.microcloud.config;import javax.annotation.Resource;import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders .AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.builders.WebSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration .WebSecurityConfigurerAdapter; import org.springframework.security.config.http.SessionCreationPolicy;@Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter {@Overridepublic void configure(WebSecurity web) throws Exception {web.ignoring().antMatchers("/hystrix.stream","/turbine.stream") ;}@Resourcepublic void configGlobal(AuthenticationManagerBuilder auth)throws Exception {auth.inMemoryAuthentication().withUser("studyjava").password("hello").roles("USER").and().withUser("admin").password("hello").roles("USER", "ADMIN");}@Overrideprotected void configure(HttpSecurity http) throws Exception {// 表示所有的訪問都必須進(jìn)行認(rèn)證處理后才可以正常進(jìn)行http.httpBasic().and().authorizeRequests().anyRequest().fullyAuthenticated();// 所有的Rest服務(wù)一定要設(shè)置為無狀態(tài),以提升操作性能http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);} } 現(xiàn)在所有的安全策略會(huì)自動(dòng)拋開以上的兩個(gè)訪問路徑,這種是基于 Bean 配置,如果要是你現(xiàn)在基于的是 application.yml 文件的配置,則就必須修改 application.yml 配置文件,追加如下內(nèi)容:security:ignored:- /hystrix.stream- /turbine.stream這個(gè)時(shí)候如果啟動(dòng)之后沒有出現(xiàn)任何的錯(cuò)誤提示,那么就表示現(xiàn)在已經(jīng)可以繞過了 Security 的配置而直接進(jìn)行服務(wù)的訪問了。?
總結(jié)
以上是生活随笔為你收集整理的SpringCloud:Hystrix 熔断机制(基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringCloud:Feign接口转
- 下一篇: gradle idea java ssm