Skip to content

Commit 7e0ecbb

Browse files
author
YunaiV
committed
增加 Eureka 示例
1 parent 6de512a commit 7e0ecbb

File tree

9 files changed

+241
-4
lines changed

9 files changed

+241
-4
lines changed

labx-22/labx-22-scn-eureka-demo01-provider/src/main/resources/application.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ server:
77

88
eureka:
99
client:
10-
register-with-eureka: true
11-
fetch-registry: true
10+
register-with-eureka: true # 注册到 Eureka-Server,默认为 true
11+
fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true
1212
service-url:
13-
defaultZone: http://127.0.0.1:8761/eureka/
13+
defaultZone: http://127.0.0.1:8761/eureka/ # Eureka-Server 地址
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-22</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-22-scn-eureka-demo02-consumer</artifactId>
13+
14+
<properties>
15+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
16+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
17+
</properties>
18+
19+
<!--
20+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
21+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
22+
-->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>${spring.boot.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-dependencies</artifactId>
35+
<version>${spring.cloud.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-web</artifactId>
47+
</dependency>
48+
49+
<!-- 引入 Spring Cloud Netflix Eureka Client 相关依赖,将 Eureka 作为注册中心的客户端,并实现对其的自动配置 -->
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cn.iocoder.springcloudalibaba.labx22.consumerdemo;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
import org.springframework.cloud.client.discovery.DiscoveryClient;
8+
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.web.bind.annotation.GetMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.client.RestTemplate;
14+
15+
import java.util.List;
16+
17+
@SpringBootApplication
18+
// @EnableDiscoveryClient
19+
public class DemoConsumerApplication {
20+
21+
public static void main(String[] args) {
22+
SpringApplication.run(DemoConsumerApplication.class, args);
23+
}
24+
25+
@Configuration
26+
public class RestTemplateConfiguration {
27+
28+
@Bean
29+
public RestTemplate restTemplate() {
30+
return new RestTemplate();
31+
}
32+
33+
}
34+
35+
@RestController
36+
static class TestController {
37+
38+
@Autowired
39+
private DiscoveryClient discoveryClient;
40+
@Autowired
41+
private RestTemplate restTemplate;
42+
@Autowired
43+
private LoadBalancerClient loadBalancerClient;
44+
45+
@GetMapping("/hello")
46+
public String hello(String name) {
47+
// 获得服务 `demo-provider` 的一个实例
48+
ServiceInstance instance;
49+
if (true) {
50+
// 获取服务 `demo-provider` 对应的实例列表
51+
List<ServiceInstance> instances = discoveryClient.getInstances("demo-provider");
52+
// 选择第一个
53+
instance = instances.size() > 0 ? instances.get(0) : null;
54+
} else {
55+
instance = loadBalancerClient.choose("demo-provider");
56+
}
57+
// 发起调用
58+
if (instance == null) {
59+
throw new IllegalStateException("获取不到实例");
60+
}
61+
String targetUrl = instance.getUri() + "/echo?name=" + name;
62+
String response = restTemplate.getForObject(targetUrl, String.class);
63+
// 返回结果
64+
return "consumer:" + response;
65+
}
66+
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
spring:
2+
application:
3+
name: demo-consumer # Spring 应用名
4+
server:
5+
port: 28080 # 服务器端口。默认为 8080
6+
7+
eureka:
8+
client:
9+
register-with-eureka: true # 注册到 Eureka-Server,默认为 true
10+
fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true
11+
service-url:
12+
defaultZone: http://eureka-node01:18761/eureka/, http://eureka-node02:28761/eureka/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>labx-22</artifactId>
7+
<groupId>cn.iocoder.springboot.labs</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>labx-22-scn-eureka-demo02-provider</artifactId>
13+
14+
<properties>
15+
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
16+
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
17+
</properties>
18+
19+
<!--
20+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
21+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
22+
-->
23+
<dependencyManagement>
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-parent</artifactId>
28+
<version>${spring.boot.version}</version>
29+
<type>pom</type>
30+
<scope>import</scope>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.cloud</groupId>
34+
<artifactId>spring-cloud-dependencies</artifactId>
35+
<version>${spring.cloud.version}</version>
36+
<type>pom</type>
37+
<scope>import</scope>
38+
</dependency>
39+
</dependencies>
40+
</dependencyManagement>
41+
42+
<dependencies>
43+
<!-- 引入 SpringMVC 相关依赖,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-web</artifactId>
47+
</dependency>
48+
49+
<!-- 引入 Spring Cloud Netflix Eureka Client 相关依赖,将 Eureka 作为注册中心的客户端,并实现对其的自动配置 -->
50+
<dependency>
51+
<groupId>org.springframework.cloud</groupId>
52+
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cn.iocoder.springcloudalibaba.labx22.providerdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@SpringBootApplication
10+
@EnableDiscoveryClient
11+
public class DemoProviderApplication {
12+
13+
public static void main(String[] args) {
14+
SpringApplication.run(DemoProviderApplication.class, args);
15+
}
16+
17+
@RestController
18+
static class TestController {
19+
20+
@GetMapping("/echo")
21+
public String echo(String name) {
22+
return "provider:" + name;
23+
}
24+
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
spring:
2+
application:
3+
name: demo-provider # Spring 应用名
4+
5+
server:
6+
port: 18080 # 服务器端口。默认为 8080
7+
8+
eureka:
9+
client:
10+
register-with-eureka: true # 注册到 Eureka-Server,默认为 true
11+
fetch-registry: true # 从 Eureka-Server 获取注册表,默认为 true
12+
service-url:
13+
defaultZone: http://eureka-node01:18761/eureka/, http://eureka-node02:28761/eureka/

labx-22/labx-22-scn-eureka-server-cluster/src/main/resources/application-node02.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ eureka:
1212
register-with-eureka: true
1313
fetch-registry: true
1414
service-url:
15-
defaultZone: http://eureka-node01:28761/eureka/
15+
defaultZone: http://eureka-node01:18761/eureka/

labx-22/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
<module>labx-22-scn-eureka-server-standalone</module>
1616
<module>labx-22-scn-eureka-demo01-provider</module>
1717
<module>labx-22-scn-eureka-demo01-consumer</module>
18+
1819
<module>labx-22-scn-eureka-server-cluster</module>
20+
<module>labx-22-scn-eureka-demo02-provider</module>
21+
<module>labx-22-scn-eureka-demo02-consumer</module>
22+
1923
</modules>
2024

2125

0 commit comments

Comments
 (0)