配置如下
- 日志级别
- 契约
- 拦截器
- 超时时间
导入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
编写feign接口
package com.guigu.cloud.feign;
import com.guigu.cloud.pojo.TbUser;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
//eureka服务名
@FeignClient(name = "user-provider")
public interface UserFeign {
//服务提供者的请求地址
@GetMapping("user/find/{id}")
TbUser findById(@PathVariable Integer id);
}
编写启动类
package com.guigu.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.cloud.openfeign.EnableFeignClients;
//开启feign的自动配置类
@EnableFeignClients/*(basePackages = {指定扫描feign包名
"com.guigu.cloud.feign",
"com.guigu.cloud.xxx"
})*/
/*(clients = {如果多个feign接口,指定生成代理
UserFeign.class
})*/
//排除数据库的自动配置类
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class UserConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(UserConsumerFeignApplication.class,args);
}
}
日志级别配置:
打印日志
注入Bean
/*日志级别配置*/
@Bean
public Logger.Level level(){
/*
* No logging.
*/
//NONE,
/*
* 只记录请求方法和URL、响应状态代码和执行时间。
* Log only the request method and URL and the response status code and execution time.
*/
//BASIC,
/*
* 记录基本信息以及请求和响应头。
* Log the basic information along with request and response headers.
*/
//HEADERS,
/*
*记录请求和响应的头、正文和元数据。
* Log the headers, body, and metadata for both requests and responses.
*/
//FULL
return Logger.Level.BASIC;
}
application.xml文件配置
logging:
level:
# 包名路径
com.guigu.cloud: debug
日志结果
2022-01-15 18:43:54.423 DEBUG 30056 --- [nio-4317-exec-1] com.guigu.cloud.feign.UserFeign : [UserFeign#findById] ---> GET http://user-provider/user/find/1 HTTP/1.1
2022-01-15 18:43:54.521 DEBUG 30056 --- [nio-4317-exec-1] com.guigu.cloud.feign.UserFeign : [UserFeign#findById] <--- HTTP/1.1 200 (96ms)
契约配置:
因为feign默认使用的是springBoot的http的connection,切换为feign自带的http
注入Bean
/*契约*/
@Bean
public static Contract contract() {
return new Contract.Default();
}
编写feign接口
package com.guigu.cloud.feign;
import com.guigu.cloud.pojo.TbUser;
import feign.Param;
import feign.RequestLine;
import org.springframework.cloud.openfeign.FeignClient;
//eureka服务名
@FeignClient(name = "user-provider")
public interface UserFeign {
//请求地址
@RequestLine(value = "GET /user/find/{id}")
//服务提供者的controlelr的api
TbUser findById(@Param("id") Integer id);
}
拦截器配置:
微服务之间的调用,通过拦截器,对微服务请求添加header信息
实现feign.RequestInterceptor接口,添加header信息
package com.guigu.cloud.interceptor;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import java.util.UUID;
public class FeignInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
String s = UUID.randomUUID().toString();
template.header("tonken",s);
}
}
获取header参数注解@RequestHeader
public TbUser findById(@PathVariable Integer id, @RequestHeader String token){
log.info("token:"+token)
}
超时时间
设置对微服务调用的响应时间
注入Bean
/*超时时间*/
@Bean
public static Request.Options options(){
return new Request.Options(3000,1000);
}
对服务提供者的微服务,return前使用线程休眠
Thread.sleep(4000);
结果如下: