一、服务注册
在 【SpringCloud】springcloud 学习环境搭建 中我们完成了环境的的搭建,但是并没有将 [ 服务提供者的搭建 springcloud-provider-dept-8081 ] 注册到 EurekaServer端 中,下面我们完成 服务的注册。
1、添加依赖
[ 服务提供者的搭建 springcloud-provider-dept-8081 ] 是作为客户端存在的,所以我们需要的是 EurekaClient 的依赖。
<!-- eureka 客户端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
除了 EurekaClient 的依赖外,我还需要 spring-boot-starter-actuator 启动器,actuator是监控系统健康情况的工具。
<!-- actuator:完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、编写配置文件(application.yaml)
编写 Eureka 的配置
# Eureka 的配置,进行服务注册
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/ # EurekaServer端的url
instance:
instance-id: this is a provider server and port of 8081 # 修改 Eureka 的状态(Status)信息
prefer-ip-address: true #显示真是的IP地址
编写 actuator 的配置,其中除了 info: 是固定的以外,其他的名称皆可自定义。
# actuator 配置
info:
app.info: this is a provider server and port of 8081
author.name: StudiousTiger
author.email: studioustiger@163.com
author.blog: https://blog.csdn.net/m0_45067620
3、开启注解
在主启动类上添加 @EnableEurekaClient 注解,在服务启动后,自动注册到 Eureka 的服务端。
添加 @EnableDiscoveryClient 注解,在服务启动后,会开启服务发现。
@SpringBootApplication
@EnableEurekaClient //在服务启动后,自动注册到 Eureka 的服务端
@EnableDiscoveryClient //服务发现
public class SpringcloudProviderDept8081Application {
public static void main(String[] args) {
SpringApplication.run(SpringcloudProviderDept8081Application.class, args);
}
}
4、测试
启动 [ springcloud-eureka-7001 ] 和 [ springcloud-provider-dept-8081 ] 后访问 http://localhost:7001/
。
之所以我们上面需要spring-boot-starter-actuator启动器
和开启 @EnableDiscoveryClient
注解注解的原因就是因为 this is a provider server and port of 8081 的 url 是 http://desktop-epo7ajk:8081/actuator/info ,而 http://desktop-epo7ajk:8081/actuator/info 正是 actuator 自己的 info 页面。如果不进行 配置 actuator 那么 this is a provider server and port of 8081 就不可以使用。
点击 this is a provider server and port of 8081
,访问 http://desktop-epo7ajk:8081/actuator/info
。
二、Eureka的自我保护机制
EurekaServer端 的自我保护机制的自我保护机制,不如说是对服务的暂存机制。在应用生产过程中,难免会碰到某些服务休眠(出现故障不能正常服务,如:断电、宕机…)。因为这些微服务都是注册在Eureka中的,EurekaServer端 对其有一个监事的作用。EurekaServer端 对服务的监视是通过一种 “心跳” 机制(默认每隔 90 秒查看服务是否活跃)
当某些微服务出现休眠状态,即使 EurekaServer端 监控到服务的休眠,不会注销任何服务实例,而是会服务注册表中的信息。当 EurekaServer端 监控到的 “心跳” 再次恢复到阈值以上时(活跃),EurekaServer端 节点会自动退出自我保护机制。
自我保护机制就是一种对网络异常的安全保护实施,他会报错所有的微服务(健康或者不健康的微服务都会保存),而不会盲目的删除任何微服务,可以让 Eureka集群 更加的健壮/稳定。
EurekaServer端 可取消自我保护机制:
eureka:
server:
enable-self-preservation: false
三、发现服务
如果我们想要获取 获取微服务列表清单 或 获取一个具体的微服务 ,在spring中给我们提供了 DiscoveryClient 类 [ org.springframework.cloud.client.discovery.DiscoveryClient ]。
getServices():获取微服务列表清单
getInstances(String serviceID):获取一个具体的微服务,serviceID就是这里对应的内容
@Autowired
DiscoveryClient client;
@GetMapping("/dept/discovery")
public Object discovery(){
//获取微服务列表清单
List<String> services = client.getServices();
System.out.println(services.toString());
//获取一个具体的微服务
List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT-8081");
for (ServiceInstance instance : instances) {
System.out.println(
instance.getServiceId()+"\n"+
instance.getUri()+"\t"+
instance.getHost()+"\t"+
instance.getPort()+"\n\n"
);
}
return this.client;
}
访问:http://localhost:8081/dept/discovery
✈ ❀ 希望平凡の我,可以给你不凡の体验 ☂ ✿ …