Spring Cloud Alibaba创建服务消费者Feign(负载均衡)

[!NOTE]

本篇内容需要在上一篇的基础上进行。

请找到上一篇的项目代码,本篇在上一篇的源代码上进行编写。

本系列教程目录:https://laisc7301.github.io/blog/2024/01/29/202401290001SpringBoot%E7%B3%BB%E5%88%97%E5%9F%BA%E7%A1%80%E6%95%99%E7%A8%8B/

接着上一篇继续。

首先按照图示新建模块:

整个项目的文件结构如下图所示:

修改pom.xml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?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>
<groupId>org.laisc</groupId>
<artifactId>my-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>my-consumer-feign</artifactId>
<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 -->
<!-- Nacos 注册与发现 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- sentinel 熔断 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Nacos 分布式配置中心 -->
<!-- <dependency>-->
<!-- <groupId>com.alibaba.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
<!-- </dependency>-->
<!-- Spring Cloud End-->
</dependencies>

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

添加application.yml文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
spring:
application:
name: my-consumer-feign
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

server:
port: 9092

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

其它文件的源代码:

ConsumerFeignApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.laisc.consumer.feign;

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

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerFeignApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerFeignApplication.class, args);
System.out.println("ok!");
}
}

FeignService.java

1
2
3
4
5
6
7
8
9
10
11
package org.laisc.consumer.feign.service;

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

@FeignClient(value = "my-provider")
public interface FeignService {
@GetMapping(value = "/test/{message}")
String test(@PathVariable("message") String message);
}

ConsumerFeignController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package org.laisc.consumer.feign.conttroller;

import org.laisc.consumer.feign.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 ConsumerFeignController {

@Autowired
private FeignService FeignService;

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

启动ProviderApplicationConsumerFeignApplication

打开 http://localhost:9092/test/hi ,你会在浏览器上看到:

当前服务收到消息: Hi Feign, 该服务由端口: 8081提供。

测试负载均衡

右击服务中的ProviderApplication,复制配置,按照图示配置好。

依次启动ProviderApplicationConsumerFeignApplication和新添加的服务ProviderApplication (1)

在浏览器上多次访问 http://localhost:9092/test/hi ,浏览器交替显示:

当前服务收到消息: Hi Feign, 该服务由端口: 8081提供。

当前服务收到消息: Hi Feign, 该服务由端口: 8082提供。

项目源代码下载:https://pan.baidu.com/s/1-Ncvfk480DjVg-H2ULCtLg?pwd=mbuw

上一篇:Spring Cloud Alibaba创建服务消费者:https://laisc7301.github.io/blog/2024/01/27/202401270002SpringCloudAlibaba%E5%88%9B%E5%BB%BA%E6%9C%8D%E5%8A%A1%E6%B6%88%E8%B4%B9%E8%80%85/

下一篇:Spring Cloud Alibaba添加熔断机制Sentinel:https://laisc7301.github.io/blog/2024/01/29/202401290000SpringCloudAlibaba%E6%B7%BB%E5%8A%A0%E7%86%94%E6%96%AD%E6%9C%BA%E5%88%B6Sentinel/