初识SpringCloud|第二篇|Ribbon

​ 在微服务架构中,业务被拆分成一个个独立的服务,各个服务之间一般通过Restful Api进行通讯。Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign。在这一篇文章首先讲解下基于ribbon+rest。

Ribbon简介

1
2
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是一个负载均衡客户端,可以很好的控制htt和tcp的一些行为。而Feign默认集成了ribbon。

准备工作

​ 因为是基于第一篇的,所以需要启动eureka-sever和eureka-client工程,因为测试Ribbon,所以启动两个服务提供者,将eureka-client工程配置文件中的端口号由8762改成8763再启动工程,这个时候会发现在eureka-server中注册了两个服务,这就相当于一个小的集群。

img

需要注意的是,idea,是不能直接修改了yml文件之后就启动多个实例的,会出现如下提醒:

img

idea需要在工程edit configuration设置勾选Allow paraller run(我的idea版本是2019.1.3,有的版本是要将将默认的Single instance only(单实例)的钩去掉。)

img

服务消费者

​ 重新建立一个springboot模块,取名eureka-ribbon,流程基本同上,pom文件依旧继承父pom,与yml的配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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.kn</groupId>
<artifactId>eureka-ribbon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>eureka_ribbon</name>
<description>Demo project for Spring Boot</description>

<parent>
<groupId>com.kn</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-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>

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@EnableEurekaClient
//@EnableDiscoveryClient
@SpringBootApplication
public class EurekaRibbonApplication {

public static void main(String[] args) {
SpringApplication.run(EurekaRibbonApplication.class, args);
}

@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}

​ 这里说一下@EnableEurekaClient和@EnableDiscoveryClient的区别:

  • @EnableDiscoveryClient注解是基于spring-cloud-commons依赖,并且在classpath中实现;

  • @EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用;

    简单的来说就是@EnableEurekaClient只适用于eureka作为注册中心的,而@EnableDiscoveryClient也适用其他注册中心的落地实现如consul等。

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

1
2
3
4
5
6
7
8
9
10
11
@Service
public class HelloService {

@Autowired
RestTemplate restTemplate;

public String hiService(String name) {
return restTemplate.getForObject("http://EUREKA-CLIENT/hi?name="+name,String.class);
}

}
1
2
3
4
5
6
7
8
9
10
11
@RestController
public class HelloController {

@Autowired
HelloService helloService;

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

启动工程,eureka-server界面如下:

img

在浏览器上多次访问http://localhost:8764/hi?name=kn会重复显示:

1
2
hi kn ,i am from port:8762
hi kn ,i am from port:8763

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

结尾

​ 服务实例名称不能以下划线连接否则在消费者调用restTemplate.getForObject()时会报错,现在架构方面是一个服务注册中心eureka-server:8761,两个服务提供者eureka-client:8762和8763,一个服务消费者eureka-ribbon:8764,消费者和服务提供者之间通过rest+ribbon进行通讯。

0%