标签:重试 设置 bin 只读 pad when dns 配置ip 文件中
1 spring.application.name=eureka-server 2 server.port=8761 3 4 eureka.instance.hostname=localhost 5 6 # 代表不向注册中心注册自己 7 eureka.client.register-with-eureka=false 8 9 # 不去检索服务 10 eureka.client.fetch-registry=false 11 eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
1 package com.suns.eurekaserver; 2 3 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 7 8 @EnableEurekaServer 9 @SpringBootApplication 10 public class EurekaServerApplication { 11 12 public static void main(String[] args) { 13 SpringApplication.run(EurekaServerApplication.class, args); 14 } 15 }
spring.application.name=eureka-service server.port=8762 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
1 package com.suns.eurekaservice; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 @EnableDiscoveryClient 8 @SpringBootApplication 9 public class EurekaServiceApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaServiceApplication.class, args); 13 } 14 }
1 package com.suns.eurekaservice.api; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.cloud.client.ServiceInstance; 6 import org.springframework.cloud.client.discovery.DiscoveryClient; 7 import org.springframework.web.bind.annotation.RequestMapping; 8 import org.springframework.web.bind.annotation.RequestMethod; 9 import org.springframework.web.bind.annotation.RestController; 10 11 12 @RestController 13 public class HelloController { 14 15 private final Logger logger = Logger.getLogger(getClass()); 16 17 @Autowired 18 private DiscoveryClient client; 19 20 @RequestMapping(value = "/hello", method = RequestMethod.GET) 21 public String index() { 22 ServiceInstance instance = client.getLocalServiceInstance(); 23 logger.info("/hello, host: " + instance.getHost() + ", service_id: " + instance.getServiceId()); 24 return "Hello World"; 25 } 26 }
application.properties
1 spring.application.name=eureka-client 2 server.port=8763 3 4 eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
EurekaClientApplication.java
1 package com.suns.eurekaclient; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.web.client.RestTemplate; 9 10 @EnableDiscoveryClient 11 @SpringBootApplication 12 public class EurekaClientApplication { 13 14 @Bean 15 @LoadBalanced 16 RestTemplate restTemplate() { 17 return new RestTemplate(); 18 } 19 20 public static void main(String[] args) { 21 SpringApplication.run(EurekaClientApplication.class, args); 22 } 23 }
ConsumerController.java
1 package com.suns.eurekaclient.api; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RestController; 7 import org.springframework.web.client.RestTemplate; 8 9 10 @RestController 11 public class ConsumerController { 12 13 @Autowired 14 RestTemplate restTemplate; 15 16 @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET) 17 public String helloConsumer() { 18 return restTemplate.getForEntity("http://EUREKA-SERVICE/hello", String.class).getBody(); 19 } 20 }
2020-02-10 07:33:01.234 INFO 4036 --- [nio-8762-exec-9] c.s.eurekaservice.api.HelloController : /hello, host: DCNA7245.APAC.Local, service_id: eureka-service
127.0.0.1 server1
127.0.0.1 server2
--- spring: profiles: server1 # 指定profile=server1 application: name: eureka-high-avai-server server: port: 8761 eureka: instance: hostname: server1 # 指定当profile=server1时,主机名 client: serviceUrl: defaultZone: http://server2:8762/eureka/ # 将自己注册到server2这个Eureka上面去 server: enableSelfPreservation: false # 关掉Eureka的自我保护机制 --- spring: profiles: server2 application: name: eureka-high-avai-server server: port: 8762 eureka: instance: hostname: server2 client: serviceUrl: defaultZone: http://server1:8761/eureka/ server: enable-self-preservation: false
1 package com.suns.eurekahighavaiserver; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 7 @EnableEurekaServer 8 @SpringBootApplication 9 public class EurekaHighAvaiServerApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaHighAvaiServerApplication.class, args); 13 } 14 }
1 --- 2 spring: 3 profiles: service1 4 application: 5 name: eureka-high-avai-service 6 server: 7 port: 8763 8 eureka: 9 client: 10 serviceUrl: 11 defaultZone: http://server1:8761/eureka/,http://server2:8762/eureka/ 12 13 --- 14 spring: 15 profiles: service2 16 application: 17 name: eureka-high-avai-service 18 server: 19 port: 8764 20 eureka: 21 client: 22 serviceUrl: 23 defaultZone: http://server1:8761/eureka/,http://server2:8762/eureka/
EurekaHighAvaiServiceApplication.java
1 package com.suns.eurekahighavaiservice; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 7 @EnableDiscoveryClient 8 @SpringBootApplication 9 public class EurekaHighAvaiServiceApplication { 10 11 public static void main(String[] args) { 12 SpringApplication.run(EurekaHighAvaiServiceApplication.class, args); 13 } 14 }
1 package com.suns.eurekahighavaiservice.api; 2 3 import org.apache.log4j.Logger; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.cloud.client.ServiceInstance; 7 import org.springframework.cloud.client.discovery.DiscoveryClient; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RequestMethod; 10 import org.springframework.web.bind.annotation.RestController; 11 12 13 @RestController 14 public class HelloController { 15 16 private final Logger logger = Logger.getLogger(getClass()); 17 18 @Autowired 19 private DiscoveryClient client; 20 21 @Value("${server.port}") 22 String port; 23 24 @RequestMapping(value = "/hello", method = RequestMethod.GET) 25 public String index() { 26 ServiceInstance instance = client.getLocalServiceInstance(); 27 logger.info("/hello, host: " + instance.getHost() + ", service_id: " + instance.getServiceId() + ", port:" + port); 28 return "/hello, host: " + instance.getHost() + ", service_id: " + instance.getServiceId() + ", port:" + port; 29 } 30 }
1 spring.application.name=eureka-client 2 server.port=8765 3 4 eureka.client.service-url.defaultZone=http://server1:8761/eureka/,http://server2:8762/eureka/
1 package com.suns.eurekahighavaiclient; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 import org.springframework.cloud.client.loadbalancer.LoadBalanced; 7 import org.springframework.context.annotation.Bean; 8 import org.springframework.web.client.RestTemplate; 9 10 @EnableDiscoveryClient 11 @SpringBootApplication 12 public class EurekaHighAvaiClientApplication { 13 14 @Bean 15 @LoadBalanced 16 RestTemplate restTemplate() { 17 return new RestTemplate(); 18 } 19 20 public static void main(String[] args) { 21 SpringApplication.run(EurekaHighAvaiClientApplication.class, args); 22 } 23 }
1 package com.suns.eurekahighavaiclient.api; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.RequestMethod; 6 import org.springframework.web.bind.annotation.RestController; 7 import org.springframework.web.client.RestTemplate; 8 9 @RestController 10 public class ConsumerController { 11 12 @Autowired 13 RestTemplate restTemplate; 14 15 @RequestMapping(value = "/ribbon-consumer", method = RequestMethod.GET) 16 public String helloConsumer() { 17 return restTemplate.getForEntity("http://EUREKA-HIGH-AVAI-SERVICE/hello", String.class).getBody(); 18 } 19 }
/hello, host: DESKTOP-1BVDM6L, service_id: eureka-high-avai-service, port:8763 /hello, host: DESKTOP-1BVDM6L, service_id: eureka-high-avai-service, port:8764
1 eureka: 2 instance: 3 leaseRenewalIntervalInSeconds: 1 #心跳间隔时间1s 4 leaseExpirationDurationInSeconds: 2 #连续2s未响应时将服务注销
1 eureka: 2 server: 3 enable-self-preservation: false
参数名 | 说明 | 默认值 |
enabled | 启用Eureka客户端 | true |
registryFetchIntervalSeconds | 从Eureka服务端获取注册信息的时间间隔 | 30S |
instanceInfoReplicationIntervalSeconds | 更新实例信息的变化到Eureka服务端的时间间隔 | 30S |
initialInstanceInfoReplicationIntervalSeconds | 初始化实例信息到Eureka服务端的时间间隔 | 40S |
eurekaServiceUrlPollIntervalSeconds | 轮询Eureka服务端地址更改的时间间隔。当与Spring Cloud Config配合,动态刷新Eureka的serviceURL地址需要关注 | 300S |
eurekaServerReadTimeoutSeconds | 读取Eureka Server信息的超时时间 | 8S |
eurekaServerConnectTimeoutSeconds | 连接Eureka Server信息的超时时间 | 5S |
eurekaServerTotalConnections | 从Eureka客户端到所有Eureka服务端的连接总数 | 200S |
eurekaServerTotalConnectionsPerHost | 从Eureka客户端到所有Eureka服务端主机的连接总数 | 50S |
eurekaConnectionIdleTimeoutSeconds | Eureka服务端连接的空闲关闭时间 | 30S |
heartbeatExecutorThreadPoolSize | 心跳连接池的初始化线程数 | 2S |
heartbeatExecutorExponentialBackOffBound | 心跳超时重试延迟时间的最大乘数值 | 10S |
cacheRefreshExecutorThreadPoolSize | 缓存刷新线程池的初始化线程数 | 2S |
cacheRefreshExecutorExponentialBackOffBound | 缓存刷新重试延迟时间的最大乘数值 | 10S |
useDnsForFetchingServiceUrls | 使用DNS来获取Eureka服务端的serviceUrl | false |
registerWithEureka | 是否要将自身的实例信息注册到Eureka服务端 | true |
preferSameZoneEureka | 是否偏好使用处于相同Zone的Eureka服务端 | true |
filterOnlyUpInstances | 获取实例时是否过滤,仅保留UP状态的实例 | true |
fetchRegistry | 是否从Eureka服务端获取注册信息 | true |
${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}
1 management.context-path=/hello 2 3 eureka.instance.statusPageUrlPath=${management.context-path}/info 4 eureka.instance.healthCheckUrlPath=${management.context-path}/health
参数名 | 说明 | 默认值 |
preferIpAddress | 是否优先使用IP作为主机名的标识 | false |
leaseRenewalIntervalInSeconds | Eureka客户端向服务端发送心跳的时间间隔 | 30S |
leaseExpirationDurationInSeconds | Eureka服务端收到最后一次心跳之后等待的时间上限。超过之后服务端将该服务实例从服务清单中剔除,从而禁止服务调用请求被发送到该实例 | 90S |
nonSecurePort | 非安全的通信端口号 | 80 |
securePort | 安全的通信端口号 | 443 |
nonSecurePortEnabled | 是否启用非安全的通信端口号 | true |
securePortEnabled | 是否启用非安全的通信端口号 | |
appname | 服务名,默认取spring.application.name配置值,如果没有则为unknown | |
hostname | 主机名,不配置的时候将根据操作系统的主机名来获取 |
|
Spring Cloud微服务实战-服务治理(Spring Cloud Eureka)
标签:重试 设置 bin 只读 pad when dns 配置ip 文件中
原文地址:https://www.cnblogs.com/kesuns/p/12284238.html