Eureka集群搭建

1.搭建2个Eureka服务

eureka7001,eureka7002

1.pom文件

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

2.yml文件

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/ #集群版
#设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版
#server:
#enable-self-preservation: false #不启用默认的保护机制, #eviction-interval-timer-in-ms: 2000 #设置默认世间为2秒


7002和7001类似

改hosts文件
 eureka7001、eureka7002 映射到 127.0.0.1

3.启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}

4.页面展示



2.我们再来写支付服务

1.pom文件

<!--eureka client-->
<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-actuator</artifactId>
</dependency>

2.yml文件

server:
port: 8001 #服务端口
spring:
application:
name: cloud-payment-service #服务名
eureka:
client:
#表示是否将自己注册进EurekaServer默认为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 #集群版
#defaultZone: http://eureka7001.com:7001/eureka #单机版
instance:
instance-id: payment8001
prefer-ip-address: true #点进去左下角会显示ip
#Eureka 客户端像服务端发送心跳的时间间隔,单位为秒(默认是30秒)
#lease-renewal-interval-in-seconds: 1
#Eureka 服务端收到最后一次心跳后等待时间上线,单位为秒(默认是90秒),超时将剔除服务
#lease-expiration-duration-in-seconds: 2

3.启动类

@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}

3.最终实现效果

原文地址:https://www.cnblogs.com/fuqiang-zhou/p/12666808.html