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

Spring Cloud是一个为开发者提供了快速构建分布式系统的工具集,其中非常重要的一部分就是OpenFeignOpenFeign是一个声明式、模板化的HTTP客户端,它可以让开发者更加方便的调用HTTP接口。下面我们来详细了解一下Spring Cloud整合OpenFeign使用方式。 首先,我们需要在pom.xml文件中添加依赖,如下所示: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>{版本号}</version> </dependency> ``` 然后,我们需要在启动类上添加@EnableFeignClients注解,表示开启Feign客户端自动配置。同时,我们还需要通过@FeignClient注解来定义接口。例如: ``` @FeignClient(name = "user-service") public interface UserFeignClient { @GetMapping("/user/findById") User findById(@RequestParam("id") Long id); } ``` 在上面的代码中,@FeignClient注解中的name属性表示调用的服务名,而接口中的findById方法就是定义的远程调用的接口。其中,@RequestParam注解表示使用@RequestParam方式传参。 最后,我们在业务代码中使用定义的接口即可。例如: ``` @RestController public class UserController { @Autowired private UserFeignClient userFeignClient; @GetMapping("/findUser") public User findUser(Long id) { return userFeignClient.findById(id); } } ``` 通过以上步骤,我们就可以方便地使用OpenFeign来调用HTTP接口,实现微服务之间的远程调用。整合OpenFeign有很多细节需要注意,例如如何处理调用异常、如何配置重试等等。但总体来说,Spring Cloud整合OpenFeign使用起来非常简单,是我们构建分布式系统的重要利器之一。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值