我是靠谱客的博主 温柔皮皮虾,最近开发中收集的这篇文章主要介绍Fegin集成Hystrix实现声明式服务调用及服务熔断降级,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1:fegin集成了Hystrix。所以只需要引入feigin的依赖:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>

2:启动类添加注解开启fegin声明式客户端调用和Hystrix的熔断降级启用策略

package com.itmuch.cloud.microserviceconsumermoviefeign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableCircuitBreaker
@ServletComponentScan
@EnableHystrixDashboard
public class MicroserviceConsumerMovieFeignApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceConsumerMovieFeignApplication.class, args);
}
}

3:fegin集成Hystrix。但是需要配置Hystrix可用,已经熔断超时时长。

#开启feign对hystrix的支持
feign:
hystrix:
enabled: true
#修改feign客户端的超时时间 500ms
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 5000

4:编写声明式服务调用feiginClient代码:value = "microservice-provider-user",path = "/userController"指定调用的服务已经服务的请求路径

package com.itmuch.cloud.microserviceconsumermoviefeign.FeginClient;
import com.itmuch.cloud.microserviceconsumermoviefeign.entity.User;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
* : 描述信息
*
* @author liyy
* @date 2018-07-28 14:59
*/
@FeignClient(value = "microservice-provider-user",path = "/userController",fallback = UserFeignFallBack.class)
public interface UserFeignClient{
@GetMapping(value = "/{id}")
public User findOne(@RequestParam("id") Long id);
}

回调类:

package com.itmuch.cloud.microserviceconsumermoviefeign.FeginClient;
import com.itmuch.cloud.microserviceconsumermoviefeign.controller.FeignController;
import com.itmuch.cloud.microserviceconsumermoviefeign.entity.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* : 描述信息
*
* @author liyy
* @date 2018-07-31 11:21
*/
@Component
public class UserFeignFallBack implements UserFeignClient{
public static Logger logger = LoggerFactory.getLogger(UserFeignFallBack.class);
@HystrixCommand
public User findOne(Long id) {
logger.info("断路器打开,执行回调");
User user = new User();
user.setId(id);
user.setAge("-1");
user.setUsername("zs");
return user;
}
}

 

最后

以上就是温柔皮皮虾为你收集整理的Fegin集成Hystrix实现声明式服务调用及服务熔断降级的全部内容,希望文章能够帮你解决Fegin集成Hystrix实现声明式服务调用及服务熔断降级所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部