12、Feign整合断路器Hystrix |
您所在的位置:网站首页 › feignclient原理 › 12、Feign整合断路器Hystrix |
公众号: java 乐园 上编说了《RestTemplate+Ribbon整合断路器Hystrix》,这篇来看看如何Feign整合断路器Hystrix,Feign整合断路器Hystrix也是相对比较简单的。Feign默认已经自带断路器Hystrix,所以不需要像RestTemplate+Ribbon整合断路器Hystrix那样需要在SpringBoot的启动类添加注解。但是Feign自带断路器并没有打开,需要做些额外的配置。 feign: hystrix: enabled: true1、 新建项目sc-eureka-client-consumer-feign-hystrix,对应的pom.xml文件如下 4.0.0 spring-cloud sc-eureka-client-consumer-feign 0.0.1-SNAPSHOT jar sc-eureka-client-consumer-feign http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import UTF-8 1.8 1.8 org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.cloud spring-cloud-starter-openfeign备注:从继续关系可以看出spring-cloud-starter-openfeign已经集成断路器Hystrix 2、 新建springboot启动类 package sc.consumer; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(ConsumerFeignApplication.class, args); } }3、 新建配置文件bootstrap.yml和application.yml bootstrap.yml server: port: 5800 application.yml spring: application: name: sc-eureka-client-consumer-feign-hystrix eureka: client: registerWithEureka: true #是否将自己注册到Eureka服务中,默认为true fetchRegistry: true #是否从Eureka中获取注册信息,默认为true serviceUrl: defaultZone: http://localhost:5001/eureka/ feign: hystrix: enabled: true说明:在application.yml配置文件添加了开启断路器Hystrix的配置项 4、 新建服务消费者类UserService.java package sc.consumer.service; import java.util.Map; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.DeleteMapping; 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.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import sc.consumer.model.User; import sc.consumer.service.hystrix.UserServiceHystrix; @FeignClient(value="sc-eureka-client-provider", fallback=UserServiceHystrix.class) public interface UserService { @GetMapping("/user/getUser/{id}") Map getUser(@PathVariable(value ="id") Long id); @RequestMapping("/user/listUser") Map listUser(); @PostMapping("/user/addUser") Map addUser(@RequestBody User user); @PutMapping("/user/updateUser") Map updateUser(@RequestBody User user); @DeleteMapping("/user/deleteUser/{id}") Map deleteUser(@PathVariable(value ="id") Long id); }可以看到在该类的FeignClient注解上添加了一个属性fallback 5、 新建断路器处理类UserServiceHystrix.java package sc.consumer.service.hystrix; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Component; import sc.consumer.model.User; import sc.consumer.service.UserService; @Component public class UserServiceHystrix implements UserService{ @Override public Map getUser(Long id) { Map result = new HashMap(); result.put("code", "000000"); result.put("msg", "success"); User u = new User(); u.setId(-1L); u.setUserName("failBackName"); result.put("body", u); return result; } @Override public Map listUser() { Map result = new HashMap(); result.put("code", "000000"); result.put("msg", "success"); List list = new ArrayList(); User u = new User(); u.setId(-1L); u.setUserName("failBackName"); list.add(u); result.put("body", list); return result; } @Override public Map addUser(User user) { Map result = new HashMap(); result.put("code", "000000"); result.put("msg", "success"); result.put("body", 0); return result; } @Override public Map updateUser(User user) { Map result = new HashMap(); result.put("code", "000000"); result.put("msg", "success"); result.put("body", 0); return result; } @Override public Map deleteUser(Long id) { Map result = new HashMap(); result.put("code", "000000"); result.put("msg", "success"); result.put("body", 0); return result; } }该类实现了UserService接口,并实现了该接口的所有方法。 6、 分别启动注册中心sc-eureka-server和服务提供者sc-eureka-client-provider 7、 启动sc-eureka-client-consumer-feign-hystrix,并验证是否启动成功 方式一:查看日志看看对应的端口是启动 方式二:查看注册中心是否注册成功 8、 使用postman验证断路器是否启用 上篇从服务提供者是否正常启动验证断路器是否启动,今天看看从数据库是否启动来验证断路器是否启动 (1)MySQL服务正常启动时访问: http://127.0.0.1:5800/feign/user/listUser (2)MySQL服务关闭时访问 http://127.0.0.1:5800/feign/user/listUser 再看下后台日志: 其他接口可以自行按照以上方式进行验证,看看断路器是否启动
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |