Spring Cloud Feign
阿新 • • 發佈:2018-11-10
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();
}
}
- 啟動所有訪問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;
}
下節API閘道器治理元件Zuul