一、概念部分
SpringCloud中的核心组件Eureka提供了服务注册和服务发现功能,管理分布式系统中的各种服务,比如注册、发现、熔断、负载均衡等。
假设一个场景:服务A的某些业务需要调用服务B的某些功能。
使用服务中心前,服务之间的调用关系如下

使用服务注册中心后,服务之间的调用关系如下

服务A和服务B首先将自己注册到服务注册中心,然后服务A不再直接调用服务B,而是通过服务注册中心去获取服务B的调用。
假设一个场景:服务A需要调用服务B,服务B需要调用服务C
他们的调用关系为 A->B->C
使用服务注册中心,他们的调用关系图如下:

服务A、服务B、服务C都注册都服务注册中心,然后服务A通过服务注册中心获取到服务B的调用,服务B再通过服务注册中心获取到服务C的调用。
分布式系统中所有的服务都将注册到注册中心,服务直接都不能直接调用。所有的调用必须通过注册中心来调用。
通过服务中心来调用服务,不需要关心所调用服务所在机器的ip地址或其他信息,每个服务注册到服务中心时只需提供一个注册名称,调用时将通过服务名称来调用。
假设一个场景:服务A需要调用服务B,并且要保证服务B的高可用性,所以服务B有多个机器实例来组成集群。,关系图如下:

我们可以提供几台相同的服务注册到服务中来做集群和负载均衡,服务调用者无需关心服务所在机器的信息,我们可以很方便的做很多高级的功能,比如监控服务器调用成功率来做熔断,移除服务列表中的故障点;监控服务调用时间来对不同的服务器设置不同的权重等等。
二、代码示例
1.在build.gradle中添加依赖
compile('javax.servlet:javax.servlet-api:3.1.0')
compile('org.springframework.cloud:spring-cloud-starter')
compile('org.springframework.cloud:spring-cloud-starter-eureka-server’)
2.在应用代码处启用注解
/**
* 服务注册中心
*/
@SpringBootApplication
@EnableEurekaServer // 启用Eureka服务
public class ServiceRegistryApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceRegistryApplication.class, args);
}
}
3.新建配置文件application.yml
#服务器配置
server:
#端口
port: 9000
#配置Spring
spring:
application:
#应用名称 (服务注册中心)
name: service-registry
#服务器注册发现配置
eureka:
client:
service-url:
#服务注册中心地址,查询服务和注册服务都需要依赖这个地址,默认地址:[http://localhost:8761/eureka](http://localhost:8761/eureka) ,可以配置多个地址用逗号分隔
defaultZone: http://localhost:${server.port}/eureka
#禁用将自己注册到自己Eureka Server
register-with-eureka: false
#禁用从Eureka Server 获取注册信息
fetch-registry: false
4.运行项目,浏览器访问 http://localhost:9000

其他客户端就可以通过http://localhost:9000/eureka 来注册自己到服务中心
集群注册中心
在分布式系统中,注册中心是最重要的基础部分,为了防止因为注册中心故障导致毁灭性灾难,必须保证注册中心的高可用性,我们可以使用集群的方式。
如下图所示:

这里用3个服务注册中心实例为例子,每个注册中心都和其他两个注册中心进行注册自己,实现两两相互注册。
1.添加配置文件:
application-registry1.yml
#spring配置
spring:
application:
#应用名称
name: service-registry
#服务器配置
server:
#端口
port: 9001
#服务中心发现注册配置
eureka:
instance:
#主机名称
hostname: service-registry1
appname: service-registry
client:
#服务注册中心地址
service-url:
defaultZone: http://service-registry2:9002/eureka,http://service-registry3:9003/eureka
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
register-with-eureka: true
fetch-registry: true
application-registry2.yml
#spring配置
spring:
application:
#应用名称
name: service-registry
#服务器配置
server:
#端口
port: 9002
#服务中心发现注册配置
eureka:
instance:
#主机名称
hostname: service-registry2
appname: service-registry
client:
#服务注册中心地址
service-url:
defaultZone: http://service-registry1:9001/eureka,http://service-registry3:9003/eureka
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
register-with-eureka: true
fetch-registry: true
application-registry3.yml
#spring配置
spring:
application:
#应用名称
name: service-registry
#服务器配置
server:
#端口
port: 9003
#服务中心发现注册配置
eureka:
instance:
#主机名称
hostname: service-registry3
client:
#服务注册中心地址
service-url:
defaultZone: http://service-registry1:9001/eureka,http://service-registry2:9002/eureka
#服务注册中心相互注册一定要显示的设置register-with-eureka 和fetch-registry的值为true,否则会服务不可用
register-with-eureka: true
fetch-registry: true
2.修改操作系统的host文件
添加如下配置:
#配置服务注册中心
127.0.0.1 service-registry1
127.0.0.1 service-registry2
127.0.0.1 service-registry3
3.打包
gradle build
4.运行
java -jar service-registry-0.0.1-SNAPSHOT.jar --spring.profiles.active=registry1
java -jar service-registry-0.0.1-SNAPSHOT.jar --spring.profiles.active=registry2
java -jar service-registry-0.0.1-SNAPSHOT.jar --spring.profiles.active=registry3
打开浏览器
输入 http://localhost:9001/
或者 http://localhost:9002/
或者 http://localhost:9003/
都可以看到如下图所示信息

说明服务注册中心已经实现了集群。
项目地址:
https://github.com/lanshiqin/cloud-project.git
欢迎点赞
注意事项
在集群中由于配置不当可能遇到的问题:下图表示服务注册中心不可用的节点

解决方案:
1.服务注册中心集群相互注册一定要开启
register-with-eureka: true
fetch-registry: true
2.服务注册中心集群的spring.application.name一定要一样
3.eureka.client.service-url.defaultZone:不能出现 localhost,一定要使用host指定主机名
正确配置的服务注册中心集群信息如下

有关更多服务注册的文章,可参考其他优秀文章:
http://www.roncoo.com/article/detail/128041
http://chenja.iteye.com/blog/2375104
http://blog.csdn.net/liuchuanhong1/article/details/54895070
http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html
网友评论