Skip to content

Commit 05b978e

Browse files
author
YunaiV
committed
增加 Eureka 示例
1 parent 38c3e8c commit 05b978e

File tree

15 files changed

+414
-1
lines changed

15 files changed

+414
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@
186186
## API 网关
187187

188188
* [《芋道 Spring Cloud 服务网关 Spring Cloud Gateway 入门》](http://www.iocoder.cn/Spring-Cloud/Spring-Cloud-Gateway/?github) 对应 [labx-08](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-08)
189-
* [《芋道 Spring Cloud Netflix 服务网关 Zuul 入门》](http://www.iocoder.cn/Spring-Cloud/Zuul/?github) 对应 对应 [labx-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-21)
189+
* [《芋道 Spring Cloud Netflix 服务网关 Zuul 入门》](http://www.iocoder.cn/Spring-Cloud/Netflix-Zuul/?github) 对应 对应 [labx-21](https://github.com/YunaiV/SpringBoot-Labs/tree/master/labx-21)
190190
* [《性能测试 —— Spring Cloud Gateway、Zuul 基准测试》](http://www.iocoder.cn/Performance-Testing/SpringCloudGateway-Zuul-benchmark/?github) 对应 [lab-07](https://github.com/YunaiV/SpringBoot-Labs/tree/master/lab-07)
191191

192192
> 如下非 Spring Cloud 网关,先放在这里...
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-demo01-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
10+
fetch-registry: true
11+
service-url:
12+
defaultZone: http://127.0.0.1:8761/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-demo01-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
11+
fetch-registry: true
12+
service-url:
13+
defaultZone: http://127.0.0.1:8761/eureka/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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-server-cluster</artifactId>
13+
14+
<!--
15+
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
16+
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
17+
-->
18+
<dependencyManagement>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-parent</artifactId>
23+
<version>${spring.boot.version}</version>
24+
<type>pom</type>
25+
<scope>import</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>org.springframework.cloud</groupId>
29+
<artifactId>spring-cloud-dependencies</artifactId>
30+
<version>${spring.cloud.version}</version>
31+
<type>pom</type>
32+
<scope>import</scope>
33+
</dependency>
34+
</dependencies>
35+
</dependencyManagement>
36+
37+
<dependencies>
38+
<!-- 引入 Spring Cloud Netflix Eureka Server 相关依赖,将 Eureka 作为注册中心的服务器,并实现对其的自动配置 -->
39+
<dependency>
40+
<groupId>org.springframework.cloud</groupId>
41+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
42+
</dependency>
43+
</dependencies>
44+
45+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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-server-standalone</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+
<!-- 引入 Spring Cloud Netflix Eureka Server 相关依赖,将 Eureka 作为注册中心的服务器,并实现对其的自动配置 -->
44+
<dependency>
45+
<groupId>org.springframework.cloud</groupId>
46+
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
47+
</dependency>
48+
</dependencies>
49+
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloudalibaba.labx22.eurekaserverdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
@SpringBootApplication
8+
@EnableEurekaServer
9+
public class EurekaServerNode01Application {
10+
11+
public static void main(String[] args) {
12+
System.setProperty("spring.profiles.active", "node01");
13+
SpringApplication.run(EurekaServerNode01Application.class,args);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloudalibaba.labx22.eurekaserverdemo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
6+
7+
@SpringBootApplication
8+
@EnableEurekaServer
9+
public class EurekaServerNode02Application {
10+
11+
public static void main(String[] args) {
12+
System.setProperty("spring.profiles.active", "node02");
13+
SpringApplication.run(EurekaServerNode02Application.class,args);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
server:
2+
port: 18761
3+
4+
spring:
5+
application:
6+
name: eureka-server
7+
8+
eureka:
9+
instance:
10+
hostname: eureka-node01
11+
client:
12+
register-with-eureka: true
13+
fetch-registry: true
14+
service-url:
15+
defaultZone: http://eureka-node02:28761/eureka/

0 commit comments

Comments
 (0)