从零开始玩转SpringCloud(一):Eureka注册中心

Eureka


介绍:Eureka,古希腊词语,含义为我找到了,我发现了!相传阿基米德发现福利原理时说出了这个词。

Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来。Eureka包含了服务器端和客户端组件。服务器端,也被称作是服务注册中心,用于提供服务的注册与发现。Eureka支持高可用的配置,当集群中有分片出现故障时,Eureka就会转入自动保护模式,它允许分片故障期间继续提供服务的发现和注册,当故障分片恢复正常时,集群中其他分片会把他们的状态再次同步回来。客户端组件包含服务消费者与服务生产者。在应用程序运行时,Eureka客户端向注册中心注册自身提供的服务并周期性的发送心跳来更新它的服务租约。同时也可以从服务端查询当前注册的服务信息并把他们缓存到本地并周期性的刷新服务状态。

Eureka-Server服务搭建


  1. 引入依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. 在Application中使用@EnableEurekaServer
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
  1. 配置文件
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
spring:
application:
name: eureka-server # cAPP名称,在Eureka注册名称
profiles:
active: peer

eureka:
instance:
hostname: peer # 服务注册中心实例的主机名
client:
register-with-eureka: false # 是否注册自己
fetch-registry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
enableSelfPreservation: false #关闭自我保护机制

logging:
level:
com:
netflix:
eureka: info
discovery: info

server:
port: 8760

至此一个Eureka-Server就搭建好了。

Eureka-Server服务高可用

说到高可用,就是要保证一个节点挂掉,不会影响整个系统的运行。解决办法就是多部署几个实例,搭建集群,那么一个实例节点挂掉,其他实例仍可提供服务。

  1. 新建三个配置文件application-peer1.yml、application-peer2.yml、application-peer3,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
spring:
application:
name: eureka-server
profiles:
active: peer1

eureka:
instance:
hostname: peer1 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8762/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #关闭自我保护

logging:
level:
com:
netflix:
eureka: info
discovery: info

server:
port: 8761

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
spring:
application:
name: eureka-server
profiles:
active: peer2

eureka:
instance:
hostname: peer2 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8763/eureka
server:
enableSelfPreservation: false #关闭自我保护

logging:
level:
com:
netflix:
eureka: info
discovery: info

server:
port: 8762

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
spring:
application:
name: eureka-server
profiles:
active: peer3

eureka:
instance:
hostname: peer3 #服务注册中心实例的主机名
client:
serviceUrl: # 另外几个注册中心地址
defaultZone: http://localhost:8761/eureka, http://localhost:8762/eureka
server:
enableSelfPreservation: false #关闭自我保护

logging:
level:
com:
netflix:
eureka: info
discovery: info

server:
port: 8763
  1. idea下按配置文件启动多个项目,Active profiles指定启动配置文件。分别启动刚刚写好的三个配置文件即可。
    在这里插入图片描述

  2. 生产环境下是否需要动态配置注册中心,目前的配置对生产环境动态配置非常不友好

客户端配置

  1. 引入Eureka Client依赖
1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
  1. 在Application中使用@EnableEurekaServer
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.example.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaClient
@SpringBootApplication
public class EurekaClientApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaClientApplication.class, args);
}
}
  1. 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
spring:
application:
name: gateway # 在eureka-server注册名称

eureka:
client:
service-url:
defaultZone: http://localhost:8760/eureka/
# #高可用配置
# defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

server:
port: 8081
  1. 至此已EurekaClient已经搭建成功

在这里插入图片描述