feign配置ribbon
全局配置
直接实用ribbon.<key>=<value>
如 ribbon.ConnectTimeout=500
ribbon.ReadTimeout=5000
指定服务配置
实用 <服务名>.ribbon.<key>=<value>
hello-service.ribbon.ConnectTimeout=500
hello-service.ribbon.ReadTimeout=5000
版本 spring boot 1.5.14
feign配置hystrix
具体详细代码 请参考 申明式web服务客户端 Feign 基本使用
服务降级配置:
helloService 接口 |
package springcloud.helloserviceapi; import org.springframework.web.bind.annotation.*;
@RequestMapping("helloController") public interface HelloService {
@RequestMapping(value = "/hello",method = RequestMethod.GET) String hello();
@RequestMapping(value = "/hello1",method = RequestMethod.GET) String hello(@RequestParam("name") String name);
@RequestMapping(value = "/hello2",method = RequestMethod.GET) User hello(@RequestHeader("name") String name, @RequestHeader("age") Integer age);
/* @RequestMapping(value = "/hello3",method = RequestMethod.POST) String hello(@RequestBody User user);*/
} |
调整feign接口 |
package springcloud.feign; import org.springframework.cloud.netflix.feign.FeignClient; import org.springframework.web.bind.annotation.*; import springcloud.helloserviceapi.HelloService; import springcloud.helloserviceapi.User;
@FeignClient(name = "hello-service",fallback = HelloServiceFallback.class) public interface HelloServiceExtends extends HelloService{
}
|
添加 fallback接口
注意 @RequestMapping 必须要更 HelloService 的不一样,否则会冲突 @RequestMapping("fallback/helloController") |
package springcloud.feign;
import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.*; import springcloud.helloserviceapi.HelloService; import springcloud.helloserviceapi.User;
@Component @RequestMapping("fallback/helloController") public class HelloServiceFallback implements HelloServiceExtends{
@Override public String hello() { return "error"; }
@Override public String hello(String name) { return null; }
@Override public User hello(String name, Integer age) { return null; } } |
结果:
请求压缩
spring cloud feign 支持对请求与响应进行GZIP,以减少通信过程中的性能损耗。我们只需要通过
下面两个参数设置,就能开启请求与响应的压缩功能
feign.compression.request.enabled=true
feign.compression.response.enabled=true
feign 的属性类
@ConfigurationProperties("feign.compression.request")
public class FeignClientEncodingProperties
参考 :
小马哥 spring cloud 视频
spring cloud微服务实战(程序员DD)