SpringCloud的Eureka Server模块
1. Eureka Server: 注册中心, 提供服务的注册和发现;
2. Service Provider: 服务提供方, 将自身服务注册到Eureka Server;
3. Service Consumer: 服务消费方,从 Eureka Server 获取注册列表, 从而能够调用相关服务;
脑补一下画面,Eureka Server 就是那个居间桥梁,当然 Eureka在实践中要使用集群,实现多活。
样例步骤:
0. 新建一个SpringBoot项目;
1. pom.xml 引入SpringCloud 的 Eureka 组件依赖;
org.springframework.cloud spring-cloud-dependencies Finchley.RELEASE pom import org.springframework.cloud spring-cloud-starter-netflix-eureka-server
2. 启动入口类增加 @EnableEurekaServer 注解
@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}
3. 配置文件如下:
修改hosts文件 /etc/hosts, 增加如下配置
127.0.0.1 eureka-server-01127.0.0.1 eureka-server-02127.0.0.1 eureka-server-03
单台: application-dev.properties
spring.application.name=xts-eureka-serverserver.port=9001#### 开发环境 Eureka-Server为单实例: 单服务中心## 是否将自己注册到EurekaServer上eureka.client.register-with-eureka=false## 是否从Eureka Server获取注册信息eureka.client.fetch-registry=false## 主机hostnameeureka.instance.hostname=eureka-server-01## 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址eureka.client.service-url.defaultZone=http://eureka-server-02:9001/eureka/
启动:
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
DashBoard查看: http://eureka-server-01:9001/
多节点(3台):
application-test1.properties
spring.application.name=xts-eureka-serverserver.port=9001eureka.instance.hostname=eureka-server-01eureka.client.service-url.defaultZone=http://eureka-server-02:9002/eureka/,http://eureka-server-03:9003/eureka/
application-test2.properties
spring.application.name=xts-eureka-serverserver.port=9002eureka.instance.hostname=eureka-server-02eureka.client.service-url.defaultZone=http://eureka-server-01:9001/eureka/,http://eureka-server-03:9003/eureka/
application-test3.properties
spring.application.name=xts-eureka-serverserver.port=9003eureka.instance.hostname=eureka-server-03eureka.client.service-url.defaultZone=http://eureka-server-01:9001/eureka/,http://eureka-server-02:9002/eureka/
4. 启动
打好jar包,然后分别执行:
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test1
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test2
java -jar eureka-server-0.0.1-SNAPSHOT.jar --spring.profiles.active=test3
5. DashBoard 查看地址
http://eureka-server-01:9001/
http://eureka-server-02:9002/
http://eureka-server-03:9003/