什么是openFeign?
openfeign在springcloud中的角色是服务调用,没有openfeign的时候呢,我们用RestTemplate调用服务,但是在实际开发中,对服务的调用不止一个地方,所以openfeign在此基础上又做了一次封装。openfeign集成了ribbon和RestTemplate,我们只需要创建接口加注解就可以实现跨服务调用,符合我们程序员的对接口编程的习惯。
openFeign实现
1.创建FeignClient接口 ,加注解FeignClient,value为服务提供者名称。
import com.xx.job.common.CommonResult;
import com.xx.job.entity.Payment;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "SPRING-CLOUD-PAYMENT")
public interface PaymentService {
@GetMapping("/payment/selectById/{id}")
public CommonResult selectById(@PathVariable("id") Long id);
@GetMapping("/payment/insert")
public CommonResult insert( Payment payment);
@GetMapping("/payment/timeOut")
public CommonResult timeOut();
}
2.controller层注入openFeign接口
import com.xx.job.common.CommonResult;
import com.xx.job.entity.Payment;
import com.xx.job.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
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;
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
@Autowired
private PaymentService paymentService;
@GetMapping("/selectById/{id}")
public CommonResult selectById(@PathVariable Long id){
return paymentService.selectById(id);
}
@GetMapping("/insert")
public CommonResult insert( Payment payment){
return paymentService.insert(payment);
}
@GetMapping("/timeOut")
public CommonResult timeOut(){
return paymentService.timeOut();
}
}
3.启动类开启openFeign
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class OrderOpenfeignConsumer80Application {
public static void main(String[] args) {
SpringApplication.run(OrderOpenfeignConsumer80Application.class, args);
}
}
4.测试通过。
openFeign的超时控制 ,默认值是1秒pom配置
server:
port: 82
spring:
application:
name: order-openfeign-consumer80
eureka:
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
feign:
client:
config:
default:
#建立连接所用的时间,适用于网络状况正常的情况下,两端连接所需要的时间
ConnectTimeOut: 5000
#指建立连接后从服务端读取到可用资源所用的时间
ReadTimeOut: 10000
可以自己写一个sleep方法测试。配置ConnectTimeOut(连接最长时间)和ReadTimeOut(服务端读取最长时间)
openFeign日志增强
1.容器中注入Logger。
import feign.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OpenFeignConfig {
@Bean
Logger.Level feingLoggerLevel(){
return Logger.Level.FULL;
}
}
2.配置新加,是哪个接口就写那个。我的是com.xx.job.service.PaymentService
logging:
level:
com.xx.job.service.PaymentService: debug
git地址,spring-cloud-demo: spring-cloud-demo试例openfeign项目order-openfeign-consumer80