标签:create method 微服务 run ado 分享 find common framework
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
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; /** * @author mapleins * @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); } }
@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"); }
添加服务熔断,会造成方法翻倍,每一个接口都需要一个服务熔断,此时就可以使用服务降级,类似异常处理+切面编程
之前将服务的调用通过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(); }
feign:
hystrix:
enabled: true #开启服务降级
开启2个eureka server,1个provider-dept,1个consumer
关闭provider
需要监控的服务必须依赖:
<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); } }
如果访问路径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; }
标签:create method 微服务 run ado 分享 find common framework
原文地址:https://www.cnblogs.com/mapleins/p/10262366.html