[Spring Cloud Alibaba] 5.1 Start a Consumer Application - LoadBalancer

Combining the LoadBalanceClient and RestTemolate explicitly to access the RESTful service...

Posted by Mr.Vincent on 2021-05-27
Estimated Reading Time 7 Minutes
Words 1.2k In Total
Viewed Times

概述

服务消费者的创建与服务提供者大同小异,这里采用最原始的一种方式,即显示的使用 LoadBalanceClient 和 RestTemplate 结合的方式来访问。

案例

POM

创建一个工程名为 hello-spring-cloud-alibaba-nacos-consumer 的服务消费者项目,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
72
73
74
75
76
77
78
79
<?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.example</groupId>
<artifactId>hello-spring-cloud-alibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

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

<name>hello-spring-cloud-alibaba-nacos-consumer</name>
<version>${parent.version}</version>
<url>https://v-vincen.life</url>
<inceptionYear>2021-Now</inceptionYear>
<description>Demo project for Nacos Consumer</description>

<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>org.springframework.cloud</groupId>
<artifactId>spring-cloud-loadbalancer</artifactId>
</dependency>
<!-- Spring Cloud End -->

<!-- Spring Cloud Alibaba Begin -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- Spring Cloud Alibaba End -->

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

Application

1
2
3
4
5
6
7
8
9
10
/**
* @author vincent
*/
@SpringBootApplication
@EnableDiscoveryClient
public class HelloSpringCloudAlibabaNacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringCloudAlibabaNacosConsumerApplication.class, args);
}
}

Configuration

创建一个名为 NacosConsumerConfiguration 的 Java 配置类,主要作用是为了注入 RestTemplate。

1
2
3
4
5
6
7
8
9
10
/**
* @author vincent
*/
@Configuration
public class NacosConsumerConfiguration {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}

Controller

创建一个名为 NacosConsumerController 测试用的 Controller。

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
/**
* @author vincent
*/
@RestController
@Slf4j
public class NacosConsumerController {
@Autowired
private LoadBalancerClient loadBalancerClient;

@Autowired
private RestTemplate restTemplate;

@Value("${spring.application.name}")
private String appName;

@GetMapping("/echo/loadBalancerClient")
public String echoLoadBalancerClient() {
// Access through the combination of LoadBalanceClient and RestTemplate
ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider");
String path = String.format("http://%s:%s/echo/%s", serviceInstance.getHost(), serviceInstance.getPort(), appName);
log.info("request path: {}", path);
return restTemplate.getForObject(path, String.class);
}


@Autowired
private DiscoveryClient discoveryClient;

@GetMapping(value = "/echo/discoveryClient")
public String echoDiscoveryClient() {
ServiceInstance instance = discoveryClient.getInstances("nacos-provider").stream().findFirst().orElse(null);
if (Objects.isNull(instance)) {
return "not find nacos-provider";
}
String url = String.format("%s/echo/%s", instance.getUri(), appName);
return restTemplate.getForObject(url, String.class);
}
}

application.yml

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

server:
port: 9091

management:
endpoints:
web:
exposure:
include: '*'

启动工程

通过浏览器访问 http://localhost:8848/nacos,即 Nacos Server 网址。

nacos_consumer

你会发现多了一个名为 nacos-consumer 的服务,这时打开 http://localhost:9091/echo/loadBalancerClient 或者 http://localhost:9091/echo/discoveryClient ,你都会在浏览器上看到:

1
Hello Nacos Discovery nacos-consumer

服务的端点检查

通过浏览器访问 http://localhost:9091/actuator/nacosdiscovery 你会在浏览器上看到:

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
72
73
74
{
"subscribe": [
{
"name": "nacos-consumer",
"groupName": "DEFAULT_GROUP",
"clusters": "DEFAULT",
"cacheMillis": 1000,
"hosts": [
{
"instanceId": "192.168.0.138#9091#DEFAULT#DEFAULT_GROUP@@nacos-consumer",
"ip": "192.168.0.138",
"port": 9091,
"weight": 1.0,
"healthy": true,
"enabled": true,
"ephemeral": true,
"clusterName": "DEFAULT",
"serviceName": "DEFAULT_GROUP@@nacos-consumer",
"metadata": {
"preserved.register.source": "SPRING_CLOUD"
},
"ipDeleteTimeout": 30000,
"instanceHeartBeatInterval": 5000,
"instanceHeartBeatTimeOut": 15000
}
],
"lastRefTime": 0,
"checksum": "",
"allIPs": false,
"valid": true
}
],
"NacosDiscoveryProperties": {
"serverAddr": "127.0.0.1:8848",
"username": "",
"password": "",
"endpoint": "",
"namespace": "",
"watchDelay": 30000,
"logName": "",
"service": "nacos-consumer",
"weight": 1.0,
"clusterName": "DEFAULT",
"group": "DEFAULT_GROUP",
"namingLoadCacheAtStart": "false",
"metadata": {
"preserved.register.source": "SPRING_CLOUD"
},
"registerEnabled": true,
"ip": "192.168.0.138",
"networkInterface": "",
"port": 9091,
"secure": false,
"accessKey": "",
"secretKey": "",
"heartBeatInterval": null,
"heartBeatTimeout": null,
"ipDeleteTimeout": null,
"instanceEnabled": true,
"ephemeral": true,
"nacosProperties": {
"secretKey": "",
"namespace": "",
"username": "",
"namingLoadCacheAtStart": "false",
"serverAddr": "127.0.0.1:8848",
"com.alibaba.nacos.naming.log.filename": "",
"clusterName": "DEFAULT",
"password": "",
"accessKey": "",
"endpoint": ""
}
}
}

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !