Spring Cloud Feign

2017-10-09  本文已影响0人  叶小飞_

Spring Cloud Feign

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

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

@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 ...";
    }
}
@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

<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>
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
@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);
    }
}
@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();
}
@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日志配置

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

猜你喜欢

热点阅读