spring_cloud_eureka实践

创建eureka-server

1.官网生成

https://start.spring.io/

添加依赖 eureka-server

2.使用idea的spring initializr

添加依赖eureka-server

生成后的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
80
81
82
83
84
85
86
87
88
<?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>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.eureka.EurekaApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>

添加@EnableEurekaServer

1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}

}

这时启动,发现会一直报错

1
2
 [nfoReplicator-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_UNKNOWN/192.168.1.106: registering service...
2022-06-06 19:09:50.806 INFO 3394 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=java.net.ConnectException: Connection refused (Connection refused) stacktrace=com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)

访问http://localhost:8080/是可以看到eureka-server的页面的。

这是因为默认配置是使用8761端口,这个服务也向eureka-server注册。

1
2
3
4
5
6
7
8
9
10
11
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

Eureka instance的配置对应EurekaInstanceConfigBean

eureka client的配置对应EurekaClientConfigBean

registerWithEureka 默认为ture,意思是不注册到eureka

fetchRegistry 默认为ture,意思是不获取服务注册表

再启动后,不会报错,访问页面,发现Instances currently registered with Eureka 什么都没有。

创建eureka-client

同server创建方式,不添加server了,添加client和spring web

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?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>
<groupId>com.example</groupId>
<artifactId>service-a</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>service-a</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.service_a.ServiceAApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
1
2
3
4
# 应用名称
spring:
application:
name: service-a

启动后,访问http://localhost:8761/,发现 service-a的服务实例。

添加测试controller

1
2
3
4
5
6
7
8
9
10
11
12
# 应用名称
spring:
application:
name: service-a

server:
port: 8762

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
@RestController
public class TestController {

@Value("${server.port}")
String port;

@RequestMapping("/test")
public String test() {
return "i am from port:" + port;
}

}

访问http://localhost:8762/test

使用密码访问eureka

Eureka-server添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// 关闭csrf
// http.csrf().disable();
/*
* 默认情况下添加SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
*/
http.csrf().ignoringAntMatchers("/eureka/**");
// 开启认证支持HttpBasic
http.authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 应用名称
spring:
application:
name: eureka

security:
user:
name: root
password: root

server:
port: 8761

eureka:
instance:
hostname: localhost
client:
#设置服务注册中心的URL
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:${server.port}/eureka/

这时访问http://localhost:8761/,需要用户名密码

高可用,2个节点

创建application-8762.yml、application-8763.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 应用名称
spring:
application:
name: eureka

security:
user:
name: root
password: root

server:
port: 8762

eureka:
instance:
hostname: localhost
client:
#设置服务注册中心的URL
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:8763/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 应用名称
spring:
application:
name: eureka

security:
user:
name: root
password: root

server:
port: 8763

eureka:
instance:
hostname: localhost
client:
#设置服务注册中心的URL
service-url:
defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@${eureka.instance.hostname}:8762/eureka/

互相注册,8762写8763的地址,8763写8762的地址。

在idea中运行配置中,添加修改选项 vm选项, 填入 -Dspring.profiles.active=8763

修改选项 选择允许多个实例。

访问http://localhost:8762/ http://localhost:8763/

发现有对方的实例。

高可用,3节点

创建application-8764.yml、application-8765.yml、application-8766.yml

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
# 应用名称
spring:
application:
name: eureka

security:
user:
name: root
password: root

server:
port: 8764

eureka:
instance:
hostname: localhost
server:
# 设置 eureka server同步失败的等待时间 默认 5分 , 在这期间,它不向客户端提供服务注册信息
wait-time-in-ms-when-sync-empty: 0
client:
#设置服务注册中心的URL
service-url:
defaultZone: "http://root:root@${eureka.instance.hostname}:8764/eureka/,\
http://root:root@${eureka.instance.hostname}:8765/eureka/,\
http://root:root@${eureka.instance.hostname}:8766/eureka/"

访问 http://localhost:8764/ http://localhost:8765/ http://localhost:8766/

删除无效服务

1.查看spring_cloud_eureka的管理页面

http://用户名:密码@ip:port

2.删除服务节点

Request Method = DELETE
http://用户名:密码@ip:port/eureka/apps/服务名/注册名

上线与下线

1.强制下线

可以通过调用stateUpdate接口,更改实例的状态为OUT_OF_SERVICE 。

调用接口:/eureka/apps/appID/instanceID/status?value=OUT_OF_SERVICE

调用方式:PUT

2.手动上线

请求接口:/eureka/apps/appID/instanceID/status?value=UP

调用方式:PUT


spring_cloud_eureka实践
http://hanqichuan.com/2022/06/06/spring_cloud/spring_cloud_eureka实践/
作者
韩启川
发布于
2022年6月6日
许可协议