Spring Cloud Feign

来源:互联网 发布:鲨鱼网络 编辑:程序博客网 时间:2024/05/28 09:33

Spring Cloud Feign

创建spring-cloud-04-feign-service服务中心 端口为8088

创建spring-cloud-04-feign-produce 端口为2006

  • HystrixC类
@RestControllerpublic class HystrixC {    @RequestMapping(value = "/hello",method = {RequestMethod.GET})    public String hello() throws InterruptedException {        System.err.println("hello hystrix ....");        return "hello hystrix ...";    }    @RequestMapping(value = "/action",method = {RequestMethod.GET})    public String action() throws InterruptedException {        Thread.sleep(4000);        System.err.println("action hystrix ....");        return "action hystrix ...";    }}
  • Application类
@EnableDiscoveryClient@SpringBootApplicationpublic class FeignProduceApplication {    @Bean    public RestTemplate restTemplate(){        return new RestTemplate();    }    public static void main(String[] args) {        SpringApplication.run(FeignProduceApplication.class, args);    }}

创建spring-cloud-04-feign-consumer

  • pom.xml
<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</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-ribbon</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-hystrix</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-feign</artifactId></dependency>
  • application.properties也可以对feign代理做一些高性能输入输出配置
  • 注意:feign在第四个版本后需要手动开启断路器功能才能生效
pring.application.name=feign-consumerserver.context-path=/server.port=2005eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/#启动重试机制spring.cloud.loadbalancer.retry.enabled=true ##断路器hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=50000eq.fact.connect-timeout=1000eq.fact.connection-request-timeout=1000eq.fact.read-timeout=5000#是否对所有请求都进行重试lient-service.ribbon.OKToRetryOnAllOperations=true#重试切换实例得次数lient-service.ribbon.MaxAutoRetriesNextServer=1#重试切次数lient-service.ribbon.MaxAutoRetries=2feign.hystrix.enabled=true#压缩 超过设定得大小得请求才会对其进行压缩 这是默认值feign.compression.request.min-request-size=2048feign.compression.request.mime-types=text/xml,application.xml,application/jsonfeign.compression.request.enabled=truefeign.compression.response.enabled=true
  • Application类
@EnableFeignClients@EnableCircuitBreaker //开启断路器@EnableDiscoveryClient@SpringBootApplicationpublic class FeignConsumerApplication {    @Bean    @ConfigurationProperties(prefix = "req.fact")    public HttpComponentsClientHttpRequestFactory httpComponentsClientHttpRequestFactory() {        return new HttpComponentsClientHttpRequestFactory();    }    @Bean    @LoadBalanced    public RestTemplate restTemplate() {        return new RestTemplate(httpComponentsClientHttpRequestFactory());    }    public static void main(String[] args) {        SpringApplication.run(FeignConsumerApplication.class, args);    }}
  • HelloService接口 这个interface一定是已知服务(也就是注册到Eureka上得接口服务)FeignClient注解就是Fegin得注解声明,里面name属性表示了当前代理得服务AppName,fallback属性是调用服务失败后得降级策略,当网络异常等调用代理失败后,会根据断路器得超时时间降级到指定得fallback赋予得HelloServiceHystrixFallback类中,做降级处理。
@FeignClient(name = "feign-produce", fallback = HelloServiceHystrixFallback.class)public interface HelloService {    @RequestMapping(value = "/hello", method = {RequestMethod.GET})    String hello();    @RequestMapping(value = "/action", method = {RequestMethod.GET})    String action();}
  • ConsumerController类
@RestControllerpublic class ConsumerController {    @Autowired    HelloService helloService;    @RequestMapping(value = "/hello", method = {RequestMethod.GET})    public String helloConsumer() {        return helloService.hello()+"\n";    }    @RequestMapping(value = "/action", method = {RequestMethod.GET})    public String helloAction() {        return helloService.action();    }}
  • 启动所有访问http://localhost:2005/action就可以访问到

fegin日志配置

  • 在application.properties中配置
# feign日志配置logging.level.com.springcloud.feign.HelloService=DEBUG
  • 在Aplication启动类中添加
 @Bean    Logger.Level feignLoggerLevel() {        /**         *  NONE: 不记录任何信息         *  BASIE:仅记录请求方法,URL以及响应状态码和执行时间         *  HEADERS:除了记录BASIE级别得信息之外,还会记录请求和响应得头信息         *  FULL:记录所有请求与响应得明细,包括头信息,请求体,元数据等。         */        return Logger.Level.FULL;    }
  • GitHub地址

  • 下节API网关治理组件Zuul

原创粉丝点击