博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring-cloud-ribbon简单使用
阅读量:3917 次
发布时间:2019-05-23

本文共 4665 字,大约阅读时间需要 15 分钟。

在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于http restful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,

一、ribbon简介

Ribbon is a client side load balancer which gives you a lot of control over the behaviour of HTTP and TCP clients. Feign already uses Ribbon, so if you are using @FeignClient then this section also applies. —–摘自官网

ribbon是一个负载均衡客户端,可以很好的控制http和tcp的一些行为。Feign默认集成了ribbon。

ribbon 已经默认实现了这些配置bean:

  • IClientConfig ribbonClientConfig: DefaultClientConfigImpl
  • IRule ribbonRule: ZoneAvoidanceRule
  • IPing ribbonPing: NoOpPing
  • ServerList ribbonServerList: ConfigurationBasedServerList
  • ServerListFilter ribbonServerListFilter: ZonePreferenceServerListFilter
  • ILoadBalancer ribbonLoadBalancer: ZoneAwareLoadBalancer

二、准备工作

基于上一节spring-cloud-eureka,启动eureka-server 工程;启动service-client工程,在idea中指定端口并多次启动,我这里分别启动了8081、8763、8762、8765端口,

方法:在VM options 中,添加参数-Dserver.port=8081
在这里插入图片描述

三、准备一个服务消费者

在上一个项目中再创建一个springboot模块

名称为:server-ribbon
pom.xml文件如下:

4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.2.RELEASE
com.itinfo.server-ribbon
server-ribbon
0.0.1-SNAPSHOT
server-ribbon
Demo project for Spring Boot
1.8
Hoxton.SR6
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
org.springframework.cloud
spring-cloud-starter-netflix-ribbon
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin

在工程的配置文件指定服务的注册中心地址为http://localhost:8761/eureka/,程序名称为 server-ribbon,程序端口为8764。配置文件application.yaml文件内容如下:

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

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

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

写一个测试类HelloService,通过之前注入ioc容器的restTemplate来消费service-hello服务的“/hello”接口,在这里我们直接用的程序名替代了具体的url地址,在ribbon中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的url替换掉服务名,代码如下:

@Servicepublic class HelloService {
@Autowired RestTemplate restTemplate; public String helloService(String name){
return restTemplate.getForObject("http://service-hello/hello?name=" + name,String.class); }}

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

@RestControllerpublic class HelloController {
@Autowired HelloService helloService; @RequestMapping(value = "/hello") public String hello(@RequestParam String name){
return helloService.helloService(name); }}

在浏览器上多次访问http://localhost:8764/hello?name=二哈,浏览器交替显示:

  • hello二哈I am from port :8081
  • hello二哈I am from port :8763
  • hello二哈I am from port :8762
  • hello二哈I am from port :8765
    这说明当我们通过调用restTemplate.getForObject(“http://service-hello/hello?name=”+name,String.class)方法时,已经做了负载均衡,访问了不同的端口的服务实例。

此时项目结构图

在这里插入图片描述

  • 一个服务注册中心,eureka server,端口为8761
  • service-client工程跑了四个实例,端口分别为8762,8763,8765,8081分别向服务注册中心注册
  • server-ribbon端口为8764,向服务注册中心注册
  • 当server-ribbon通过restTemplate调用service-client的hello接口时,因为用ribbon进行了负载均衡,会轮流的调用service-hello:8762、8763、8765、8081 四个端口的hello接口;

转载地址:http://fwjrn.baihongyu.com/

你可能感兴趣的文章
C# 中的数字分隔符 _
查看>>
持续交付一:从开发到上线的环境
查看>>
使用 docker 构建分布式调用链跟踪框架skywalking
查看>>
深度探秘.NET 5.0
查看>>
Github Actions 中 Service Container 的使用
查看>>
天际数见数据质量巡检架构优化
查看>>
别在.NET死忠粉面前黑.NET5,它未来可期!
查看>>
Winform 进度条弹窗和任务控制
查看>>
部署Dotnet Core应用到Kubernetes(二)
查看>>
持续交付二:为什么需要多个环境
查看>>
简单理解线程同步上下文
查看>>
购票啦 | 2020中国.NET开发者峰会启动
查看>>
FreeSql接入CAP的实践
查看>>
浅析 EF Core 5 中的 DbContextFactory
查看>>
听说容器正在吃掉整个软件世界?
查看>>
使用WebBenchmark对webapi进行管理和性能测试
查看>>
持续交付三:动手自动化“开发”—>“测试”
查看>>
WebBenchmark动态测试Webapi
查看>>
Windows 7 安装 .NET 5 / .NET Core 3.1 环境的方法和依赖文件
查看>>
接口幂等设计探索实践
查看>>