Eureka (已停更进维)
官网:https://cloud.spring.io/spring-cloud-static/spring-cloud-netflix/2.2.2.RELEASE/reference/html/
github地址:https://github.com/Netflix/eureka/wiki
一、搭建 注册中心
1、模块名 cloud-eureka-server7001
2、pml.xml
<parent>
<artifactId>cloud2020</artifactId>
<groupId>com.aqiang9.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<description>Eureka 服务注册中心</description>
<artifactId>cloud-eureka-server7001</artifactId>
<dependencies>
<!-- eureka-server 这是 2.x 与 1.x有差别 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
</dependencies>
3、yml 配置文件
server:
port: 7001
spring:
application:
name: cloud-eureka-service
eureka:
instance:
hostname: eureka7001.com
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
# server:
# enable-self-preservation: false
# eviction-interval-timer-in-ms: 2000
属性说明
server.port
:当前 Eureka Server 服务端⼝。
eureka. instance.hostname
: 服务端的实例名称
eureka.client.register-with-eureka
:是否将当前的 Eureka Server 服务作为客户端进⾏注册。
eureka.client.fetch-fegistry
:是否从EurekaServer抓取注册信息 ,默认为true 单节点无所谓 集群必须
设置为true 才能配合ribbon
使用负载均衡
eureka.client.service-url.defaultZone
:注册中⼼的访问地址, 交互的地址查询服务和注册服务都需要依赖这个地址 , 如果采用的是集群 , 则 其他注册地址URL http://eureka7002.com:7002/eureka/, http://eureka7003.com:7003/eureka/
: 相互注册 相互守望
eureka.server.enable-self-preservation
: 是否开启自我保护,默认开启
eureka.server.eviction-interval-timer-in-ms
: 心跳间隔时间 ,默认2000 毫秒
4、主启动
package com.aqiang9.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* Eureka 服务注册中心
* @author aqiang9 2020-03-07
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args){
SpringApplication.run(EurekaMain7001.class,args) ;
}
}
参数说明
@SpringBootApplication
:声明该类是 Spring Boot 服务的⼊⼝。
@EnableEurekaServer
:声明该类是⼀个 Eureka Server 微服务,提供服务注册和服务发现功能,即
注册中⼼。
5、测试:
访问:http://eureka7001.com:7001/
二、搭建 server 端
1、模块名 cloud-provider-payment8001
2、pml.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<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>
</dependencies>
3、yml 配置文件
server:
port: 8001
spring:
application:
name: cloud-payment-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka
instance:
instance-id: payment8001
prefer-ip-address: true
# lease-renewal-interval-in-seconds: 1
# lease-expiration-duration-in-seconds: 2
属性说明
server.port
:当前 Eureka Server 服务端⼝。
eureka. instance.instance-id
: 服务名称
eureka. instance. prefer-ip-address
: 是否显示ip 默认false
eureka. instance.lease-expiration-duration-in-seconds
: 服务端在收到最后一次心跳等待时间上限默认90秒 超时会踢出服务
4、主启动
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;
/**
* @author zq 2020-03-07
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient // 服务发现
public class PaymentMain8001 {
public static void main(String[] args){
SpringApplication.run(PaymentMain8001.class,args) ;
}
}
参数说明
@EnableEurekaClient
:声明该类是⼀个 Eureka Server 微服务,提供服务注册和服务发现功能,即
注册中⼼。
响应类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CommonResult<T> implements Serializable {
private Integer code ;
private String message ;
private T data ;
}
/**
* @author aqiang9 2020-03-07
*/
@RestController
@Slf4j // lombok注解
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/payment/getport")
public String getServerPort() {
return this.serverPort;
}
@GetMapping("/payment/discovery")
public Object discovery() {
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("element---{}", service);
}
// 服务名称获取
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info("id:{} ,host:{} , port:{} ,uri:{}", instance.getServiceId(), instance.getHost(), instance.getPort(), instance.getUri());
}
return this.discoveryClient;
}
}
5、测试:
1、查看 注册中心 http://eureka7001.com:7001/
总结:
Eureka: service 连接 Consumer 与 Provider
- 什么是服务治理
- 什么是服务注册与发现 可以防止单点故障
* 维持心跳
- Eureka Server 提供服务注册
- Eureka Client 通过注册中心访问
-
单机 =》集群
单机:
底层也是使用httpClient
集群:
- 相互注册 相互守望
actuator 微服务信息完善 必须有actuator 与web包
服务发现discovery
Eureka自我保护 : 进入自我保护时,不再删除微服务信息(不清理 CAP里面的ap分支,)
清理时间(默认90秒) 宁愿多保留时间 , 也不盲目删除微服务, `好死不如赖活`
禁止自我保护机制 server + client 都需要修改
为什么产生 Eureka 自我保护机制
为了防止 EurkaClient 可以正常运行,但是
EurkaServer 网络不通的情况下,EurkeServer 不会即刻 将 EurekaClient 服务剔除。
什么是自我保护模式
默认情况,如果EurekaServer 在一定时间内没有接收到某个微服务实例的心跳,EurkaServer会注销该实例(默认90秒)。但是当网络分区故障发生(延迟、卡顿、拥挤)时,微服务与 EurekaServer 之间无法正常通讯。以上行为可能变得非常危险了-- 因为微服务本身是健康的,此时不应该注销这个微服务。 Eureka 通过“自我保护模式” 来解决这个问题 – 当 EurekaServer 节点在短时间内丢失过多客户端时(可能发生了网络分我去故障),那么这个节点就会进入自我保护模式。
在自我保护模式中,Eureka Server 会保护服务注册表中的信息,不再注销任何服务实例。
它的设计哲学就是宁可保留错误的服务注册信息,也不盲目注销任何可能健康的服务实例。
综上,自我保护模式是一种应对网络异常的安全保护措施,它的架构哲学是宁可同时保留所有微服务(健康的微服务和不健康的我服务都会保留)也不盲目注销任何健康的微服务。使用自我保护模式,可以让 Eureka 集群更加的稳健、稳定。