Eureka Server 配置

引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

YML配置

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

eureka:
instance:
#eureka服务端的实例名称(这里为了方便测试,修改了host文件,添加了 127.0.0.1 eureka7001.com映射)
hostname: eureka7001.com
client:
#false表示不向服务注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false

主启动类加入@EnableEurekaServer注解

1
2
3
4
5
6
7
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}

启动服务

image-20200804110913521


Eureka Client 配置

引入依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

YML配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 8001

spring:
application:
name: cloud-payment-service

eureka:
client:
#表示是否讲自己注册进EurekaService默认为true
register-with-eureka: true
#是否从EurekaServer抓取自己有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: payment8001
prefer-ip-address: true

主启动类加入@EnableEurekaClient注解

1
2
3
4
5
6
7
8
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}

查看 Eureka Dashboard

image-20200804111630870


Eureka Server 集群配置

YML配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 7001

eureka:
instance:
hostname: eureka7001.com #eureka服务端的实例名称
client:
#false表示不向服务注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7002.com:7002/eureka/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
server:
port: 7002

eureka:
instance:
hostname: eureka7002.com #eureka服务端的实例名称
client:
#false表示不向服务注册中心注册自己
register-with-eureka: false
#false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://eureka7001.com:7001/eureka/

Eureka Server 集群配置的时候需要注意,服务器1的 defaultZone 要配置成服务器2的,同样服务器2的 defaultZone 也要配置成1的地址信息,Eureka 服务端才能进行交互,如果2台以上的集群,每一个都要与其他的服务器进行配置,服务器1的要同时与其他所有的服务器进行交互,依次类推

查看 Eureka Dashboard

image-20200804112841045

image-20200804112854706


Eureka Client 集群配置

YML配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 8001

spring:
application:
name: cloud-payment-service

eureka:
client:
#表示是否讲自己注册进EurekaService默认为true
register-with-eureka: true
#是否从EurekaServer抓取自己有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
server:
port: 8002

spring:
application:
name: cloud-payment-service

eureka:
client:
#表示是否讲自己注册进EurekaService默认为true
register-with-eureka: true
#是否从EurekaServer抓取自己有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
instance:
instance-id: payment8002
prefer-ip-address: true

在同一个客户端集群里面的所有客户端,都要有相同的spring.application.name,同时,如果服务端也是集群配置的话,eureka.service-url.defaultZone需要把所有的服务端集群节点的信息都要配置上

查看 Eureka Dashboard

image-20200804113746084