一、注册中心的搭建
1.创建EurekaServer项目

2.启动类添加开启EurekaServer的注解@EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class DemoEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(DemoEurekaApplication.class, args);
}
}
3.添加yml配置文件
server:
port: 8761
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #localhost一般为服务器ip
register-with-eureka: false #对于注册中心的优化,不注册本服务
fetch-registry: false
4.启动项目并访问

二、客户端的搭建
1.创建EurekaClient项目

2.添加yml配置文件
server:
port: 8080
spring:
application:
name: demo-member #项目名
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka #注册中心地址
register-with-eureka: true #注册该服务,默认为true
fetch-registry: true #获取服务列表,默认为true
3.启动项目,在Eureka管理页里就可以看到注册的服务

三、Eureka的高可用
1.原理图

2.只用修改配置文件即可,启动三个Eureka-Server在不同端口上,服务同时注册到这三个Eureka-Server上。
# erurek的注册中心地址
server:
port: 876[1:3] #注册中心的地址
spring:
application:
name: eureka-server
eureka:
client:
service-url:
defaultZone: [http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka](http://localhost:8761/eureka,http://localhost:8762/eureka,http://localhost:8763/eureka)
register-with-eureka: true # 把自己注册到别的eureka
fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
网友评论