什么是Feign
Feign是一个声明式WebService客户端,可以在微服务之间互调
首先如何整合Feign
第一步加依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
第二步:写注解
@EnableFeignClients //在启动类上加
具体例子使用
例子1
@FeignClient(name=“trade-server”) //这个服务的名字
public interface ITradeService{
@RequestMapping(value="/trade/queryFund")
StringqueryFund(@RequestParam(“account”) String account);
@RequestMapping(value="/trade/order")
Stringorder(@RequestParam(“stock”)String stock,@RequestParam(“price”)Double price,@RequestParam(“count”)Double count);
@RequestMapping(value="/trade/orderJSON")
StringorderJson(@RequestBodyJSONObject json);
}
例子2
@FeignClient(name=“user-center”) //这个服务的名字
public interface UserCenterFeignClient{
@GetMapping("/users/{id}")
UserM findById(@PathVariable Integer id);
}
组成,目前前默认就好
- Decoder – ResponseEntityDecoder,包装SpringDecoder, 用于解码Response
- 编码器– SpringEncoder,用于编码RequestBody
- 记录器– Slf4jLogger是Feign使用的默认记录器
- 契约– SpringMvcContract,提供注释处理
- Feign-Builder – HystrixFeign.Builder用于构造组件
- 客户端– LoadBalancerFeignClient或默认的Feign客户端
自定义Http请求器
@FeignClient(value = "jplaceholder",
url = "https://jsonplaceholder.typicode.com/",
configuration = MyClientConfiguration.class)
@Configuration
public class MyClientConfiguration {
@Bean
public OkHttpClient client() {
return new OkHttpClient();
}
}
feign-okhttp 地址 : feign-okhttp
feign-httpclient 地址 : feign-httpclient
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
服务降级,也就是服务请求失败的处理方案
我们需要启用Hystrix,并在属性文件中添加 feign.hystrix.enabled = true。
@Component
public class JSONPlaceHolderFallback implements JSONPlaceHolderClient {
@Override
public List<Post> getPosts() {
return Collections.emptyList();
}
@Override
public Post getPostById(Long postId) {
return null;
}
}
@FeignClient(value = "jplaceholder",
url = "https://jsonplaceholder.typicode.com/",
fallback = JSONPlaceHolderFallback.class)
public interface JSONPlaceHolderClient {
// APIs
}
日志打印功能
代码设置
logging:
level:
com.liuyun: debug
#如果我们只想为包中的一个特定客户端启用日志记录,则可以使用完整的类名:
com.liuyun.cloud.openfeign.client.JSONPlaceHolderClient: debug
public class ClientConfiguration {
@Bean
Logger.Level feignLoggerLevel() {
return Logger.Level.FULL;
}
}
@FeignClient(name=“user-center”,configuration = ClientConfiguration.class) // 在这里配置
public interface UserCenterFeignClient{
@GetMapping("/users/{id}")
UserM findById(@PathVariable Integer id);
}
yml属性配置
logging:
level:
com.liuyun: debug
feign:
client:
config:
user-center:
loggerLevel: full
代码全局配置Fegin的Log打印
代码如下
@MapperScan(basePackages = "com.liuyun")
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = UserCenterFeignConfiguration.class) //这一行的配置
public class ContentCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ContentCenterApplication.class, args);
}
}
public class UserCenterFeignConfiguration {
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
属性全局配置Fegin的Log打印
yml文件里面更改
logging:
level:
com.liuyun: debug
feign:
client:
config:
default:
loggerLevel: full
Feign多参数请求构造
get
@FeignClient(name = "study02")
public interface CommentFeignClient {
@GetMapping("/find")
public DemoComment query(@SpringQueryMap DemoComment comment);
}
//或者
@FeignClient(name = "study02")
public interface CommentFeignClient {
@RequestMapping(value = "/find",method = RequestMethod.GET)
public DemoComment query(@RequestParam("id") Long id, @RequestParam("name") String name);
}
post
@FeignClient(name = "study02")
public interface CommentFeignClient {
@RequestMapping(value = "/save",method = RequestMethod.POST)
public DemoComment query(@RequestBody DemoComment comment);
}
Feign性能优化
加依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
写配置
feign:
httpclient:
# 让feign使用apache httpclient作请求
enabled: true
# feign的最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
OkHttp配置
<!-- https://mvnrepository.com/artifact/io.github.openfeign/feign-okhttp -->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>11.0</version>
</dependency>
fegin:
httpclient:
# feign 最大连接数
max-connections: 200
# feign 单个路径请求的最大连接数
max-connections-per-route: 50
okhttp:
enabled: true
合理打印日志可以提高性能
生产环境使用 Logger.Level.BASIC