SpringCloud(Hoxton.SR3)基础篇:第五章、Hystrix-request collapsing(请求合并)
2021-02-10 19:20
标签:eal Stub finally nbsp 开始 final source response jdk 介绍: Hystrix的请求合并就是把重复的请求批量的用一个HystrixCommand命令去执行,以减少通信消耗和线程数的占用。Hystrix的请求合并用到了HystrixCollapser这个抽象类,它在HystrixCommand之前前放置一个合并处理器,将处于一个很短的时间窗(默认10ms)内对同一依赖服务的多个请求进行整合并以批量方式发起请求的功能(服务提供方也需要提供相应的匹配实现接口)。下面我们通过一个例子来看看怎么使用。 示例: 一.首先我们需要一个EurekaServer来作为注册中心。可以参考代码示例搭建一个 springcloud-eureka-service工程 二.新建一个服务提供者工程provider-user (1)pom.xml相关依赖 (2)新建一个User.java。这个model必须要有一个无参的默认构造器,否则后面的实验会报错 (3)新建一个提供服务的controller (4)springboot的启动入口类(注意:Controller等注解类都得在springboot入口类的相同包或子包下面才能被扫描) (5)配置文件application.yml 继承HystrixCollapser实现合并请求 (1)pom.xml相关配置 (2)新建一个User.java。这个model一定要有无参的构造器 (3) service负责调用服务 (4)HystrixCommand命令执行请求 (5)HystrixCollapser命令来做请求合并 (6)写一个controller来辅助测试 (7)springboot的启动入口类 (8)application.yml配置信息 测试: 1.运行springcloud-eureka-service启动注册中心 2.运行provider-user启动服务提供者 3.运行consumer-movie-ribbon-with-hystrix-collapser启动服务消费者 4.在浏览器输入: http://localhost:8010/collapse 输出结果:从结果中我们看到前3次请求合并为一个请求,后面2次请求合并为了一个请求 注解方式实现合并请求 (1)修改service实现类 (2)修改control层代码 测试结果 SpringCloud(Hoxton.SR3)基础篇:第五章、Hystrix-request collapsing(请求合并) 标签:eal Stub finally nbsp 开始 final source response jdk 原文地址:https://www.cnblogs.com/wps54213/p/12741202.html parent>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-parentartifactId>
version>2.2.6.RELEASEversion>
relativePath />
parent>
properties>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
java.version>1.8java.version>
spring-cloud.version>Hoxton.SR3spring-cloud.version>
properties>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-dependenciesartifactId>
version>${spring-cloud.version}version>
type>pomtype>
scope>importscope>
dependency>
dependencies>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-webartifactId>
dependency>
dependency>
groupId>org.springframework.cloudgroupId>
artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-actuatorartifactId>
dependency>
dependencies>
package com.qxj.cloud.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
public User(Long id, String username) {
this.id = id;
this.username = username;
}
public User() {
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private String name;
@Column
private int age;
@Column
private BigDecimal balance;
setters()&getters();
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.qxj.cloud.entity.User;
//RestController = @Controller + @ResponseBody
@RestController
public class UserController {
@RequestMapping(value = "/users/{ids}", method = RequestMethod.GET,produces="application/json;charset=UTF-8")
public List
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
//Springboot启动入口类
@SpringBootApplication
//eureka客户端注解
@EnableEurekaClient
public class SimpleProviderUserApplication {
public static void main(String[] args) {
SpringApplication.run(SimpleProviderUserApplication.class, args);
}
}
server:
port: 7900
spring:
application:
name: provider-user
#eureka客户端连接配置
eureka:
client:
#开启健康检查
healthcheck:
enabled: true
service-url:
#注册中心地址
defaultZone: http://user:password123@localhost:8761/eureka
instance:
#将ip注册到eureka上
prefer-ip-address: true
#微服务向eureka注册实例名${spring.cloud.client.ip-address} 表示ip地址
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
三.新建一个服务消费者工程consumer-movie-ribbon-with-hystrix-collapser,在这个工程里我们测试hystrix批量服务调用的请求合并功能
import java.math.BigDecimal;
public class User {
private Long id;
private String username;
private String name;
private Short age;
private BigDecimal balance;
//一定要有无参的构造器
public User(){}
public Long getId() {
return this.id;
}
getter() & setter();
}
import java.util.List;
import com.qxj.cloud.entity.User;
public interface UserService {
public User find(Long id);
public List
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.qxj.cloud.entity.User;
@Service("userService")
public class UserServiceImpl implements UserService{
@Autowired
private RestTemplate restTemplate;
@Override
public User find(Long id) {
return restTemplate.getForObject("http://provider-user:7900/users/{1}", User.class,id);
}
@Override
public List
import java.util.ArrayList;
import java.util.List;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.qxj.cloud.entity.User;
import com.qxj.cloud.service.UserService;
/**
* 自定义HystrixCommand实现
* @author computer
*
*/
public class UserBatchCommand extends HystrixCommand
>{
private UserService userService;
private List
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import com.netflix.hystrix.HystrixCollapser;
import com.netflix.hystrix.HystrixCollapserKey;
import com.netflix.hystrix.HystrixCollapserProperties;
import com.netflix.hystrix.HystrixCommand;
import com.qxj.cloud.entity.User;
import com.qxj.cloud.service.UserService;
/**
* 通过看HystrixCollapser类的源码: public abstract class
* HystrixCollapser
, User, Long>{
private UserService userService;
private Long id;
public UserCollapseCommand(String collapserKey,UserService userService, Long id) {
//接收Setter对象 设置key 和 合并请求时间100ms
super(Setter.withCollapserKey(HystrixCollapserKey.Factory.asKey(collapserKey)).
andCollapserPropertiesDefaults(HystrixCollapserProperties.Setter().withTimerDelayInMilliseconds(100)));
this.userService = userService;
this.id = id;
}
@Override
public Long getRequestArgument() {
// TODO Auto-generated method stub
return id;
}
/**
* @param collapsedRequests 保存了延迟时间窗中收集到的所有获取单个User的请求。通过获取这些请求的参数来组织
* 我们准备的批量请求命令UserBatchCommand实例
*/
@Override
protected HystrixCommand
> createCommand(Collection
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.qxj.cloud.command.UserCollapseCommand;
import com.qxj.cloud.entity.User;
import com.qxj.cloud.service.UserService;
@RestController
public class MovieController {
@Autowired
private UserService userService;
@RequestMapping(value = "/collapseTest", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
public List
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
//该注解表明应用既作为eureka实例又为eureka client 可以发现注册的服务
@EnableEurekaClient
//在启动该微服务的时候就能去加载我们的自定义Ribbon配置类
@RibbonClient(name = "provider-user")
//Hystrix启动类注解,允许断路器
@EnableCircuitBreaker
public class ConsumerMovieRibbonApplication {
@Bean
//使用该注解可以用 http://provider-user:7900/simple/ 虚拟主机名 访问地址
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
public static void main(String[] args) {
SpringApplication.run(ConsumerMovieRibbonApplication.class, args);
}
}
server:
port: 8010
spring:
application:
name: consumer-movie-ribbon-with-hystrix-collapser
#eureka客户端连接配置
eureka:
client:
healthcheck:
enabled: true
service-url:
#注册中心地址
defaultZone: http://user:password123@localhost:8761/eureka
instance:
#将ip注册到eureka上
prefer-ip-address: true
#微服务向eureka注册实例名${spring.cloud.client.ip-address} 表示ip地址 spring2.0以上为ip-address
instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}:${spring.application.instance_id:${server.port}}
import java.util.List;
import java.util.concurrent.Future;
import com.qxj.cloud.entity.User;
public interface PeopleService {
public Future
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qxj.cloud.entity.User;
@Service("peopleService")
public class PeopleServiceImpl implements PeopleService {
@Autowired
private RestTemplate restTemplate;
//批量调用方法findAll,合并请求时间200ms
@HystrixCollapser(batchMethod = "findAll", collapserProperties = {
@HystrixProperty(name = "timerDelayInMilliseconds", value = "200") })
public Future
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestContext;
import com.qxj.cloud.entity.User;
import com.qxj.cloud.service.PeopleService;
@RestController
public class MovieAnnotationController {
@Autowired
private PeopleService peopleService;
@RequestMapping(value = "/AnnoCollapseTest", method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
public List
文章标题:SpringCloud(Hoxton.SR3)基础篇:第五章、Hystrix-request collapsing(请求合并)
文章链接:http://soscw.com/index.php/essay/53711.html