服务注册、治理和发现:Eureka
代码基于Spirng Boot 2.X 和 Spring Cloud H版
服务治理:
在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务与服务之间依赖关系,可以实现服务调用,均衡负载,容错等,实现服务发现与注册。
服务注册与发现:
Eureka采用CS的设计模式,Eureka Server 作为服务注册功能的服务器,它是服务注册中心。而系统中的其他微服务,使用Eureka的客户端连接到Eureka Server并维持心跳。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。
在服务注册与发现中,有一个注册中心,当服务器启动的时候,会把当前自己服务器的信息。比如服务器通讯地址等以别名方式注册到注册中心上。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC调用RPC远程调用框架核心设计思想:在于注册中心,因为使用注册中心管理服务与服务之间的一个依赖关系(服务治理概念)。在任何RPC远程框架中,都会有一个注册中心(存放服务器地址相关信息(接口地址))
上图为Eureka的系统架构
Eureka Server代码:
pom:
org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter
org.springframework.boot
spring-boot-starter-actuator
注:这里需要说明得是,1.x和2.x引用是不一样得
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-eurekaartifactId> dependency>
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId> dependency>
主启动类:
@SpringBootApplication @EnableEurekaServer public class EurekaMain { public static void main(String[] args) { SpringApplication.run(EurekaMain.class,args); } }
application.yml(单机版):
server: port: 1001 eureka: instance: hostname: eureka1001.com client: #false表示服务不会向注册中心注册自己 register-with-eureka: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port} server: #关闭自我保护机制 enable-self-preservation: false
application.yml(集群版):
server: port: 1001 eureka: instance: hostname: eureka1001.com client: #表示服务不会向注册中心注册自己本身 register-with-eureka: false fetch-registry: false service-url:
#集群版和单机版的唯一区别就是这里要写另外两台注册中心的地址 defaultZone: http://eureka1002.com:1002,http://eureka1003.com:1003 server: #关闭自我保护机制 enable-self-preservation: false
注:集群版还需要修改host,将注册中心的hostname映射到本机的localhost
127.0.0.1 eureka1001.com 127.0.0.1 eureka1002.com 127.0.0.1 eureka1003.com
最后的结果展示:
注:网上的eureka的教程在yml文件中defaultZone的写法一般都是http://xxxxxxxx:xxxx/eureka
我这里是去掉了eureka。
因为集群版的eureka,在点击如下图红框中所示的另外两台注册中心是可以跳转到目标的注册中心的。
如果加上/eureka的话,会出现:
客户端
pom:
服务端的artifactID为:spring-cloud-starter-netflix-eureka-server
客户端的artifactID为:spring-cloud-starter-netflix-eureka-client
<dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId> dependency>
启动类:
服务端引用的注解为:EnableEurekaServer
客户端引用的注解为:EnableEurekaClient
@SpringBootApplication
@EnableEurekaClient
public class CosumerMain{
public static void main(String[] args) {
SpringApplication.run(CosumerMain.class,args);
}
}
yml:
server: port: 80 spring: application: name: cloud-cosumer zipkin: base-url: http://localhost:9411 sleuth: sampler: probability: 1 datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: org.gjt.mm.mysql.Driver url: jdbc:mysql://localhost:3306/test?useUnicodetrue&characterEncoding=utf-8&useSSL=false username: root password: 1234 eureka: client: register-with-eureka: true fetch-registry: true service-url:
# 这里需要向所有的注册中心注册 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka instance: instance-id: comsumer80 prefer-ip-address: true