使用openfeign
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、服务调用
示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。
1.引入库
代码如下(示例):
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.主启动
代码如下(示例):
@EnableFeignClients
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
3.消费端调用服务端
只需要@FeignClient这个注解,value写你指定调用的服务名,然后消费端的controller调用此接口的方法就好啦.
@Component
@FeignClient(value = "consumer-paymnet-8888")
public interface PaymentService {
/**
* 服务端的url
* @return
*/
@GetMapping("/a1")
String a1();
}
二、显示详细调用日志
1.加入logger配置
一定要是feign.Logger这个包,不要导错包
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author: GuoQi
* @create: 2021-04-17
* @description: feign
*/
@Configuration
public class FeignLogConfig {
@Bean
Logger.Level feignLoggerLevel(){
return Logger.Level.FULL;
}
}
2.配置application.yml
你的service的路径
logging:
level:
com.demo.service: debug
3.打印出来的效果
三、解决服务超时问题
1.只需在application.yml添加配置即可
# 设置ribbon超时时间
ribbon:
ReadTimeout: 5000
ConnectTimeout: 5000
总结
在主启动类上加@EnableFeignClients,调用服务的时候在接口上面加@FeignClient(value = “你的服务名”).