Skip to content

Commit 2509475

Browse files
committed
Spring Cloud Alibaba基础教程:Nacos下使用RestTemplate实现负载均衡消费
1 parent ede1816 commit 2509475

File tree

4 files changed

+124
-2
lines changed

4 files changed

+124
-2
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.didispace</groupId>
7+
<artifactId>alibaba-nacos-discovery-client-resttemplate</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<parent>
12+
<groupId>org.springframework.boot</groupId>
13+
<artifactId>spring-boot-starter-parent</artifactId>
14+
<version>2.0.5.RELEASE</version>
15+
<relativePath/> <!-- lookup parent from repository -->
16+
</parent>
17+
18+
<properties>
19+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
21+
<java.version>1.8</java.version>
22+
</properties>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.springframework.cloud</groupId>
31+
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.projectlombok</groupId>
36+
<artifactId>lombok</artifactId>
37+
<version>1.18.2</version>
38+
<optional>true</optional>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework.boot</groupId>
42+
<artifactId>spring-boot-starter-test</artifactId>
43+
<scope>test</scope>
44+
</dependency>
45+
</dependencies>
46+
47+
<dependencyManagement>
48+
<dependencies>
49+
<dependency>
50+
<groupId>org.springframework.cloud</groupId>
51+
<artifactId>spring-cloud-dependencies</artifactId>
52+
<version>Finchley.SR1</version>
53+
<type>pom</type>
54+
<scope>import</scope>
55+
</dependency>
56+
<dependency>
57+
<groupId>org.springframework.cloud</groupId>
58+
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
59+
<version>0.2.1.RELEASE</version>
60+
<type>pom</type>
61+
<scope>import</scope>
62+
</dependency>
63+
</dependencies>
64+
</dependencyManagement>
65+
66+
<build>
67+
<plugins>
68+
<plugin>
69+
<groupId>org.springframework.boot</groupId>
70+
<artifactId>spring-boot-maven-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.didispace.alibaba.nacos.discovery.client;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
8+
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.web.bind.annotation.GetMapping;
11+
import org.springframework.web.bind.annotation.RestController;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
15+
@EnableDiscoveryClient
16+
@SpringBootApplication
17+
public class TestApplication {
18+
19+
public static void main(String[] args) {
20+
SpringApplication.run(TestApplication.class, args);
21+
}
22+
23+
@Slf4j
24+
@RestController
25+
static class TestController {
26+
27+
@Autowired
28+
RestTemplate restTemplate;
29+
30+
@GetMapping("/test")
31+
public String test() {
32+
String result = restTemplate.getForObject("http://alibaba-nacos-discovery-server/hello?name=didi", String.class);
33+
return "Return : " + result;
34+
}
35+
}
36+
37+
@Bean
38+
@LoadBalanced
39+
public RestTemplate restTemplate() {
40+
return new RestTemplate();
41+
}
42+
43+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.application.name=alibaba-nacos-discovery-client-resttemplate
2+
server.port=9000
3+
4+
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848

4-Finchley/pom.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010

1111
<modules>
1212
<!-- spring cloud alibaba专题-->
13-
<module>alibaba-nacos-discovery-server</module> <!-- 使用Nacos实现服务注册与发现:服务提供方-->
14-
<module>alibaba-nacos-discovery-client-common</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(spring cloud common接口实现负载均衡)-->
13+
<module>alibaba-nacos-discovery-server</module> <!-- 使用Nacos实现服务注册与发现:服务提供方-->
14+
<module>alibaba-nacos-discovery-client-common</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(spring cloud common接口实现负载均衡)-->
15+
<module>alibaba-nacos-discovery-client-resttemplate</module> <!-- 使用Nacos实现服务注册与发现:服务消费方(RestTemplate实现)-->
1516

1617
<!--spring cloud stream专题-->
1718
<module>stream-consumer-self</module> <!-- 消费自己的消息 -->

0 commit comments

Comments
 (0)