Spring Cloud Feign

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33127597/article/details/78183094

Spring Cloud Feign

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

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

  • HystrixC类
@RestController
public 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
@SpringBootApplication
public 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-consumer
server.context-path=/
server.port=2005
eureka.client.service-url.defaultZone=http://eureka1:8008/eureka/
#启动重试机制
spring.cloud.loadbalancer.retry.enabled=true 
##断路器
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=50000


eq.fact.connect-timeout=1000
eq.fact.connection-request-timeout=1000
eq.fact.read-timeout=5000

#是否对所有请求都进行重试
lient-service.ribbon.OKToRetryOnAllOperations=true
#重试切换实例得次数
lient-service.ribbon.MaxAutoRetriesNextServer=1
#重试切次数
lient-service.ribbon.MaxAutoRetries=2

feign.hystrix.enabled=true
#压缩 超过设定得大小得请求才会对其进行压缩 这是默认值
feign.compression.request.min-request-size=2048
feign.compression.request.mime-types=text/xml,application.xml,application/json
feign.compression.request.enabled=true
feign.compression.response.enabled=true
  • Application类
@EnableFeignClients
@EnableCircuitBreaker //开启断路器
@EnableDiscoveryClient
@SpringBootApplication
public 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类
@RestController
public 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();
    }
}

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;
    }

猜你喜欢

转载自blog.csdn.net/qq_33127597/article/details/78183094