微服务SpringCoud之Ribbon学习案例和源码

xiaoxiao2025-04-06  26

ribbon在springcloud微服务中应用主要就是用来作为负载均衡的。相对于传统的Nginx来说它有着独特的优点。Nginx可以说是服务端的负载均衡,而ribbon可以说是服务端,客户端的负载均衡。

下边是引用网上的一些内容,大家可以自己理解一下:

Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。它是一个基于HTTP和TCP的客户端负载均衡器。它可以通过在客户端中配置ribbonServerList来设置服务端列表去轮询访问以达到均衡负载的作用。

当Ribbon与Eureka联合使用时,ribbonServerList会被DiscoveryEnabledNIWSServerList重写,扩展成从Eureka注册中心中获取服务实例列表。同时它也会用NIWSDiscoveryPing来取代IPing,它将职责委托给Eureka来确定服务端是否已经启动。

而当Ribbon与Consul联合使用时,ribbonServerList会被ConsulServerList来扩展成从Consul获取服务实例列表。同时由ConsulPing来作为IPing接口的实现。

我们在使用Spring Cloud Ribbon的时候,不论是与Eureka还是Consul结合,都会在引入Spring Cloud Eureka或Spring Cloud Consul依赖的时候通过自动化配置来加载上述所说的配置内容,所以我们可以快速在Spring Cloud中实现服务间调用的负载均衡。

上一篇说到:微服务的配置中心环境的搭建和测试案例

我们今天的任务就是学习微服务的Ribbon

1.下载注册中心

注册中心是springcloud微服务的最基础的一个服务 。它是用来管理微服务的一个注册中心,在这里就可以去下载该项目https://download.csdn.net/download/uniqueweimeijun/10750443,

2.建立Ribbon服务端项目两个

Ribbonservice1  &  Ribbonservice2

2.1pom文件

内容只包含依赖内容(够了)

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

2.2配置文件yml

eureka: client: service-url: defaultZone: http://localhost:1000/eureka/ instance: appname: ribbonservice lease-renewal-interval-in-seconds: 1 server: port: 8071 spring: application: name: ribbonservice

其中defaultZone:的值就是上边需要下载的注册中心,启动后的服务名,意思就是该项目启动之后会注册到注册中心里。

2.3建立springboot的启动类

package com.ribbon.service1; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class ServiceOneApplication { public static void main(String[] args) { SpringApplication.run(ServiceOneApplication.class, args); } /** * Spring提供的用于访问Rest服务的客户端 * @return */ @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }

RestTemplate

RestTemplate是Spring提供的用于访问Rest服务的客户端。RestTemplate提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。调用RestTemplate的默认构造函数,RestTemplate对象在底层通过使用java.net包下的实现创建HTTP 请求,可以通过使用ClientHttpRequestFactory指定不同的HTTP请求方式。 ClientHttpRequestFactory接口主要提供了两种实现方式,一种是SimpleClientHttpRequestFactory,使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接,还有一种方式是使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务,使用HttpClient可以配置连接池和证书等信息。

2.4建立个controller用于接受客户端发起的请求

package com.ribbon.service1; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/ribbon") public class RibbonTestApi { /** * * * @return 相关信息 */ @RequestMapping(value = "name", method = RequestMethod.GET) public String getMyBlogNameApi() { return "该服务器端口号:8071"; } }

2.5重复上边操作新建另外的服务端,

1.其中yml文件里的端口改成8072,其他的所有内容都相同。

2.controller中的  return "该服务器端口号:8071";改成  return "该服务器端口号:8072";这是为了验证效果。

2.6 启动上边两个项目

在注册中心中可以看到两个项目在注册中心

3.新建一个spirngboot项目做为Ribbon客户端

3.0 Maven的pom文件依赖

<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>

 

3.1新建启动类

package com.ribbon.client; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableDiscoveryClient public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } /** * Spring提供的用于访问Rest服务的客户端 * * @return */ @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }

3.2 配置文件application.yml

eureka: client: service-url: defaultZone: http://localhost:1000/eureka/ instance: appname: ribbon-client server: port: 8092 spring: application: name: ribbon-client

3.3 建立控制器

package com.ribbon.client; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping(value = "/test") public class TestRibbonApi { /** * 注入RestTemplate */ @Autowired RestTemplate restTemplate; @RequestMapping(value = "/blog/name", method = RequestMethod.GET) public String testGetNameOfBlog() { String url = "http://ribbonservice/ribbon/name"; return restTemplate.getForObject(url, String.class); } @RequestMapping("/index") public String index() { return "index"; } }

 3.4启动项目

在注册中心中可以看到

3.5发起请求 

验证完成。

最近持续更新微服务相关内容,如您喜欢,请点赞评论哦,需要的话加个关注也可以哦。您的鼓励是我前进的动力。 

转载请注明原文地址: https://www.6miu.com/read-5027583.html

最新回复(0)