spring cloud ribbon 两则实例(自定义zone与脱离eureka注册中心路由)

区域感知

它会根据提供服务的实例所处的区域(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/

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值