OpenFeign的使用

文章目录

OpenFeign是什么?

Feign是一个声明式的Web Service客户端。它的出现使开发Web Service客户端变得很简单。使用Feign只需要创建一个接口加上对应的注解,比如:FeignClient注解。
OpenFeign貌似只能调用提供者的controller,即写一个提供者controller的接口,消费者模块来调用这个接口,就还是相当于提供者的controller,和restTemplate没有本质区别

OpenFeign可以做什么?

OpenFeign主要是用来使java Http客户端变得更容易。

怎么使用OpenFeign?

1.新建一个消费者模块,因为OpenFeign自带负载均衡,所以不用手动配置。
2.引入依赖

<dependencies>
    <!-- Open Feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
    <!-- eureka Client -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <groupId>com.krisswen.cloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>
	3.编写配置文件
server:
  port: 80
spring:
  application:
    name: cloud-consumer-feign-service
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4.编写启动类

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class CloudFeignConsumerApplication80 {
    public static void main(String[] args) {
        SpringApplication.run(CloudFeignConsumerApplication80.class,args);
    }
}

5.编写controller

@RestController
@SuppressWarnings(value = "all")
public class CustomerFeignController {

    @Resource
    PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/feign/payment/{id}")
    public CommonResult<Payment> getPaymentId(@PathVariable("id") Integer id){
        return paymentFeignService.getPaymentId(id);
    }

    @GetMapping("/consumer/payment/feign/timeout")
    public String timeOut(){
        String s = paymentFeignService.timeOut();
        return s;
    }
}

6.编写service

@Component
@FeignClient("cloud-service-payment")  //服务提供者
public interface PaymentFeignService {
    @GetMapping("/findById/{id}")   //这里的路径是调用服务提供者controller里的访问路径
    CommonResult<Payment> getPaymentId(@PathVariable("id") Integer id);


    @GetMapping("/payment/feign/timeout")
    String timeOut();
}

最后目录结构是这样

OpenFeign的使用
然后启动我们的两台Eureka,和两台服务提供方
然后启动新建的feign模块,
访问:localhost:/consumer/feign/payment/1即可

上一篇:Spring Cloud OpenFeign 工作原理解析


下一篇:飞天加速计划初体验