1、ribbon简介 

  spring cloud的Netflix中提供了两个组件实现软负载均衡调用:ribbon和feign。

Ribbon 是一个基于 HTTP 和 TCP 客户端的负载均衡器

它可以在客户端配置 ribbonServerList(服务端列表),然后轮询请求以实现均衡负载

它在联合 Eureka 使用时

ribbonServerList 会被 DiscoveryEnabledNIWSServerList 重写,扩展成从 Eureka 注册中心获取服务端列表

同时它也会用 NIWSDiscoveryPing 来取代 IPing,它将职责委托给 Eureka 来确定服务端是否已经启动

2、启动多个client实例

  停止eureka-client服务,打开Run/Debug Configurations

把图中勾选取消,然后保存,启动,此时启动端口为8762,打开配置文件,将端口修改为8763,再次启动,可以看到在注册中心启动了两个服务

3、创建服务消费者service-ribbon

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.study</groupId>
<artifactId>service-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>service-ribbon</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>com.study</groupId>
<artifactId>cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency> </dependencies> </project>

配置文件

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
server:
port: 8764
spring:
application:
name: service-ribbon

在工程的启动类中,通过@EnableDiscoveryClient向服务中心注册;并且向程序的ioc注入一个bean: restTemplate;并通过@LoadBalanced注解表明这个restRemplate开启负载均衡的功能。

@EnableEurekaClient
@EnableDiscoveryClient
@SpringBootApplication
public class ServiceRibbonApplication { public static void main(String[] args) {
SpringApplication.run(ServiceRibbonApplication.class, args);
} @Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hi服务的“/hi”接口,在这里我们直接用的程序名替代了具体的url地址,

在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

@Service
public class HelloService { @Autowired
RestTemplate restTemplate; public String hiService(String name) {
return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class);
}
}

写一个controller,在controller中用调用HelloService 的方法,代码如下:

@RestController
public class HelloControler { @Autowired
HelloService helloService; @GetMapping(value = "/hi")
public String hi(@RequestParam String name) {
return helloService.hiService( name );
}
}

启动工程,在浏览器中访问http://localhost:8764/hi?name=study 浏览器交替显示:

hi study ,i am from port:8762
hi study ,i am from port:8763

这说明当我们通过调用restTemplate.getForObject(“http://SERVICE-HI/hi?name=“+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

4、此时的架构

5、架构分析

  5.1、看实线部分,service-hi 8762、service-hi 8763 和 service-ribbon 8764都作为服务消费者分别向服务注册中心eureka-server 8761中注册。

  5.2、看虚线部分,当sercvice-ribbon通过restTemplate调用service-hi的hi接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hi:8762和8763 两个端口的hi接口。

最新文章

  1. [LeetCode] Next Permutation 下一个排列
  2. 使用VS2013进行单元测试
  3. 问题解决——MFC resource.h 无法添加、提交到SVN
  4. .NET Framework中Object基类有哪些方法?
  5. 怎么解决xp系统不能安装NET Framework4.0?
  6. zoj 3820 Building Fire Stations 树的中心
  7. linux驱动程序之电源管理之新版linux系统设备架构中关于电源管理方式的变更
  8. VS Code 项目编译
  9. POJ 2299 Ultra-QuickSort (求序列的逆序对数)
  10. wordpess关闭评论的方法,wordpress开发
  11. [洛谷P1392] 取数
  12. 【BZOJ4007】[JLOI2015]战争调度(动态规划)
  13. [转]Linux中python3.6+ipython+Jupyter Notebook环境
  14. 最接近的三数之和(java实现)
  15. Golang Struct 声明和使用
  16. HDU - 1260 (Tickets)
  17. [LeetCode] questions conclusion_ Binary Search
  18. Kafka 基本概念学习笔记
  19. SharePoint 创建 Lookup 类型的Site Column解决跨站问题
  20. C++ 使用vector时遇到的一个问题

热门文章

  1. bzoj2073
  2. Coursera Algorithms week3 快速排序 练习测验: Selection in two sorted arrays(从两个有序数组中寻找第K大元素)
  3. Filter,Interceptor和Aspect
  4. PCB genesis短槽加引导孔实现方法
  5. [Swift通天遁地]四、网络和线程-(2)通过BlockOperation实现线程的队列
  6. akka设计模式系列(Actor模型)
  7. 题解报告:hdu 1863 畅通工程
  8. 363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
  9. Linux下查看CPU和内存(很详细)
  10. 消息队列 (1) mac安装RabbitMQ