一、简介
在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理起来由于复杂,所以需要服务治理,管理服务与服务之间的依赖关系,可以实现服务调用、负载均衡和容错等实现服务注册与发现。
eureka采用了CS的设计架构,Eureka Server作为服务注册功能的服务器,它是服务注册中心,而系统中的其它微服务,使用Eureka的客户端连接到Eureka Server并维持心跳连接。这样系统的维护人员就可以通过Eureka Server来监控系统中各个微服务是否正常运行。
当客户端向 Eureka Server, 注册时,它会提供有关自身的元数据,例如主机和端口 , 健康指标 URL, 主页等。Eureka 从属于服务的每个实例接收心跳消息。如果心跳故障超过可配置的时间表,,则该实例通常会从注册表中删除。另一方(消费者|服务提供者),以该别名的方式去注册中心上获取到实际的服务通讯地址,然后再实现本地RPC远程调用框架核心设计思想:在于注册中心,因为注册中心会维护一个服务列表(管理每个服务与服务之间的一个依赖关系),客户端可以通过注册中心获取服务列表。
二、Eureka Server单机部署
1. pom依赖引入
<!--服务发现客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--web相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency>
2. 配置主启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //表示服务注册中心 @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
3. yml配置
server: port: 7001 spring: application: name: cloud-eureka-service eureka: instance: hostname: eureka7001 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
4. 测试
三、Eureka Server集群部署
1. pom依赖引用(和单机版一致)
<!--服务发现客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--web相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-actuator</artifactId> </dependency>
2. 配置启动类
//eureka-7001 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //表示服务注册中心 @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } } //eureka-7002 import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //表示服务注册中心 @EnableEurekaServer @SpringBootApplication public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class,args); } }
3. yml配置
# 7001 server: port: 7001 spring: application: name: cloud-eureka-service eureka: instance: hostname: eureka7001 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址 defaultZone: http://eureka7002:7002/eureka/ # 7002 server: port: 7002 spring: application: name: cloud-eureka-service eureka: instance: hostname: eureka7002 client: #false表示不向注册中心注册自己 register-with-eureka: false #false表示自己就是注册中心,我的职责是维护服务实例,并不需要去检索服务 fetch-registry: false service-url: #设置与eureka server交互的地址查询服务和注册服务都依赖这个地址 defaultZone: http://eureka7001:7001/eureka/
4. 测试
四、Eureka client部署(服务消费者和服务提供者)
1. pom依赖引入
<!--服务发现客户端--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--web相关--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2. 配置主启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @EnableEurekaClient @SpringBootApplication public class PaymentApplication { public static void main(String[] args) { SpringApplication.run(PaymentApplication.class,args); } }
3. yml配置
server: port: 8888 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka instance: prefer-ip-address: true instance-id: payment-8888
4. 测试
五、配置actuator信息
eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka instance: prefer-ip-address: true #显示主机ip名称 instance-id: payment-8888 #实例id management: endpoint: health: show-details: always #显示详细信息 endpoints: web: exposure: exclude: env #排除actuator暴露的接口 include: ["health","info","mappings"] #设置暴露的actuator接口
温馨提示
点击如果/info获取空信息获取到的数据为空,需要做如下配置:
- pom文件配置,添加springboot插件
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <finalName>${project.name}</finalName> </configuration> </plugin> </plugins> </build>
- 执行springboot:build-info
- 执行成功后,重启项目即可获取info的build信息
{ "build": { "version": "1.0-SNAPSHOT", "artifact": "payment", "name": "payment", "group": "com.yuyue.online.springcloud", "time": "2021-07-24T15:21:12.701Z" } }
- yml配置文件配置服务相关信息
info: # info后字段随意配置 author: javacfox desc: 支付服务 # 结果展示 { "author": "javacfox", "desc": "支付服务", "build": { "version": "1.0-SNAPSHOT", "artifact": "payment", "name": "payment", "group": "com.yuyue.online.springcloud", "time": "2021-07-24T15:21:12.701Z" } }
原理解析
使用了springboot插件生成了build信息(build-info.properties),actuator会读取文件中信息