一 Eureka的架构图
二 常用配置
心跳配置
服务列表抓取配置
元数据的配置和使用
关闭自我保护模式
三 新建ek-server项目
1 配置
server:
port: 8761
eureka:
client:
register-with-eureka: false
fetch-registry: false
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
四 新建ek-provider项目
1 配置
spring:
application:
name: ek-provider
eureka:
instance:
leaseRenewalIntervalInSeconds: 5
leaseExpirationDurationInSeconds: 10
metadata-map:
company-name: crazyit
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
logging:
level:
com.netflix: DEBUG
2 测试心跳是5秒
2018-07-15 12:02:31.932 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.MonitoredConnectionManager : Get connection: {}->http://localhost:8761, timeout = 5000
2018-07-15 12:02:31.932 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : [{}->http://localhost:8761] total kept alive: 1, total issued: 0, total allocated: 1 out of 200
2018-07-15 12:02:31.932 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Getting free connection [{}->http://localhost:8761][null]
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.MonitoredConnectionManager : Released connection is reusable.
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Releasing connection [{}->http://localhost:8761][null]
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Pooling connection [{}->http://localhost:8761][null]; keep alive indefinitely
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Notifying no-one, there are no waiting threads
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] n.d.s.t.j.AbstractJerseyEurekaHttpClient : Jersey HTTP PUT http://localhost:8761/eureka//apps/EK-PROVIDER/DESKTOP-5SDKDG4:ek-provider; statusCode=200
2018-07-15 12:02:31.935 DEBUG 1296 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EK-PROVIDER/DESKTOP-5SDKDG4:ek-provider - Heartbeat status: 200
2018-07-15 12:02:36.936 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.MonitoredConnectionManager : Get connection: {}->http://localhost:8761, timeout = 5000
2018-07-15 12:02:36.936 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : [{}->http://localhost:8761] total kept alive: 1, total issued: 0, total allocated: 1 out of 200
2018-07-15 12:02:36.936 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Getting free connection [{}->http://localhost:8761][null]
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.MonitoredConnectionManager : Released connection is reusable.
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Releasing connection [{}->http://localhost:8761][null]
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Pooling connection [{}->http://localhost:8761][null]; keep alive indefinitely
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] c.n.d.shared.NamedConnectionPool : Notifying no-one, there are no waiting threads
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] n.d.s.t.j.AbstractJerseyEurekaHttpClient : Jersey HTTP PUT http://localhost:8761/eureka//apps/EK-PROVIDER/DESKTOP-5SDKDG4:ek-provider; statusCode=200
2018-07-15 12:02:36.940 DEBUG 1296 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient : DiscoveryClient_EK-PROVIDER/DESKTOP-5SDKDG4:ek-provider - Heartbeat status: 200
2018-07-15 12:02:39.080 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.MonitoredConnectionManager : Closing connections idle longer than 30000 SECONDS
2018-07-15 12:02:39.080 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.NamedConnectionPool : Closing connections idle longer than 30000 SECONDS
2018-07-15 12:02:39.093 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.MonitoredConnectionManager : Closing connections idle longer than -1 SECONDS
2018-07-15 12:02:39.093 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.NamedConnectionPool : Closing connections idle longer than 0 SECONDS
2018-07-15 12:02:39.093 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.NamedConnectionPool : Closing connection last used @ Sun Jul 15 12:02:36 CST 2018
2018-07-15 12:02:39.093 DEBUG 1296 --- [t-Conn-Cleaner2] c.n.d.shared.NamedConnectionPool : Deleting connection [{}->http://localhost:8761][null]
3 测试10秒超期
关闭ek-provider项目,超过10秒后访问Eureka信息页面
四 新建ek-invoker
1 新建配置
server:
port: 8081
spring:
application:
name: ek-invoker
eureka:
client:
registry-fetch-interval-seconds: 30
serviceUrl:
defaultZone: http://localhost:8761/eureka/
logging:
level:
com.netflix: DEBUG
2 新建控制类
package org.crazyit.cloud;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
@Controller
@Configuration
public class TestController {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
@GetMapping("/router")
@ResponseBody
public String router() {
RestTemplate tpl = getRestTemplate();
String json = tpl.getForObject("http://first-police/call/1", String.class);
return json;
}
@Autowired
private DiscoveryClient discoveryClient;
@GetMapping("/list")
@ResponseBody
public String serviceCount() {
List<String> names = discoveryClient.getServices();
for(String serviceId : names) {
List<ServiceInstance> instances = discoveryClient.getInstances(serviceId);
System.out.println(serviceId + ": " + instances.size());
}
return "";
}
@GetMapping("/meta")
@ResponseBody
public String getMetadata() {
List<ServiceInstance> instances = discoveryClient.getInstances("ek-provider");
for(ServiceInstance ins : instances) {
String name = ins.getMetadata().get("company-name");
System.out.println(ins.getPort() + "---" + name);
}
return "";
}
}
3 测试
输入:
http://localhost:8081/list
ek-provider: 1
ek-invoker: 1
8080---crazyit