一、整合feign
第一步:给三个模块引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
第二步:在启动类上加上@EnableFeignClients
第三步:编写代码
package com.badcat.a.feignClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author 50244
*/
@FeignClient(name = "b")
public interface BFeignClinet {
@GetMapping("/hello/say_hello")
String sayHello();
}
package com.badcat.a.controller;
import com.badcat.a.feignClient.BFeignClinet;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
/**
* @author 50244
*/
@Slf4j
@RestController
@RequestMapping("/feign")
public class TestFeignController {
@Autowired
private BFeignClinet bFeignClinet;
@GetMapping("/let_module-b_say_hello")
public String letModuleBSayHello(){
String result = this.bFeignClinet.sayHello();
log.info("module-b say : [{}]", result);
return String.format("module-b say : [%s]", result);
}
}
调用结果,完美兼容ribbon组件:
二、feign的日志级别
下面使用全局配置的方式将日志级别修改为full
前提:需要先将feign接口所在的包的日志级别修改为debug
logging:
level:
## 设置feign接口的日志级别为debug
com.badcat.a.feignClient: debug
方式一:Java代码
package com.badcat.a.config.feign;
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class FeignConfiguration {
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
修改启动文件的注解:@EnableFeignClients(defaultConfiguration = FeignConfiguration.class)
package com.badcat.a;
import com.badcat.a.config.feign.FeignConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @author 50244
*/
@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignConfiguration.class)
public class AApplication {
public static void main(String[] args) {
SpringApplication.run(AApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
调用结果,调用module-b、module-c都呈现full级别日志:
方式二:配置文件方式
回退上面Java方式的代码,添加如下配置
feign:
client:
config:
default:
loggerLevel: full
调用结果,调用module-b、module-c都呈现full级别日志:
三、细粒度控制feign日志级别
下面修改日志级别,调用module-b的日志级别是full,调用module-c的日志级别是basic
前提:需要先将feign接口所在的包的日志级别修改为debug
logging:
level:
## 设置feign接口的日志级别为debug
com.badcat.a.feignClient: debug
方式一:Java代码:
注意:@Configuration的问题同上一章ribbon类似。
package com.badcat.a.config.feign;
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
public class BFeignConfiguration {
@Bean
public Logger.Level level(){
return Logger.Level.FULL;
}
}
package com.badcat.a.feignClient;
import com.badcat.a.config.feign.BFeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author 50244
*/
@FeignClient(name = "b", configuration = BFeignConfiguration.class)
//@FeignClient(name = "b")
public interface BFeignClinet {
@GetMapping("/hello/say_hello")
String sayHello();
}
package com.badcat.a.config.feign;
import feign.Logger;
import org.springframework.context.annotation.Bean;
public class CFeignConfiguration {
@Bean
public Logger.Level level(){
return Logger.Level.BASIC;
}
}
package com.badcat.a.feignClient;
import com.badcat.a.config.feign.BFeignConfiguration;
import com.badcat.a.config.feign.CFeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* @author 50244
*/
@FeignClient(name = "c", configuration = CFeignConfiguration.class)
//@FeignClient(name = "c")
public interface CFeignClinet {
@GetMapping("/hello/say_hello")
String sayHello();
}
调用结果,调用module-b呈现full级别日志,调用module-c呈现basic基本别日志:
方式二:配置文件方式
先将Java方式的代码还原,在配置文件中添加如下配置:
feign:
client:
config:
b:
loggerLevel: full
c:
loggerLevel: basic
调用结果,调用module-b呈现full级别日志,调用module-c呈现basic基本别日志:
四、feign支持的配置项
五、对象类参数的处理方式
例如对于这样的接口,feign该如何写呢?
public User getOne(User user);
如果是get请求,需要在参数前添加@SpringQueryMap,例如:
@FeignClient("user")
public interface UserFeignClient {
@GetMapping("/get_one")
public User getOne(@SpringQueryMap User user);
}
如果是get请求,需要在参数前添加@RequestBody,例如:
@FeignClient("user")
public interface UserFeignClient {
@PostMapping("/get_one")
public User getOne(@RequestBody User user);
}
六、使用连接池
方式一:httpclient
引入依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
添加配置
feign:
httpclient:
enabled: true
# feign的最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
方式二:okhttp
引入依赖
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
<version>11.8</version>
</dependency>
添加配置
feign:
httpclient:
# enabled: true
# feign的最大连接数
max-connections: 200
# feign单个路径的最大连接数
max-connections-per-route: 50
okhttp:
enabled: true
这部分代码放到了码云 study_badcat/spring-cloud-alibaba-study (gitee.com)
,v4.0分支