四、Spring Cloud Alibaba 创建服务消费者(Feign)

Spring Cloud Alibaba 创建服务消费者(Feign)

Feign 是一个声明式的伪 Http 客户端,它使得写 Http 客户端变得更简单。使用 Feign,只需要创建一个接口并注解。它具有可插拔的注解特性,可使用 Feign 注解和 JAX-RS 注解。Feign 支持可插拔的编码器和解码器。Feign 默认集成了 Ribbon,Nacos 也很好的兼容了 Feign,默认实现了负载均衡的效果

修改项目

  1. 我们直接修改``idreamyou4cloud-auth模块,在pom.xml中添加feign`的依赖
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

整体文件如下:

<?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>idreamyou4cloud</artifactId>
        <groupId>cn.idreamyou</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>idreamyou4cloud-auth</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--web 模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--注册中心客户端-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  1. 在启动类中添加@EnableFeignClients注解开启Feign功能,整体如下
package cn.idreamyou.idreamyou4cloud.auth;

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

/**
 * 认证授权中心
 *
 * @author juning
 * @date 2020/9/14
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class AuthApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

}
  1. 创建Feign接口

通过 @FeignClient("服务名") 注解来指定调用哪个服务。代码如下:

package cn.idreamyou.idreamyou4cloud.auth.server.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * feign客户端
 *
 * @author juning
 * @date 2020/9/14
 */
@FeignClient(value = "idreamyou4cloud-upms")
public interface FeignService {

    @GetMapping(value = "/test/{message}")
    String test(@PathVariable("message") String message);

}
  1. 编写控制器controller
package cn.idreamyou.idreamyou4cloud.auth.controller;

import cn.idreamyou.idreamyou4cloud.auth.server.feign.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * 服务消费者(Feign)
 *
 * @author juning
 * @date 2020/9/14
 */
public class NacosConsumerFeignController {

    @Autowired
    private FeignService feignService;

    @GetMapping(value = "/test/hello")
    public String test() {
        return feignService.test("Hi Feign");
    }

}

启动项目

浏览器访问http://localhost:8082/test/hello

image.png

测试负载均衡

  1. 设置idea可以启动多个实例

image.png

  1. idreamyou4cloud-upms模块的端口改为8083,并启动

image.png

  1. 浏览器访问http://localhost:8848/nacos,可以发现idreamyou4cloud-upms已经启动了两个实例:

image.png

  1. 浏览器多次访问http://localhost:8082/test/hello,可以看到浏览器会交替显示:
Hello Nacos Discovery Hi Feign i am from port 8083
Hello Nacos Discovery Hi Feign i am from port 8081

image.png

image.png

负载均衡已生效

上一篇 下一篇