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 组件依赖;
<!-- 引入SpringCloud依赖管理 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 引入SpringCloud Eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. 启动入口类增加 @EnableEurekaServer 注解
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3. 配置文件如下:
修改hosts文件 /etc/hosts, 增加如下配置
127.0.0.1 eureka-server-01
127.0.0.1 eureka-server-02
127.0.0.1 eureka-server-03
单台: application-dev.properties
spring.application.name=xts-eureka-server
server.port=9001
##
## 开发环境 Eureka-Server为单实例: 单服务中心
## 是否将自己注册到EurekaServer上
eureka.client.register-with-eureka=false
## 是否从Eureka Server获取注册信息
eureka.client.fetch-registry=false
## 主机hostname
eureka.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-server
server.port=9001
eureka.instance.hostname=eureka-server-01
eureka.client.service-url.defaultZone=http://eureka-server-02:9002/eureka/,http://eureka-server-03:9003/eureka/
application-test2.properties
spring.application.name=xts-eureka-server server.port=9002 eureka.instance.hostname=eureka-server-02 eureka.client.service-url.defaultZone=http://eureka-server-01:9001/eureka/,http://eureka-server-03:9003/eureka/
application-test3.properties
spring.application.name=xts-eureka-server server.port=9003 eureka.instance.hostname=eureka-server-03 eureka.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/