服务注册与发现——Eureka
是什么?
- 是Netflix的一个子模块,也是核心模块之一,基于REST的服务
- 用于云端中间层服务发现与故障转移。
- 有了服务注册与发现,只需要**使用服务的标识符就可以访问到服务**了
Eureka架构图
- Eureka Server提供服务注册与发现
- Service Provider服务提供方,将自己的信息注册进Eureka,从而使Consumer能够消费
- Service Consumer服务消费方从Eureka Server获取服务列表,从而消费服务
Eureka两个组件
Server
- Eureka Server提供**注册服务**
- 各个节点启动后,会在Eureka Server中注册自己信息,各个服务节点信息都可以在界面中看到
Client
- Client是一个Java客户端,简化与Server的交互,也具备一个内置的、使用轮询策略的负载均衡器
构建Eureka Server服务
-
POM.XML
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloud</artifactId> <groupId>online.abor</groupId> <version>0.0.1-SNAPSHOT</version> <relativePath>../springcloud/pom.xml</relativePath> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springcloud-eureka-7001</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>
-
application.yml
server: port: 7001 eureka: instance: hostname: localhost client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
-
主启动类
package online.abor.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaServer7001_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7001_App.class,args); } }
构建Eureka Client
-
在POM.XML中新增两个依赖
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
修改application.yml
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka instance: instance-id: springcloud-dept8001 prefer-ip-address: true #访问路径可以显示IP地址
-
修改主启动类
package online.abor.springcloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient public class DeptProvider8001_App { public static void main(String[] args) { SpringApplication.run(DeptProvider8001_App.class,args); } }
在provider中模拟发现服务
@Autowired0000000
private final DiscoveryClient client;
@GetMapping(value = "/dept/discovery")
public Object discovery()
{
List<String> list = client.getServices();
System.out.println("**********" + list);
List<ServiceInstance> srvList = client.getInstances("SPRINGCLOUD-DEPT");
for (ServiceInstance element : srvList) {
System.out.println(element.getServiceId() + "\t" + element.getHost() + "\t" + element.getPort() + "\t"
+ element.getUri());
}
return this.client;
}
actutor微服务信息完善
-
添加依赖pom.xml
<!-- actuator监控信息完善 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
-
修改application.yml
info: app.name: abor-springcloud-dept company.name: www.abor.online # build.artifactId: $project.artifactId$ build.artifactId: ${project.artifactId} # build.version: $project.version$ build.version: ${project.version}
-
效果演示
Eureka的自我保护
-
某一时刻某一个微服务不可用了,eureka不会立刻清理掉这个不可用的微服务,依旧会保存这这个微服务的信息
-
可以使用配置来禁用自我保护
eureka: server: enable-self-preservation: false
Eureka集群配置
- 修改hosts映射
127.0.0.1 localhost 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com
- 新建两个eureka server 的model
- 配置三台eureka server的yml
- server1
server: port: 7001 eureka: instance: hostname: eureka7001.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
- server2
server: port: 7002 eureka: instance: hostname: eureka7002.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
- server3
server: port: 7003 eureka: instance: hostname: eureka7003.com #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
- server1
- 修改Provider的yml
eureka:
client: #客户端注册进eureka服务列表内
service-url:
# defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: springcloud-dept8001
prefer-ip-address: true #访问路径可以显示IP地址
- 效果如图