Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是一个基于REST的服务,用于定位服务,以实现云端中间层服务发现和故障转移,服务注册与发现对于微服务来说是非常重要的,有了服务发现与注册,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于Dubbo的注册中心,比如Zookeeper;
特点
SpringCloud 封装了NetFlix公司开发的Eureka模块来实现服务注册和发现
Eureka采用了C-S的架构设计,EurekaServer 作为服务注册功能的服务器,他是服务注册中心
而系统中的其他微服务。使用Eureka的客户端连接到EurekaServer并维持心跳连接。这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,SpringCloud的一些其他模块(比如Zuul)就可以通过EurekaServer来发现系统中的其他微服务,并执行相关的逻辑;和Dubbo架构对比:
Eureka
Dubbo
Eureka 包含两个组件:Eureka Server 和 Eureka Client 。
Eureka Server 提供服务注册服务,各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
Eureka Client是一个Java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向EurekaServer发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除掉(默认周期为90秒)
三大角色
1、新建一个子模块:springcloud-eureka-7001
2、添加Euruka服务依赖
<dependencies> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <version>2.3.3.RELEASE</version> </dependency> </dependencies>
3、创建application.yaml配置文件
server: port: 7001 #Eureka配置 eureka: instance: hostname: localhost #Eureka服务端的实例名称 client: register-with-eureka: false #表示是否向Eureka注册中心注册自己 fetch-registry: false #如果为false,则表示自己为注册中心 service-url: #监控页面 #eureka.instance.hostname对应上面的localhost, server.port 7001 #http://localhost:7001/eureka/ defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4、编写主启动类
package com.study.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //开启Eureka服务,可以接收别人注册进来 public class EurekaServer_7001 { public static void main(String[] args) { SpringApplication.run(EurekaServer_7001.class,args); } }
5、在需要注册的模块添加eureka客户依赖(springcloud-provider-dept-8001)
<!--Eureka--> <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> <version>2.2.5.RELEASE</version> </dependency>
6、在yaml配置文件中添加Eureka配置
#Eureka配置,服务注册到哪里 eureka: client: service-url: defaultZone: http://localhost:7001/eureka/ instance: instance-id: springcloud-provider-dept8001 #修改Eureka默认描述信息
7、在主启动类中添加@EnableEurekaClient注解,标注这个模块是Eureka客户
8、启动测试,查看是否注册成功
某时刻某一个微服务不可用,eureka不会立即清理,依旧会对该微服务的信息进行保存!
1、在eureka客户(springloud-provider-dept-8001)中添加actuator依赖
<!--完善监控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、配置eureka客户yaml
#info配置 info: app.name: zhangsan-springcloud company.name: blog.study.com
3、启动eureka客户,继续访问eureka服务
显示信息
1、在eureka客户模块(springcloud-provider-dept-8001)中添加监管信息依赖
<!--完善监控信息--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2、编写yaml监管信息
#info配置 info: app.name: zhangsan-springcloud company.name: blog.study.com
3、主启动类中添加@EnableDiscoveryClient注解
@EnableDiscoveryClient //服务发现
4、在eureka客户模块编写controller
//用来获取一些注册进来的微服务的消息 @GetMapping("/discovery") public Object discovery(){ //获取微服务列表的清单 List<String> services = discoveryClient.getServices(); //输出微服务的清单 System.out.println("discovery=>:"+services); //得到一个具体的服务的信息,通过具体的微服务id,applicationName List<ServiceInstance> instances = discoveryClient.getInstances("SPRINGCLOUD-PROVIDER-DEPT"); for (ServiceInstance instance : instances) { System.out.println( instance.getHost()+"\t"+ instance.getPort()+"\t"+ instance.getUri()+"\t"+ instance.getServiceId() ); } return this.discoveryClient; }
5、重启7001和8001测试