区域感知
它会根据提供服务的实例所处的区域(Zone)与消费者自身的所处区域(Zone)进行比较,过滤
掉那些不是同处一个区域的实例
版本 spring boot 1.5.14
区域实例
eureka 注册中心一 配置 |
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=peer1 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ |
eureka 注册服务器二 配置 |
spring.application.name=eureka-server server.port=1112
eureka.instance.hostname=peer2 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ |
服务提供一 |
spring.application.name=hello-service
eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/
eureka.instance.metadata-map.zone=gz |
服务提供一 controller |
@RestController public class HelloController { @Autowired private DiscoveryClient client; @Qualifier("eurekaRegistration") @Autowired private org.springframework.cloud.client.serviceregistry.Registration eurekaRegistration;
@RequestMapping(value = "/hello",method = RequestMethod.GET) public String index(){ System.out.println("serviceId:"+eurekaRegistration.getServiceId()+" hostName:"+eurekaRegistration.getHost()); return "Hello World eueka_provider gz1"; } } |
服务提供二 配置 |
spring.application.name=hello-service
eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/
eureka.instance.metadata-map.zone=hz
server.port=8081 |
服务提供二 controller |
@RestController public class HelloController { @Autowired private DiscoveryClient client; @Qualifier("eurekaRegistration") @Autowired private org.springframework.cloud.client.serviceregistry.Registration eurekaRegistration;
@RequestMapping(value = "/hello",method = RequestMethod.GET) public String index(){ System.out.println("serviceId:"+eurekaRegistration.getServiceId()+" hostName:"+eurekaRegistration.getHost()); return "Hello World eueka_provider_zone hz1"; } } |
消费者配置 |
spring.application.name=ribbon-consumer server.port=9000 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ eureka.instance.metadata-map.zone=gz |
消费者controller |
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET) public String helloConsumer(){
return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody(); }
} |
消费者启动类 |
@EnableDiscoveryClient @SpringBootApplication public class EurekaConsumerApplication {
public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); }
@Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); }
} |
客户端路由,跳过注册中心例子
消费者 配置 |
spring.application.name=ribbon_consumer_2 server.port=8040 ribbon.eureka.enabled=false eureka.client.fetch-registry=false eureka.client.register-with-eureka=false HELLO-SERVICE.listOfServers=localhost:8080 |
消费者controller |
@RestController public class ConsumerController { @Autowired RestTemplate restTemplate; @RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET) public String helloConsumer(){ return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody(); } } |
消费者启动类 |
@SpringBootApplication public class RibbonconsumerApplication {
public static void main(String[] args) { SpringApplication.run(RibbonconsumerApplication.class, args); }
@Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); } } |
服务提供者 配置 |
spring.application.name=hello-service
eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/
eureka.instance.metadata-map.zone=gz |
服务提供者controller |
package springcloud.eureka_provider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;
import java.util.List; @RestController public class HelloController { @Autowired private DiscoveryClient client; @Qualifier("eurekaRegistration") @Autowired private org.springframework.cloud.client.serviceregistry.Registration eurekaRegistration;
@RequestMapping(value = "/hello",method = RequestMethod.GET) public String index(){ System.out.println("serviceId:"+eurekaRegistration.getServiceId()+" hostName:"+eurekaRegistration.getHost()); return "Hello World eueka_provider gz1"; } } |
注册中心一配置 |
spring.application.name=eureka-server server.port=1111 eureka.instance.hostname=peer1 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ |
注册中心二配置 |
spring.application.name=eureka-server server.port=1112
eureka.instance.hostname=peer2 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ |