SpringCloud-Eureka的使用
Eureka 的使用
单机版 Eureka 注册中心
注册中心
新建一个 spring-cloud-eureka 项目,用作注册中心
引入依赖
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<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<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>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>配置属性
1
2
3spring-cloud-eureka =
8090 =
http://localhost:8090/eureka =自己注册自己
启动 eureka-server,在 application 中增加注解@EnableEurekaServer
启动服务,由于自己注册自己的服务,在启动的时候会报错,可以忽略,也可以在配置文件中增加
1
2false =
false =
服务提供者
增加依赖
1
2
3
4<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>启动 eureka-client,在 application 中增加注解@EnableEurekaClient
增加配置文件
1
2
3order-service =
8188 =
http://localhost:8090/eureka =访问注册中心 http://localhost:8090,
Application AMIs Availability Zones Status ORDER-SERVICE n/a (2) (2) UP (2) - 10.64.12.118:order-service:8188 , 10.64.12.118:order-service:8999
高可用版注册中心
Eureka 的高可用是启动多个 eureka 服务节点,每个节点之间相互注册服务,互为主备,一旦某个节点挂掉之后,其他节点照常提供服务。
创建 2 个 springboot 项目
添加依赖
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<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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>修改配置文件
配置文件 1
1
2
3
4
5
6spring-cloud-eureka2 =
8091 =
http://localhost:8090/eureka =
false =
false =配置文件 2
1
2
3
4
5spring-cloud-eureka =
8090 =
http://localhost:8091/eureka =
false =
false =
2 个配置文件中的注册中心地址,分别为交换地址,如果存在 2 个以上的注册中心,可以用逗号继续添加
启动 eureka-server,在 appplication 中添加@EnableEurekaServer 注解
分别访问 http://localhost:8091/,http://localhost:8090/,对应服务都注册到上面了。
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 zeofuns!