Spring Cloud Netflix Ribbon & RestTemplate 负载均衡调用服务

Posted by 彭超 on 2019-10-18
Estimated Reading Time 2 Minutes
Words 568 In Total
Viewed Times

简介

在微服务架构中,业务都会被拆分成一个独立的服务,服务与服务的通讯是基于 Http Restful 的。Spring Cloud 有两种服务调用方式,一种是 Ribbon + RestTemplate ,另一种是 Feign

Ribbon 是一个负载均衡客户端,可以很好的控制 HTTP 和 TCP 的一些行为。

引入依赖

在统一依赖管理项目中继承 Spring Boot 2.0.2.RELEASE 父项目,并声明 Spring Cloud Finchley.RC1 依赖版本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
</parent>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RC1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

在当前项目 pom.xml 中主要添加依赖 spring-cloud-starter-netflix-eureka-serverspring-cloud-starter-netflix-ribbon

1
2
3
4
5
6
7
8
9
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</dependency>

相关配置

application.yml 中添加以下配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
spring:
application:
name: hello-spring-cloud-web-admin-ribbon
thymeleaf:
cache: false
mode: LEGACYHTML5
encoding: UTF-8
servlet:
content-type: text/html

server:
port: 8764

eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/

Application 入口类中通过添加 @EnableDiscoveryClient 注解开启发现服务功能

1
2
3
4
5
6
7
@SpringBootApplication
@EnableDiscoveryClient
public class WebAdminRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(WebAdminRibbonApplication.class, args);
}
}

创建 RestTemplateConfiguration 配置类,该类用于注入 RestTemplate,并通过 @LoadBalanced 注解表明开启负载均衡功能

1
2
3
4
5
6
7
8
9
@Configuration
public class RestTemplateConfiguration {

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

Service

在这里我们直接用的程序名替代了具体的 URL 地址,在 Ribbon 中它会根据服务名来选择具体的服务实例,根据服务实例在请求的时候会用具体的 URL 替换掉服务名,代码如下:

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

@Autowired
private RestTemplate restTemplate;

public String sayHi(String message) {
return restTemplate.getForObject("http://hello-spring-cloud-service-admin/hi?message=" + message, String.class);
}
}

Controller

创建 Controller 对外提供调用的服务

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

@Autowired
private AdminService adminService;

@RequestMapping(value = "hi", method = RequestMethod.GET)
public String sayHi(@RequestParam String message) {
return adminService.sayHi(message);
}
}

If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !