springcloud之openFeign使用

什么是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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值