标签:object col ISE serial oca 路径 成功 get请求 create
Spring Cloud Ribbon是一套实现客户端负载均衡的工具,注意是客户端,当然也有服务端的负载均衡工具,如Ngnix,可以认为Ribbon就是一个负载均衡(Load Balancer)。负载均衡就是将用户的请求平摊的分配到多个服务器,从而达到系统的高可用。
简单来说,Ribbon的主要功能是提供客户端的软件负载均衡算法,将Netflix的中间层服务连接在一起。
RestTemplate是一个HTTP客户端,使用它我们可以方便的调用HTTP接口,支持GET、POST、PUT、DELETE等方法。
<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);
返回对象为响应体中数据转化成的对象,如下:
@GetMapping("/findByName/{userName}")
public CommonResult getByUserName(@PathVariable String userName) {
return restTemplate.getForObject(userServiceUrl + "/user/findByName/{1}", CommonResult.class, userName);
}
返回对象为ResponseEntity对象,包含了响应中的一些重要信息,比如响应头、响应状态码、响应体等,如下:
@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);
}
}
<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);
@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
restTemplate.postForObject(userServiceUrl + "/user/create", user, CommonResult.class);
return new CommonResult(200, "操作成功");
}
@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
return restTemplate.postForEntity(userServiceUrl + "/user/create", user, CommonResult.class).getBody();
}
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);
示例
@PutMapping("/update")
public CommonResult update(@RequestBody User user) {
restTemplate.put(userServiceUrl + "/user/update", user);
return new CommonResult("操作成功",200);
}
void delete(String url, Object... uriVariables);
void delete(String url, Map<String, ?> uriVariables);
void delete(URI url);
示例
@DeleteMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
restTemplate.delete(userServiceUrl + "/user/delete/{1}", null, id);
return new CommonResult("操作成功",200);
}
正是开始Spring Cloud Ribbon的实战代码学习
首先创建一个user-service模块提供服务,用于给Ribbon调用
<dependencies>
<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>
</dependencies>
作为Ribbon调用的服务端,主要配置了服务名、端口和注册中心地址
server:
port: 8001 #端口
spring:
application:
name: user-service #应用名,后面Ribbon调用可直接通过应用名调用
eureka:
instance:
hostname: localhost
client:
fetch-registry: true #指定是否从注册中心获取服务列表
register-with-eureka: true #指定是否要注册到注册中心
service-url:
defaultZone: http://jamie:123456@localhost:8761/eureka/ #注册中心地址
UserController控制层类定义了对User对象常见的CRUD接口。
package com.jamieLove.controller;
import com.jamieLove.domain.CommonResult;
import com.jamieLove.domain.User;
import com.jamieLove.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @Author:JamieLove
* @Date:2019-12-10 13:42
* @Description:
*/
@RestController
@RequestMapping("/user")
public class UserController {
private static Logger LOGGER = LoggerFactory.getLogger(UserController.class);
@Autowired
private UserService userService;
@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
userService.create(user);
LOGGER.info("新增用户:{}", user);
return new CommonResult(200, "新增成功");
}
@GetMapping("/all")
public CommonResult getAllUserList() {
List<User> userLists = userService.getAllUserList();
LOGGER.info("查询所有 的用户列表:{}", userLists);
return new CommonResult(200, "查询成功", userLists);
}
@GetMapping("/getUser/{id}")
public CommonResult getUser(@PathVariable Long id) {
User user = userService.getUser(id);
LOGGER.info("通过id:{}查询用户信息:", id);
return new CommonResult(200, "操作成功", user);
}
@PostMapping("/update")
public CommonResult update(@RequestBody User user) {
userService.update(user);
LOGGER.info("更新用户信息:{}", user);
return new CommonResult(200, "更新成功");
}
@GetMapping("/delete/{id}")
public CommonResult delete(@PathVariable Long id) {
userService.delete(id);
LOGGER.info("根据id:{}删除用户信息", id);
return new CommonResult(200, "删除成功");
}
@GetMapping("/findByName/{userName}")
public CommonResult findByName(@PathVariable String userName) {
User user = userService.getUserByName(userName);
LOGGER.info("根据用户名:{}查询用户信息", userName);
return new CommonResult(200, "查询成功", user);
}
@PostMapping("/getUserList")
public CommonResult getUserList(@RequestBody List<Long> ids) {
List<User> userList = userService.getUserList(ids);
LOGGER.info("根据id集合:{}查询用户列表:{}", ids, userList);
return new CommonResult(200, "查询成功", userList);
}
}
package com.jamieLove.domain;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @Author:JamieLove
* @Date:2019-12-10 12:48
* @Description:
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
private static final long serialVersionUID = 9085447891383824313L;
private Long id;
private String userName;
private String passWord;
}
package com.jamieLove.domain;
import lombok.Data;
/**
* @Author:JamieLove
* @Date:2019-12-10 12:51
* @Description:
*/
@Data
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
public CommonResult() {
}
public CommonResult(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
}
public CommonResult(Integer code, String message) {
this.code = code;
this.message = message;
}
public CommonResult(T data) {
this.data = data;
}
}
接口类:
package com.jamieLove.service;
import com.jamieLove.domain.User;
import java.util.List;
/**
* @Author:JamieLove
* @Date:2019-12-10 12:55
* @Description:
*/
public interface UserService {
void create(User user);
User getUser(Long id);
void update(User user);
void delete(Long id);
User getUserByName(String userName);
List<User> getUserList(List<Long> ids);
List<User> getAllUserList();
}
接口实现类:
package com.jamieLove.service.impl;
import com.jamieLove.domain.User;
import com.jamieLove.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Author:JamieLove
* @Date:2019-12-10 12:58
* @Description:
*/
@Service
public class UserServiceImpl implements UserService {
private List<User> userList;
@Override
public void create(User user) {
userList.add(user);
}
@Override
public User getUser(Long id) {
List<User> findUserList = userList.stream().filter(userList -> userList.getId().equals(id)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(findUserList)) {
return findUserList.get(0);
}
return null;
}
@Override
public void update(User user) {
userList.stream().filter(userItem -> userItem.getId().equals(user.getId())).forEach(userItem -> {
userItem.setUserName(user.getUserName());
userItem.setPassWord(user.getPassWord());
});
}
@Override
public void delete(Long id) {
User user = getUser(id);
if (user != null) {
userList.remove(user);
}
}
@Override
public User getUserByName(String userName) {
List<User> findList = userList.stream().filter(userItem -> userItem.getUserName().equals(userName)).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(findList)) {
return findList.get(0);
}
return null;
}
@Override
public List<User> getUserList(List<Long> ids) {
List<User> findUserList = userList.stream().filter(userItem -> ids.contains(userItem.getId())).collect(Collectors.toList());
return findUserList;
}
@Override
public List<User> getAllUserList() {
return userList;
}
@PostConstruct
public void initUser() {
userList = new ArrayList<>();
userList.add(new User(1L, "张三", "123456"));
userList.add(new User(2L, "李四", "223456"));
userList.add(new User(3L, "王五", "323456"));
}
}
创建ribbon-service模块来调用user-service模块实现负载均衡的服务调用。
<dependencies>
<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>
</dependencies>
这里主要配置端口、应用名、注册中心地址、user-service的调用路径(通过应用名调用)以及ribbon相关的配置。
server:
port: 8003
spring:
application:
name: ribbon-service
eureka:
instance:
hostname: localhost
client:
fetch-registry: true
register-with-eureka: true
service-url:
defaultZone: http://jamie:123456@localhost:8761/eureka/
serice-url:
user-service: http://user-service
user-service:
ribbon:
ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
OkToRetryOnAllOperations: true #对超时请求启用重试机制
MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
MaxAutoRetries: 1 # 切换实例后重试最大次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
# com.netflix.loadbalancer.RandomRule #修改负载均衡算法
package com.jamieLove.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* @Author:JamieLove
* @Date:2019-12-10 18:34
* @Description:
*/
@Configuration
public class RibbonConfig {
@Bean
@LoadBalanced //赋予了restTemplate具有负载均衡的能力
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.jamieLove.controller;
import com.jamieLove.domain.CommonResult;
import com.jamieLove.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
/**
* @Author:JamieLove
* @Date:2019-12-10 18:36
* @Description:
*/
@RestController
@RequestMapping("/user")
public class RibbonServiceController {
@Autowired
private RestTemplate restTemplate;
@Value("${serice-url.user-service}")
private String userServiceUrl;
@RequestMapping("/all")
public CommonResult getAllUserList() {
return restTemplate.getForObject(userServiceUrl + "/user/all", CommonResult.class);
}
@PostMapping("/create")
public CommonResult create(@RequestBody User user) {
restTemplate.postForObject(userServiceUrl + "/user/create", user, CommonResult.class);
return new CommonResult(200, "操作成功");
}
@GetMapping("/getUser/{id}")
public CommonResult getUser(@PathVariable Long id) {
return restTemplate.getForObject(userServiceUrl + "/user/getUser/{1}", CommonResult.class, id);
}
@GetMapping("/findByName/{userName}")
public CommonResult getByUserName(@PathVariable String userName) {
return restTemplate.getForObject(userServiceUrl + "/user/findByName/{1}", CommonResult.class, userName);
}
}
package com.jamieLove;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author:JamieLove
* @Date:2019-12-10 18:30
*
* @Description:
*/
@SpringBootApplication
@EnableDiscoveryClient
public class RibbonServiceApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonServiceApplication.class, args);
}
}
注:以上服务启动成功,可在http://localhost:8761/eureka/有如下显示:
可以发现运行在8001和8002的user-service控制台交替打印如下信息:
可以通过修改ribbon的配置,改变负载均衡策略选择实例。
user-service:
ribbon:
ConnectTimeout: 1000 #服务请求连接超时时间(毫秒)
ReadTimeout: 3000 #服务请求处理超时时间(毫秒)
OkToRetryOnAllOperations: true #对超时请求启用重试机制
MaxAutoRetriesNextServer: 1 #切换重试实例的最大个数
MaxAutoRetries: 1 # 切换实例后重试最大次数
NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RoundRobinRule
# com.netflix.loadbalancer.RandomRule #修改负载均衡算法
与全局配置的区别就是ribbon节点挂在服务名称下面,如上就是对ribbon-service调用user-service时的单独配置
所谓负载均衡策略,就是当A服务调用B服务时,此时B服务有多个实例,这时A服务以何种方式来选择调用B服务的实例,Ribbon可以选择以下几种负载均衡策略。
标签:object col ISE serial oca 路径 成功 get请求 create
原文地址:https://www.cnblogs.com/JamieLove/p/12019869.html