Eureka 应用
角色:
1. 服务器端: 注册的服务实例保存在内存的注册中心, 客户端同样在内存中保存了注册表信息, 提升Eureka组件性能。
2. 服务提供者: (1) 向服务器注册服务; (2) 发送心跳给服务器; (3) 向服务器端获取注册列表, 提供自己的主机、 端口、健康检测链接等。
3. 服务调用者: 发现与调用服务。
一、 服务器端
Maven (spring-boot 2.1.1):
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
YML:
eureka.client.register-with-eureka: 是否将自己的信息注册到Eureka服务器
eureka.client.fetch-registry: 是否到Eureka服务器中抓取注册信息
启动类注解: @EnableEurekaServer
二、 服务提供者
Maven:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
YML:
spring:
application:
name: provider1
eureka:
instance:
hostname: localhost
client:
service-url:
defaultZone: http://localhost:8761/eureka/
启动类注解: @EnableEurekaClient
三 、 服务调用者
Maven:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
</dependencies>
YML:
spring:
application:
name: invoker1
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/
instance:
hostname: localhost
启动类注解: @EnableDiscoveryClient
EUREKA 集群
一、 改造服务器端
YML:
eureka:
client:
service-url:
defaultZone: http://slave2:8762/eureka/
instance:
hostname: slave1
server:
port: 8761
spring:
application:
name: server1
profiles: slave1
---
eureka:
client:
service-url:
defaultZone: http://slave1:8761/eureka/
instance:
hostname: slave2
server:
port: 8762
spring:
application:
name: server1
profiles: slave2
APPLICATION:
@EnableEurekaServer
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
String profiles = scanner.nextLine();
new SpringApplicationBuilder(Application.class).profiles(profiles).run(args);
}
}
二、 改造服务提供者
YML:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
APPLICATION:
@EnableEurekaClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
String port = scanner.nextLine();
new SpringApplicationBuilder(Application.class).properties("server.port=" + port).run(args);
}
}
三、 改造服务调用者
YML:
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/, http://localhost:8762/eureka/
服务实例的健康自检
服务提供者:
Maven:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
URL:
http://localhost:9001/actuator/health
HealthIndicator:
@Component
public class MyHealthindicatorimplements HealthIndicator {
@Override
public Health health() {
if (健康条件) {
return new Health.Builder(Status.UP).build();
} else {
return new Health.Builder(Status.DOWN).build();
}
}
}
HealthCheckHandler:
@Component
public class MyHealthCheckHandler implements HealthCheckHandler {
@Autowired
private MyHealthindicator indicator;
@Override
public InstanceStatus getStatus(InstanceStatus currentStatus) {
Status s = indicator.health().getStatus();
if (s.equals(Status.UP)) {
执行健康逻辑;
return InstanceStatus.UP;
}else {
执行不健康逻辑;
return InstanceStatus.DOWN;
}
}
}
eureka.client.instance-info-replication-interval-seconds: 配置服务实例的状态更新到服务器的时间(默认30秒)
服务调用者: 获取可用服务列表
private static final Loggerlogger = LoggerFactory.getLogger(InvokerController.class);
@Autowired
private DiscoveryClientdiscoveryClient;
List serviceInstances = getServiceInstances();
for (ServiceInstance serviceInstance : serviceInstances) {
EurekaServiceInstance eurekaServiceInstance = (EurekaServiceInstance) serviceInstance;
InstanceInfo instanceInfo = eurekaServiceInstance.getInstanceInfo();
logger.info(instanceInfo.getAppName() +"---" + instanceInfo.getInstanceId() +"---" + instanceInfo.getStatus());
}
private List getServiceInstances() {
List services = discoveryClient.getServices();
List serviceInstances =new ArrayList();
for (String service : services) {
List instances = discoveryClient.getInstances(service);
serviceInstances.addAll(instances);
}
return serviceInstances;
}
EUREKA 常用配置
eureka.instance.leaseRenewalIntervalInSeconds: 客户端实例向服务器发送周期性心跳的时间间隔(默认30秒)
eureka.instance.leaseExpirationDurationInSeconds: 服务器端接收心跳请求(默认90秒)
eureka.server.eviction-interval-timer-in-ms: 注册表的清理间隔(默认60秒)
eureka.client.registryFetch-IntervalSeconds: 客户端去服务器抓取注册表间隔(默认30秒)
eureka.instance.metadata-map: 配置元数据(通过discoveryClient.genInstance("实例")[0].getMetadata().get("元数据键")获取)
eureka.server.enable-self-preservation: 是否开启自我保护模式