<Spring Cloud>入門五 hystrix
阿新 • • 發佈:2019-01-13
feign pathvaria bsp lis 每一個 instance lns configure version
1.服務熔斷
1.1引入坐標
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
1.2 主啟動類標識
package org.maple; 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; /** * @authormapleins * @Date 2019-01-12 17:13 * @Desc **/ @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient//服務發現 可以看到服務信息 @EnableCircuitBreaker //開啟斷路器 public class App_Provider_Dept_8001_Hystrix { public static void main(String[] args) { SpringApplication.run(App_Provider_Dept_8001_Hystrix.class,args); } }
1.3 添加熔斷方法
@GetMapping("/dept/get/{id}") //出錯調用hystrixCommand中的方法 @HystrixCommand(fallbackMethod = "hystrix_get") public Dept get(@PathVariable("id") Long id) { Dept dept = service.find(id); if (null == dept) { throw new RuntimeException("該id"+id+"沒有對應信息"); } return dept; } public Dept hystrix_get(@PathVariable("id") Long id){ return new Dept().setDeptNo(id).setDName("該id"+id+"沒有對應的信息,null--@HystrixCommand").setDb_source("no this database in Mysql"); }
1.4 訪問
2.服務降級
添加服務熔斷,會造成方法翻倍,每一個接口都需要一個服務熔斷,此時就可以使用服務降級,類似異常處理+切面編程
2.1 針對接口編寫回調函數工廠,在接口上聲明工廠類
之前將服務的調用通過feign來實現接口調用,現在對接口操作實現服務降級,對整個方法進行統一管理,實現解耦
package org.maple.service; import feign.hystrix.FallbackFactory; import org.maple.entity.Dept; import org.springframework.stereotype.Component; import java.util.List; /** * @author mapleins * @Date 2019-01-13 12:01 * @Desc 服務降級 **/ @Component public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService> { @Override public DeptClientService create(Throwable throwable) { return new DeptClientService() { @Override public boolean add(Dept dept) { return false; } @Override public Dept get(Long id) { return new Dept().setDeptNo(id).setDName("該服務已經關閉").setDb_source("no this database in Mysql"); } @Override public List<Dept> list() { return null; } }; } }
package org.maple.service; import org.maple.entity.Dept; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import java.util.List; /** * @author mapleins * @Date 2019-01-12 23:10 * @Desc 通過接口和註解 面向接口編程訪問微服務 **/ //@FeignClient("ms-provider-dept") @FeignClient(value = "ms-provider-dept",fallbackFactory = DeptClientServiceFallbackFactory.class) //服務降級類 public interface DeptClientService { @PostMapping("/dept/add") boolean add(@RequestBody Dept dept); @GetMapping("/dept/get/{id}") Dept get(@PathVariable("id") Long id); @GetMapping("/dept/list") List<Dept> list(); }
2.2 在調用接口的消費方開啟hystrix
feign:
hystrix:
enabled: true #開啟服務降級
2.3 調用
開啟2個eureka server,1個provider-dept,1個consumer
關閉provider
3.Hystrix Dashboard 準實時調用監控
3.1 操作
需要監控的服務必須依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
新建一個hystrix-dashboard工程
<?xml version="1.0" encoding="UTF-8"?> <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"> <parent> <artifactId>spring-cloud-learning</artifactId> <groupId>org.org.maple</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>ms-consumer-hystrix-dashboard</artifactId> <dependencies> <dependency> <groupId>org.org.maple</groupId> <artifactId>ms-common-api</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> </dependencies> </project>
server:
port: 9001
package org.maple; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; /** * @author mapleins * @Date 2019-01-13 12:44 * @Desc **/ @SpringBootApplication @EnableHystrixDashboard //開啟儀表盤 public class App_Hystrix_Dashboard_9001 { public static void main(String[] args) { SpringApplication.run(App_Hystrix_Dashboard_9001.class,args); } }
3.2 啟動界面
如果訪問路徑404,則需要在監控的服務中配置一個Bean
@Bean public ServletRegistrationBean getServlet() { HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet(); ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet); registrationBean.setLoadOnStartup(1); registrationBean.addUrlMappings("/hystrix.stream"); registrationBean.setName("HystrixMetricsStreamServlet"); return registrationBean; }
<Spring Cloud>入門五 hystrix