OpenFeign 服务接口调用

Feigen是一个声明式web Service客户端。使用Feign能让编写Web Service客户端更加简单,它的使用方法式定义一个服务接口然后在上面添加注解。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

github官网地址:github.com/spring-cloud/spring-cloud-openfeign

Feign在消费端使用

OpenFeign使用步骤:

1. 建立cloud-consumer-feign-order80的module

 

 

2.改POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>ckfuture-springcloud</artifactId>
        <groupId>com.ckfuture.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-order80</artifactId>
    <dependencies>
        <!--openfeign-->
        <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>
        <!--引入自定定义的api通用包,可以使用Payment支付实体-->
        <dependency>
            <groupId>com.ckfuture.springcloud</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>

        <!--热部署devtools-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

3.建YML

server:
  port: 80
eureka:
  client:
    register-with-eureka: false
    service-url: 
      defautZone:http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/

4.主启动类

package com.ckfuture.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

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

5.业务接口

5.1 新建“PaymentFeignService”的接口

package com.ckfuture.springcloud.service;

import com.ckfuture.springcloud.entities.CommonResult;
import com.ckfuture.springcloud.entities.Payment;
import feign.Param;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PAYMENT-SERVICE")
public interface PaymentFeignService {

    //对应8001和8002 对外提供的Controller中的方法
    @GetMapping(value = "/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id);
}

 

 5.2 新建“OrderFeignController”控制类

package com.ckfuture.springcloud.controller;

import com.ckfuture.springcloud.entities.CommonResult;
import com.ckfuture.springcloud.entities.Payment;
import com.ckfuture.springcloud.service.PaymentFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderFeignController {
    @Resource
    private PaymentFeignService paymentFeignService;
    
    @GetMapping(value = "/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymetById(@PathVariable("id") Long id){
        return paymentFeignService.getPaymentById(id);
    }
}

6.测试

依次启动 7001、7002、8001、8002、80

 

posted @ 2021-10-27 21:14  创客未来  阅读(190)  评论(0编辑  收藏  举报