这是SpringCloud集成Eureka注册中心最基本的应用
介绍
SpringCloud中的核心组件Eureka提供了服务注册和服务发现功能,管理分布式系统中的各种服务,比如注册、发现、熔断、负载均衡等。
版本说明
SpringCloud:Greenwich.RELEASE
SpringBoot :2.1.2.RELEASE
实现步骤
1.引入依赖
- 父项目
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 子项目
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.添加配置application.properties
# 项目端口号
server.port=6001
# 项目名称
spring.application.name=service-registry
# 是否把自己作为服务注册到其他服务注册中心,默认true
eureka.client.register-with-eureka=false
# 是否从其他的服务中心同步服务列表,默认true
eureka.client.fetch-registry=false
# 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
3.启动类添加启动注册中心
@SpringBootApplication
@EnableEurekaServer // 启用Eureka服务
public class Application {
public static void main(String[] args){
SpringApplication.run(Application.class,args);
}
}
集群注册中心
在分布式系统中,注册中心是最重要的基础部分,为了防止因为注册中心故障导致毁灭性灾难,必须保证注册中心的高可用性,我们可以使用集群的方式。
1.创建项目
按照以上介绍创建3个项目
registry-1
registry-2
registry-3
2.修改操作系统的host文件
- windows系统 C:\Windows\System32\drivers\etc\hosts
- linux系统 /etc/hosts
添加如下配置:
127.0.0.1 service-registry1
127.0.0.1 service-registry2
127.0.0.1 service-registry3
3、配置文件改造
#registry-1.application.properties
# 项目端口号
server.port=6001
# 项目名称
spring.application.name=service-registry
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
# 是否把自己作为服务注册到其他服务注册中心
eureka.client.register-with-eureka=true
# 是否从其他的服务中心同步服务列表
eureka.client.fetch-registry=true
# 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://service-registry2:6002/eureka/,http://service-registry3:6003/eureka/
eureka.instance.appname=service-registry
#主机名称
eureka.instance.hostname=service-registry1
#服务注册中心地址
#registry-2.application.properties
# 项目端口号
server.port=6002
# 项目名称
spring.application.name=service-registry
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
# 是否把自己作为服务注册到其他服务注册中心
eureka.client.register-with-eureka=true
# 是否从其他的服务中心同步服务列表
eureka.client.fetch-registry=true
# 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://service-registry1:6001/eureka/,http://service-registry3:6003/eureka/
eureka.instance.appname=service-registry
#主机名称
eureka.instance.hostname=service-registry2
#registry-3.application.properties
# 项目端口号
server.port=6003
# 项目名称
spring.application.name=service-registry
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
# 是否把自己作为服务注册到其他服务注册中心
eureka.client.register-with-eureka=true
# 是否从其他的服务中心同步服务列表
eureka.client.fetch-registry=true
# 设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。
eureka.client.serviceUrl.defaultZone=http://service-registry1:6001/eureka/,http://service-registry2:6003/eureka/
eureka.instance.appname=service-registry
#主机名称
eureka.instance.hostname=service-registry3
注意
1.服务注册中心集群相互注册一定要开启
register-with-eureka: true
fetch-registry: true
2.服务注册中心集群的spring.application.name一定要一样
3.eureka.client.serviceUrl.defaultZone:不能出现 localhost,一定要使用host指定主机名
参照
https://www.jianshu.com/p/a723009526b7