走进Spring Cloud之四 eureka ribbon(负载均衡服务调用者)(Greenwich版本)
走进Spring Cloud之四 eureka ribbon(负载均衡服务调用者)(Greenwich版本)
- Spring Cloud Ribbon
- service-consumer-ribbon
- pom.xml
- application.yml
- RibbonConsumerApplication.java
- RibbonServiceImpl.java
- 启动工程
- 测试服务调用
Spring Cloud Ribbon
在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯都是HTTP RESTful的。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。
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中实现服务间调用的负载均衡。
service-consumer-ribbon
新建消费者模块
new ->moudle ->service-consumer-ribbon
pom.xml
修改pom.xml添加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"> <parent> <artifactId>scexample</artifactId> <groupId>com.pubutech</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>service-cosumer-ribbon</artifactId> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
application.yml
resource目录下新建application.yml修改配置信息如下:
server: port: 8081 spring: application: name: service-consumer-ribbon eureka: client: service-url: #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。 defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/
RibbonConsumerApplication.java
新建SpringBootApplication RibbonConsumerApplication.java
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) //EnableEurekaClient表明为EurekaClient @EnableEurekaClient //EnableDiscoveryClient向服务中心注册发现 @EnableDiscoveryClient public class RibbonConsumerApplication { public static void main(String[] args) { SpringApplication.run(RibbonConsumerApplication.class, args); } //LoadBalanced 注解表明restTemplate使用LoadBalancerClient执行请求 @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
RibbonServiceImpl.java
新建具体的消费服务实现,利用RestTemplate完成消费服务
@Service public class RibbonServiceImpl implements ExampleService { @Autowired RestTemplate restTemplate; @Override public String hello(String name) { return restTemplate.getForObject("http://service-producer/hello?name="+name,String.class); } }
这里注意
http://service-producer/hello中的
service-producer对应注册中心注册的服务者,
hello指明需要的服务。
新建调用入口ConsumerController.java
@RestController public class ConsumerController { @Autowired RibbonServiceImpl ribbonServiceImpl; @GetMapping("/hello/{name}") public String index(@PathVariable("name") String name) { return ribbonServiceImpl.hello(name); } }
启动工程
再次访问 localhost:8761
测试服务调用
访问http://localhost:8081/hello/jason
- 走进Spring Cloud之五 eureka Feign(服务调用者)(Greenwich版本)
- 第四章:客户端负载均衡:Spring Cloud Ribbon(Spring cloud微服务实战)
- Spring Cloud+Eureka+Ribbon实现客户端负载均衡
- springcloud ribbon搭建服务负载均衡
- Spring Cloud微服务开发笔记5——Ribbon负载均衡策略规则定制
- 玩转SpringCloud(F版本) 一.服务的注册与发现(Eureka)
- Spring Cloud+docker实现微服务负载均衡
- 走进Spring Cloud之九 高可用配置中心(Greenwich版本)
- Spring Cloud 入门教程(五): Ribbon实现客户端的负载均衡
- 【SpringCloud】Netflix源码解析之Ribbon:负载均衡策略的定义和实现
- 走进Spring Cloud之十 高可用配置中心动态刷新(Greenwich版本)
- 微服务框架Spring Cloud介绍 Part4: 使用Eureka, Ribbon, Feign实现REST服务客户端
- springcloud ribbon实现负载均衡的时候,提示Request URI does not contain a valid hostname: http://PRODUCT_SERVICE/
- Spring Cloud源码分析:Ribbon如何为RestTemplate提供负载均衡
- 详解spring cloud中使用Ribbon实现客户端的软负载均衡
- Spring Cloud 服务注册发现-路由-负载均衡-全链路日志跟踪-监控
- spring cloud中Ribbon自定义负载均衡策略
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
- Spring Cloud(三)服务提供者 Eureka + 服务消费者(rest + Ribbon)
- 客户端实现负载均衡:springCloud Ribbon的使用