目录

今日说码

点滴记录中国代码进程

X

Spring Cloud Alibaba 学习笔记(六):创建服务消费者(Feign)

目录:Spring Cloud Alibaba学习笔记(总) - 今日说码 (96xl.top)

上一篇:Spring Cloud Alibaba学习笔记(五):创建服务消费者 - 今日说码 (96xl.top)

下一篇:Spring Cloud Alibaba学习笔记(七):熔断(Sentinel) - 今日说码 (96xl.top)


概述

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

创建子模块

创建一个名称为 spring-cloud-alibaba-nacos-consumer-feign的子模块,继承 spring-cloud-alibaba-dependencies模块,创建过程参考Spring Cloud Alibaba学习笔记(二):创建依赖管理项目 - 今日说码 (96xl.top)

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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.xl</groupId>
        <artifactId>spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0</version>
        <relativePath>../spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>

    <artifactId>spring-cloud-alibaba-nacos-consumer-feign</artifactId>
    <name>spring-cloud-alibaba-nacos-consumer-feign</name>
    <packaging>jar</packaging>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- Spring Boot Begin -->
        <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.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->

        <!-- Spring Cloud Begin -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.xl.consumer.ConsumerFeignApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

主要增加了 org.springframework.cloud:spring-cloud-starter-openfeign依赖。

创建代码目录

创建过程参考Spring Cloud Alibaba学习笔记(四):创建服务提供者 - 今日说码 (96xl.top)

创建启动类

创建过程参考Spring Cloud Alibaba学习笔记(四):创建服务提供者 - 今日说码 (96xl.top)

Application文件内容如下:

package com.xl.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignApplication extends SpringBootServletInitializer {

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

通过 @EnableFeignClients注解开启Feign功能。

创建配置文件

创建过程参考Spring Cloud Alibaba学习笔记(四):创建服务提供者 - 今日说码 (96xl.top)

yml文件内容如下:

spring:
  application:
    #当前的服务名称
    name: nacos-consumer-feign
  cloud:
    nacos:
      discovery:
        #Nacos Server启动监听的ip地址和端口
        server-addr: 127.0.0.1:8848

server:
  port: 9092

management:
  endpoints:
    web:
      exposure:
        include: "*"

创建Feign接口

在启动类同级目录下,创建 service文件夹,然后在该文件夹下创建 FeignService接口,代码如下:

package com.xl.consumer.service;

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

@FeignClient(value = "nacos-provider")
public interface FeignService {

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

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

创建controller

在启动类同级目录下,创建 controller文件夹,然后在该文件夹下创建 NacosConsumerController类,代码如下:

package com.xl.consumer.controller;

import com.xl.consumer.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NacosConsumerFeignController {

    @Autowired
    private FeignService feignService;

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

项目结构

项目最终目录结构如下:

image.png

启动项目

右键启动类,选择Debug启动,等待出现 Started ConsumerFeignApplication in 7.839 seconds (JVM running for 9.479)启动成功的提示后,访问http://localhost:8848/nacos,打开Nacos Server管理界面,在服务管理-服务列表下出现了一个名称为 nacos-consumer-feign的服务,这个名称就是在yml中指定的名称。

image.png

访问项目

在浏览器中访问http://localhost:9092/test/hi,成功出现正确响应。

image.png

测试负载均衡

idea右上角选择编辑配置。

image.png

将服务提供者的启动类配置中,允许并行运行勾上,然后确定。

image.png

修改 spring-cloud-alibaba-nacos-provider子模块yml配置文件中的端口号,将8081改为8082。

image.png

再次右键 ProviderApplication选择Debug启动,注意由于我们在启动类配置中设置了允许并行运行,所以这里8082端口号启动的项目不会影响之前8081端口号启动的项目,两个项目会并行运行。等待出现 Started ProviderApplication in 7.296 seconds (JVM running for 8.545)启动成功的提示后,访问http://localhost:8848/nacos,打开Nacos Server管理界面,在服务管理-服务列表下发现 nacos-provider服务的实例数由1变为2,说明我们设置的配置生效了,多个服务提供者实例启动成功。

image.png

在浏览器中访问http://localhost:9092/test/hi,浏览器交替显示以下响应,其中端口号来自不同的服务提供者实例,说明负载均衡生效。

Hello Nacos Discovery Hi Feign i am from port 8081
Hello Nacos Discovery Hi Feign i am from port 8082

image.png

image.png


目录:Spring Cloud Alibaba 学习笔记(总) - 今日说码 (96xl.top)

上一篇:Spring Cloud Alibaba 学习笔记(五):创建服务消费者 - 今日说码 (96xl.top)

下一篇:Spring Cloud Alibaba 学习笔记(七):熔断(Sentinel) - 今日说码 (96xl.top)


标题:Spring Cloud Alibaba 学习笔记(六):创建服务消费者(Feign)
作者:96XL
地址:https://solo.96xl.top/articles/2022/02/10/1644495338897.html