当前位置: 首页 > news >正文

Spring Cloud Ribbon:负载均衡的服务调用

Spring Cloud Ribbon:负载均衡的服务调用

Spring Cloud Ribbon 是Spring Cloud Netflix 子项目的核心组件之一,主要给服务间调用及API网关转发提供负载均衡的功能,本文将对其用法进行详细介绍

Ribbon简介

Ribbon 是 Netflix 公司开源的一个用于负载均衡的客户端组件,它是 Spring Cloud 生态系统中的一部分;Ribbon 的主要目标是提供一种简单且可定制的负载均衡解决方案,用于在微服务架构中实现服务之间的调用和负载均衡

RestTemplate

RestTemplate 是 Spring Framework 提供的一个用于进行 HTTP 请求的客户端工具类;简化了在 Java 应用程序中进行 HTTP 通信的过程,并提供了丰富的方法来处理请求和响应


RestTemplate 提供了各种方法来执行不同类型的 HTTP 请求,包括 GET、POST、PUT、DELETE 等;支持发送请求并接收响应的各种数据类型,如字符串、字节数组、JSON 对象等

GET请求方法

<T> T getForObject(String url, Class<T> responseType, Object... uriVariables);<T> T getForObject(String url, Class<T> responseType, Map<String, ?> uriVariables);<T> T getForObject(URI url, Class<T> responseType);<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Object... uriVariables);<T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables);<T> ResponseEntity<T> getForEntity(URI var1, Class<T> responseType);

POST请求方法

<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);<T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);<T> T postForObject(URI url, @Nullable Object request, Class<T> responseType);<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables);<T> ResponseEntity<T> postForEntity(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables);<T> ResponseEntity<T> postForEntity(URI url, @Nullable Object request, Class<T> responseType);

PUT请求方法

void put(String url, @Nullable Object request, Object... uriVariables);void put(String url, @Nullable Object request, Map<String, ?> uriVariables);void put(URI url, @Nullable Object request);

DELETE请求方法

void delete(String url, Object... uriVariables);void delete(String url, Map<String, ?> uriVariables);void delete(URI url);

创建user-service模块

创建user-service,用于给Ribbon提供服务调用

  • 添加依赖
<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>
  • 添加application.yml配置
server:port: 8201
spring:application:name: user-service
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
  • 添加UserController用于提供调用接口
/*** Created by macro on 2019/8/29.*/
@RestController
@RequestMapping("/user")
public class UserController {private Logger LOGGER = LoggerFactory.getLogger(this.getClass());@Autowiredprivate UserService userService;@PostMapping("/create")public CommonResult create(@RequestBody User user) {userService.create(user);return new CommonResult("操作成功", 200);}@GetMapping("/{id}")public CommonResult<User> getUser(@PathVariable Long id) {User user = userService.getUser(id);LOGGER.info("根据id获取用户信息,用户名称为:{}",user.getUsername());return new CommonResult<>(user);}@GetMapping("/getUserByIds")public CommonResult<List<User>> getUserByIds(@RequestParam List<Long> ids) {List<User> userList= userService.getUserByIds(ids);LOGGER.info("根据ids获取用户信息,用户列表为:{}",userList);return new CommonResult<>(userList);}@GetMapping("/getByUsername")public CommonResult<User> getByUsername(@RequestParam String username) {User user = userService.getByUsername(username);return new CommonResult<>(user);}@PostMapping("/update")public CommonResult update(@RequestBody User user) {userService.update(user);return new CommonResult("操作成功", 200);}@PostMapping("/delete/{id}")public CommonResult delete(@PathVariable Long id) {userService.delete(id);return new CommonResult("操作成功", 200);}
}

创建ribbon-service模块

创建ribbon-service模块调用user-service模块演示负载均衡的服务调用

  • 添加依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
  • 配置application.yml

配置端口、注册中心地址及user-service的调用路径

server:port: 8301
spring:application:name: ribbon-service
eureka:client:register-with-eureka: truefetch-registry: trueservice-url:defaultZone: http://localhost:8001/eureka/
service-url:user-service: http://user-service

负载均衡

  • @LoadBalanced注解赋予RestTemplate负载均衡的能力
/*** Created by macro on 2019/8/29.*/
@Configuration
public class RibbonConfig {@Bean@LoadBalancedpublic RestTemplate restTemplate(){return new RestTemplate();}
}
  • 添加UserRibbonController类

注入RestTemplate,使用其调用user-service中提供的相关接口

/*** Created by macro on 2019/8/29.*/
@RestController
@RequestMapping("/user")
public class UserRibbonController {@Autowiredprivate RestTemplate restTemplate;@Value("${service-url.user-service}")private String userServiceUrl;@GetMapping("/{id}")public CommonResult getUser(@PathVariable Long id) {return restTemplate.getForObject(userServiceUrl + "/user/{1}", CommonResult.class, id);}@GetMapping("/getByUsername")public CommonResult getByUsername(@RequestParam String username) {return restTemplate.getForObject(userServiceUrl + "/user/getByUsername?username={1}", CommonResult.class, username);}@GetMapping("/getEntityByUsername")public CommonResult getEntityByUsername(@RequestParam String username) {ResponseEntity<CommonResult> entity = restTemplate.getForEntity(userServiceUrl + "/user/getByUsername?username={1}", CommonResult.class, username);if (entity.getStatusCode().is2xxSuccessful()) {return entity.getBody();} else {return new CommonResult("操作失败", 500);}}@PostMapping("/create")public CommonResult create(@RequestBody User user) {return restTemplate.postForObject(userServiceUrl + "/user/create", user, CommonResult.class);}@PostMapping("/update")public CommonResult update(@RequestBody User user) {return restTemplate.postForObject(userServiceUrl + "/user/update", user, CommonResult.class);}@PostMapping("/delete/{id}")public CommonResult delete(@PathVariable Long id) {return restTemplate.postForObject(userServiceUrl + "/user/delete/{1}", null, CommonResult.class, id);}
}

http://www.mrgr.cn/news/61912.html

相关文章:

  • 如何规模化实现完全自动驾驶?Mobileye提出解题“新”思路
  • js实现md5加密
  • ARTS-01
  • 功能篇:spring事务配置
  • js监测页面可见性
  • 数据库系统概论期末复习
  • Java 泛型和反射(15/30)
  • 软件工程经验详细总结
  • 进程线程、同步异步、并发并行
  • 小游戏发展迅速,游戏平台如何从技术方向加速业务转化?
  • 如何进行Java的时间序列分析与算法优化,应该从何入手?
  • 大模型:索引构建、预检索与检索阶段、检索后与生成阶段
  • 自动批量生成图片代码
  • Apache Hive 通过Docker快速入门
  • 深入解析Sysmon日志:增强网络安全与威胁应对的关键一环
  • Leetcode—3216. 交换后字典序最小的字符串【简单】
  • 先验概率、似然概率、后验概率
  • Qt5 读写共享内存,已验证,支持汉字的正确写入和读取
  • Java 中 InputStream 的使用:try-with-resources 与传统方式的比较
  • 解密自闭症全托寄宿肇庆:专业照顾与培养一站式服务
  • node学习记录-os
  • 比较24个结构的迭代次数
  • 量化与知识蒸馏的区别
  • 加密软件有什么功能?
  • flume系列之:flume机器做条带划分提高磁盘性能和吞吐量的详细步骤
  • Xss_less靶场攻略(1-18)