Skip to content

Commit ff9eca4

Browse files
author
YunaiV
committedMay 8, 2020
增加 Eureka 示例
1 parent 7e0ecbb commit ff9eca4

File tree

11 files changed

+340
-0
lines changed

11 files changed

+340
-0
lines changed
 
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-demo03-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://eureka:woshimima@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-demo03-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:woshimima@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-server-security</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+
49+
<!-- 实现对 Spring Security 的自动化配置 -->
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-security</artifactId>
53+
</dependency>
54+
</dependencies>
55+
56+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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 EurekaServerApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(EurekaServerApplication.class,args);
13+
}
14+
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package cn.iocoder.springcloudalibaba.labx22.eurekaserverdemo.config;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
5+
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
6+
7+
@Configuration
8+
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
9+
10+
@Override
11+
protected void configure(HttpSecurity http) throws Exception {
12+
http.csrf().ignoringAntMatchers("/eureka/**");
13+
super.configure(http);
14+
}
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
server:
2+
port: 8761 # 设置 Eureka-Server 的端口
3+
4+
spring:
5+
application:
6+
name: eureka-server
7+
8+
# 使用 Spring Security 创建默认认证账号
9+
security:
10+
user:
11+
name: eureka
12+
password: woshimima
13+
14+
eureka:
15+
client:
16+
register-with-eureka: false # 不注册到 Eureka-Server,默认为 true
17+
fetch-registry: false # 不从 Eureka-Server 获取注册表,默认为 true

‎labx-22/pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<module>labx-22-scn-eureka-demo02-provider</module>
2121
<module>labx-22-scn-eureka-demo02-consumer</module>
2222

23+
<module>labx-22-scn-eureka-server-security</module>
24+
<module>labx-22-scn-eureka-demo03-provider</module>
25+
<module>labx-22-scn-eureka-demo03-consumer</module>
2326
</modules>
2427

2528

0 commit comments

Comments
 (0)
Please sign in to comment.