springcloud 文件服务器,SpringCloud之实现服务器端的负载均衡Ribbon(二)

一 Ribbon简介

Ribbon是Netflix发布的负载均衡器,它有助于控制HTTP和TCP的客户端的行为。为Ribbon配置服务提供者地址后,Ribbon就可基于某种负载均衡算法,自动地帮助服务消费者去请求。Ribbon默认为我们提供了很多负载均衡算法,例如轮询、随机等。当然,我们也可为Ribbon实现自定义的负载均衡算法。

在Spring Cloud中,当Ribbon与Eureka配合使用时,Ribbon可自动从Eureka Server获取服务提供者地址列表,并基于负载均衡算法,请求其中一个服务提供者实例。展示了Ribbon与Eureka配合使用时的架构

a4a52a4f70c09dcfbf5ecd1f1d1667de.png

搭建负载均衡Ribbon (ribbon-consumer)

接到上篇 http://www.cnblogs.com/grasp/p/9258811.html

继续在springcloud工程中添加模块ribbon-consumer,也是通过start.spring.io提供的模板创建

a4ca2bef6e13b9067615fe90cb918c1c.png

新的目录

ceb5e88feaec59563e46fb82168982b0.png

生成的pom.xml文件为

4.0.0

com.xuan

ribbon-consumer

0.0.1-SNAPSHOT

jar

ribbon-consumer

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

2.0.3.RELEASE

UTF-8

UTF-8

1.8

Finchley.RELEASE

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-netflix-ribbon

org.springframework.boot

spring-boot-starter-test

test

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

修改启动文件RibbonConsumerApplication.java,注意增加RestTemplate 的bean注解。

packagecom.xuan.ribbonconsumer;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.cloud.client.discovery.EnableDiscoveryClient;importorg.springframework.cloud.client.loadbalancer.LoadBalanced;importorg.springframework.context.annotation.Bean;importorg.springframework.web.client.RestTemplate;

@EnableDiscoveryClient

@SpringBootApplicationpublic classRibbonConsumerApplication {

@Bean

@LoadBalanced

RestTemplate restTemplate () {return newRestTemplate();

}public static voidmain(String[] args) {

SpringApplication.run(RibbonConsumerApplication.class, args);

}

}

增加测试的消费接口ConsumerController.java

packagecom.xuan.ribbonconsumer;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.client.RestTemplate;

@RestControllerpublic classConsumerController {

@Autowired

RestTemplate restTemplate;

@RequestMapping(value= "/ribbon-consumer", method =RequestMethod.GET)publicString helloConsumer() {return restTemplate.getForEntity("http://eureka-client/hello",

String.class).getBody();

}

}

注意要去实现提供者的“hello”接口,在后面在描述具体实现过程。

修改配置文件”application.properties“,让消费者注册中心注册,并且通过注册中心找到服务提供者。

spring.application.name=ribbon-consumer

server.port=9000

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/

为了观察是否进行了负载均衡,在eureka-client模块,增加一个服务提供者接口HelloController.java实现hello接口。

packagecom.xuan.eurekaclient;importcom.netflix.appinfo.InstanceInfo;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.cloud.client.ServiceInstance;importorg.springframework.cloud.client.discovery.DiscoveryClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;

@RestControllerpublic classHelloController {private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

@AutowiredprivateDiscoveryClient client;

@Value("${server.port}")

String port;

@RequestMapping(value= "hello", method =RequestMethod.GET)publicString index() {

StringBuffer uriList = new StringBuffer("Hello World " + port + " 端口为您服务!
");

returnuriList.toString();

}

}

如果需要打印服务端的详细明细可以修改为:

packagecom.xuan.eurekaclient;importcom.netflix.appinfo.InstanceInfo;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.cloud.client.ServiceInstance;importorg.springframework.cloud.client.discovery.DiscoveryClient;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestMethod;importorg.springframework.web.bind.annotation.RestController;importjava.util.List;

@RestControllerpublic classHelloController {private static final Logger logger = LoggerFactory.getLogger(HelloController.class);

@AutowiredprivateDiscoveryClient discoveryClient;

@Value("${server.port}")

String port;

@RequestMapping(value= "hello", method =RequestMethod.GET)publicString index() {StringBuffer uriList= new StringBuffer("Hello World " + port + " 端口为您服务!
");

List list = discoveryClient.getInstances("eureka-client");

uriList.append("
discoveryClient.getServices().size() = " +discoveryClient.getServices().size());for( String s : discoveryClient.getServices()){

List serviceInstances =discoveryClient.getInstances(s);for(ServiceInstance si : serviceInstances){

uriList.append("
services:" + s + ":getHost()=" +si.getHost());

uriList.append("
services:" + s + ":getPort()=" +si.getPort());

uriList.append("
services:" + s + ":getServiceId()=" +si.getServiceId());

uriList.append("
services:" + s + ":getUri()=" +si.getUri());

}

}returnuriList.toString();

}

}

在eureka-client模块再增加两个配置文件,使用不同的端口,从而在一天电脑可以启动多个服务提供者,方便测试

增加”application-peer1.properties“文件

spring.application.name=eureka-client

server.port=8091

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/

增加”application-peer1.properties“文件

spring.application.name=eureka-client

server.port=8092

eureka.client.serviceUrl.defaultZone=http://localhost:8080/eureka/

添加完成后eureka-client模块的目录结构为:

23c009350ebec2201790b38b8e0a5c5e.png

设置IDEA编辑器的Edit Configurations,增加两个启动配置,修改过完后的列表和注意的地方:

6c214802c5a15ae8341f1ba72fa15ac7.png

配置和环境都设置完成后:就可以分别启动模块了:

1.EurekaServerApplication

2.EurekaClientApplication,EurekaClientApplication1,EurekaClientApplication2

启动后打开http://localhost:8080/显示如图:

95a6b57314063ea118354370c4c0ea04.png

最后启动RibbonConsumerApplication模块在打开http://localhost:8080/显示如下

c33001453ae9cb2d33b6d87c02bd6218.png

消费者RibbonConsumer也注册成功了。

访问消费者提供的接口http://localhost:9000/ribbon-consumer,查看是否进行了负载均衡,刷新一次,端口就变化了一次,说明访问的是不同的服务提供者

4cbf04b0cee3c1bf3d127d04f5042e81.png

c86f877e3f16c77b584713760826a43d.png

c17f6b69955439a57e3e359a3f6df6bd.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值