栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringCloud学习

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringCloud学习

SpringCloud 学习笔记 前言 什么是微服务

将软件的各个功能细分为众多微小的服务,不同服务之间按照某种规范(http协议)通过接口进行通信,不同服务之间可以使用不同的编程语言,不同的存储方式,提高了开发效率,但也提高了运维成本。

AKF拆分原则

将一个系统有三个扩展维度:

x轴:也就是水平复制,多运行几个实例,成功集群加负载均衡的模式。(整体复制)

y轴:数据分区,在不同地方使用相互独立的集群,使用不同的数据分区。(同样是复制,但是数据存在不同的地方)

z周:微服务拆分,将一个整体拆分成不同的业务(不同的功能之间相互独立,通过接口通信,然后在服务的基础上进行水平复制或者数据分区的扩展)

SpringCloud和Dobbo

Springcloud接口规范比较松散,采用http协议,编程难度较小,带宽占用较大。Dubbo采用二进制传输,性能更好,难度也更大

前后端分离

前端和后端只通过JSON格式的数据进行交流,前端可以在不同设备上进行渲染的同时,使用同样的数据

无状态服务

请求的url与url的上下文无关

SpringCloud 作用

将服务拆分为微服务后,会有很多微小的项目,SpringCloud用来统一管理这些项目

SpringCloude第一代SpringCloud第二代
网关Spring Cloud ZuulSpringCloud GateWay
注册中心Eureka,Consul,ZooKeeper阿里 Nacos,拍拍货Radar
配置中心SpringCloud Config阿里Nacos,携程Apoilo,随行付Config keeper
负载均衡RibbonSpring Cloud LoadBalancer
熔断器Hystrix阿里sentinel
版本说明

按照伦敦地铁站名称首字母大写排序
Build-xxx 开发版
M 里程碑版,功能大致完成后成为候选版
RC 候选发布版,错误更正后成为正式版
SR 正式发布版,经过全面测试后变为稳定版
GA 稳定版

2.1.0 希腊字母
2:主版本号,功能架构有较大变化时更改
1:次版本号,有部分内容更改时的版本号
0:小变动
base 设计阶段
alpha 初级版本 bug较多
belta 修复了严重的bug
Gamma 修复了绝大部分bug
release 代表最终版

注册中心Eureka 注册中心的作用

注册中心的作用是服务注册和服务发现

服务注册:将服务的名称和地址添加到注册中心(张三把名字告诉我,我把他的名字和电话号码记在通讯录中)

服务发现:根据服务名称找到服务地址,并进行调用(根据张三的名字在通讯录中查找到电话号码并拨打电话)

服务提供者将可以使用的服务注册到注册中心,并与注册中心建立心跳

服务使用者从注册中心获取服务列表,得知远程调用的途径

服务使用者对服务提供者进行远程调用

常见的注册中心

创建eureka注册中心

记得别写成euraka了

创建父工程

创建一个maven项目,直接进入

配置父工程的pom文件:



    4.0.0

    com.xxxx
    euraka-demo
    pom
    1.0-SNAPSHOT
    
        euraka-server
        eureka-server02
    

    
        13
        13
    
    
        spring-boot-starter-parent
        org.springframework.boot
        2.2.4.RELEASE
    
    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR1
                pom
                import
            
    
    

这里使用

springboot 2.2.4.RELEASE(parent工程)

spring-cloud Hoxton.SR1

创建子项目eureka-server

创建一个new module,创建maven工程,选择maven-archetype-quickstart

配置pom:





  com.xxxx
  euraka-server
  1.0-SNAPSHOT
    
        com.xxxx
        euraka-demo
        1.0-SNAPSHOT
    
   4.0.0
  

      
          org.springframework.cloud
          spring-cloud-starter-netflix-eureka-server
      
      
          org.springframework.boot
          spring-boot-starter-web
      
      
          org.springframework.boot
          spring-boot-starter-test
          test
          
              
                  org.junit.vintage
                  junit-vintage-engine
              
          
      
  


删除无用代码,然后进行配置

标签申明父工程

调整项目结构,使其和springboot项目一致,然后创建启动类:

package com.xxxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer//以注册中心的方式启动
@SpringBootApplication
public class EurakaServerApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(EurakaServerApplication.class);
    }
}

在创建的resources目录下,创建yml配置文件:

server:
  port: 8761

spring:
  application:
    name: euraka-server

eureka:
  instance:
    hostname: localhost #主机名
  client:
    register-with-eureka: false #是否把自己注册到注册中心,默认true,单节点需要关闭,否则报错refuse:connect
    fetch-registry: false #从注册中心获取配置
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

打开http://localhost:8761/看到eureka的界面代表创建成功

至此单节点配置中心创建完成

高可用(多结点)配置

多节点配置中心只需要在父工程下创建一个一模一样的eureka项目即可,只是yml有所区别

server:
  port: 8761

spring:
  application:
    name: euraka-server

eureka:
  instance:
    hostname: eureka01 #主机名
  client:
    service-url:
      defaultZone: http://localhost:8762/eureka/
server:
  port: 8762

spring:
  application:
    name: euraka-server

eureka:
  instance:
    hostname: eureka02 #主机名
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

因为是多节点配置中心,所以需要在启动时将自己注册进去,但是其实是相互注册的过程,子项目直接通过url相互注册,形成闭环,也就形成了一个集群,这里使用不同的端口以及使用localhost是因为这两个集群在同一个机器上,如果在不同机器上,localhost即可改成局域网,端口可以不用改。

打开http://localhost:8761/和http://localhost:8762/都能看到两个注册中心已经被注册

使用ip名称

在instant下面加上:

prefer-ip-address: true #使用ip显示服务名
instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip名称=客户端ip+端口号
搭建服务提供者(service-provider)

创建一个spring项目,pom与注册中心的pom类似,只是需要将server改成client

spring-cloud-starter-netflix-eureka-client

完整的pom如下:(整合了mybatis和swagger)




  4.0.0

  com.xxxx
  service-provider
  
    euraka-demo
    com.xxxx
    1.0-SNAPSHOT
  
  
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
      org.projectlombok
      lombok
      1.18.22
      provided
    
    
      org.springframework.boot
      spring-boot-starter-test
      test
      
        
          org.junit.vintage
          junit-vintage-engine
        
      
    
      
      
      
    
      io.springfox
      springfox-swagger2
      3.0.0
    
    
      io.github.swagger2markup
      swagger2markup
      1.3.3
    
    
      org.springframework
      spring-test
    
    
      org.springframework.boot
      spring-boot-test
    
      
    
      org.testng
      testng
      RELEASE
      compile
    
    
      junit
      junit
      4.13.2
    
      
          org.apache.poi
          poi
          4.0.1
      
    
      org.apache.poi
      poi-ooxml
      4.0.1
    
      
          org.mybatis
          mybatis-spring
          2.0.6
      
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      2.2.0
    
    
    
      mysql
      mysql-connector-java
      runtime
    
    
      com.microsoft.sqlserver
      mssql-jdbc
      runtime
    
    
      mysql
      mysql-connector-java
      runtime
      5.1.41
    
  

所谓服务提供者就是提供各种接口供消费者调用,所以可以直接按照一个springboot 项目来编写,也就是编写controller层,service层,mapper层,提供能使用的接口即可。

启动类:

package com.xxxx;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

//因为配置了Eureka-client所以默认配置了@EnableEurekaClient
@SpringBootApplication
@MapperScan("com.xxxx.mapper")//扫描mapper层,否则会找不到
public class ServiceProviderApplication {
    public static void main(String[] args)
    {
        SpringApplication.run(ServiceProviderApplication.class);
    }
}
补充:RestTemplate用法 带参的get请求
    public static void main(String[] args) {
        String url = "https://dev.citconpay.com/payment/pay";
        // 请求头
        HttpHeaders requestHeaders = new HttpHeaders();
        // OAuth 2.0认证
        requestHeaders.add("Authorization", "5753BC63A2FAC434FFD2");
        //Param 封装url
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)//将参数放到url里面
                .queryParam("amount", "1")
                .queryParam("currency", "USD")
                .queryParam("vendor", "generic")
                .queryParam("reference", "83847328742384247832");
        //HttpEntity
        HttpEntity requestEntity = new HttpEntity(requestHeaders);
        //get
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity responseEntity = restTemplate.exchange(builder.build().encode().toUri(), HttpMethod.GET, requestEntity, String.class);
        System.out.println(responseEntity.getBody());
    }
RestTemplate带参的post请求
    public static void main(String[] args) {
        String url = "https://dev.citconpay.com/payment/pay";
        // 请求头
        HttpHeaders requestHeaders = new HttpHeaders();
        // OAuth 2.0认证
        requestHeaders.add("Authorization", "5753BC63A2FAC434FFD2");
        //body
        MultiValueMap body= new LinkedMultiValueMap<>();//参数放到请求体
        body.add("amount", "1");
        body.add("currency", "USD");
        body.add("vendor", "generic");
        body.add("reference", "83847328742384247832");
        //HttpEntity
        HttpEntity requestEntity = new HttpEntity(body,requestHeaders);
        //get
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);
        System.out.println(responseEntity.getBody());
    }
返回一个实体类的列表
private List selectProductListByDiscoveryClient(){
        StringBuffer sb=null;
        //获取服务列表
        List serviceIds=discoveryClient.getServices();
        if(CollectionUtils.isEmpty(serviceIds))//集合工具类的一个方法
            return null;
        //从service-provider获取服务列表
        List serviceInstances=discoveryClient.getInstances("service-provider");
        ServiceInstance si=serviceInstances.get(0);
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");
        //ResponseEntity 返回的数据
        ResponseEntity> response=restTemplate.exchange(//封装成这个类的列表
           sb.toString(),
           HttpMethod.GET,
                null,
                new ParameterizedTypeReference>(){}
        );
        return response.getBody();
    }
创建服务消费者(service-provider)——DiscoveryClient 创建一个springboot项目

服务提供者和服务消费者相对于注册中心都是client,所以pom文件一致,而yml略有区别

yml:

server:
  port: 9090

spring:
  application:
    name: server-consumer

eureka:
  client:
    register-with-eureka: false #这里创建的是一个纯粹的消费者,所以这里不将其注册到注册中心
    registry-fetch-interval-seconds: 10 #拉取服务列表的时间间隔,默认30s
    service-url:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8761/eureka/

##注册中心
#eureka:
#  instance:
#    prefer-ip-address: true #使用ip显示服务名
#    instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip名称=客户端ip+端口号
#  client:
#    service-url:   #注册中心的url都写上
#      defaultZone: http://localhost:8761/eureka/,http://localhost:8761/eureka/

这里使用的是纯粹的消费者,不提供任何服务,因而不需要把自己注册进注册中心,如果提供服务则使用下面那个配置

编写业务代码 controller
package com.xxxx.controller;

import com.xxxx.bean.Order;
import com.xxxx.service.OrderService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController//@controller+@responsebody
@RequestMapping("/order")
public class OrderController {
    @Resource
    private OrderService orderService;

    @GetMapping("/{id}")//路径参数id
    public Order selectOrderById(@PathVariable("id") int id)//获取路径参数id
    {
        return orderService.selectOrderById(id);
    }
}

这里将参数id放入url,可以用这种写法

bean

product

package com.xxxx.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data   //配置get和set
@NoArgsConstructor  //配置没有参数的构造方法
@AllArgsConstructor //配置包含全部参数的构造方法
public class Product implements Serializable {//序列化,方便io存储
    private int id;
    private String productName;
    private int productNum;
    private Double productPrice;
}

order

package com.xxxx.bean;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order implements Serializable {
    private int id;//主键
    private String orderNo;//请求编号
    private String orderAddress;//请求位置(中国)
    private Double totalPrice;//总价格
    private List productList;//商品列表
}

order表示一次请求的信息

service
package com.xxxx.service;

import com.xxxx.bean.Order;

public interface OrderService {
    Order selectOrderById(int id);//根据主键查询id
}
serviceImpl
package com.xxxx.serviceImpl;

import com.xxxx.bean.Order;
import com.xxxx.bean.Product;
import com.xxxx.service.OrderService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    RestTemplate restTemplate;
    @Resource
    DiscoveryClient discoveryClient;

    @Override
    public Order selectOrderById(int id) {
        return new Order(id,"order-001","中国",31994D,selectProductListByDiscoveryClient());
    }

    private List selectProductListByDiscoveryClient(){
        StringBuffer sb=null;

        //获取所有注册到注册中心的服务列表
        List serviceIds=discoveryClient.getServices();
        if(CollectionUtils.isEmpty(serviceIds))//集合工具类的一个方法
            return null;
        //根据名称为service-provider提供的服务列表
        List serviceInstances=discoveryClient.getInstances("service-provider");
        ServiceInstance si=serviceInstances.get(0);
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");//远程调用
        //ResponseEntity 返回的数据
        ResponseEntity> response=restTemplate.exchange(//封装成这个类的列表
           sb.toString(),
           HttpMethod.GET,
                null,
                new ParameterizedTypeReference>(){}
        );
        return response.getBody();
    }
}

返回的信息包括主键(就直接返回了……),地区(中国),总价格(这里没有计算),商品列表(需要远程调用来获取)

远程调用时:

StringBuffer sb:用于设置路由,线程安全的字符串处理类

List serviceIds :获取服务名称列表,判断是否为空

List serviceInstances:根据服务名称获取提供的服务信息,但其实只是为了获取主机名和端口号

ServiceInstance si:获取主机名和端口号

ResponseEntity response:获取返回的数据,数据在body中

启动类
package com.xxxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//默认开启EnableEurekaClient 以注册中心使用者启动
@SpringBootApplication
public class ServiceConsumerApplication
{
    @Bean//产生一个一个bean并交给spring容器管理,要用的时候采用依赖注入的方式使用
    public  RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main( String[] args )
    {
        SpringApplication.run(ServiceConsumerApplication.class,args);
    }
}

@Bean注解可以向spring提供一个实体类,方便后续注入(其实new一个也可以,这样可能性能更好)

创建服务消费者(service-provider)——loadBalancerClient

在serviceImpl中修改远程调用服务的方式

package com.xxxx.serviceImpl;

import com.xxxx.bean.Order;
import com.xxxx.bean.Product;
import com.xxxx.service.OrderService;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.ParameterizedType;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    RestTemplate restTemplate;
    @Resource
    DiscoveryClient discoveryClient;
    @Resource
    LoadBalancerClient loadBalancerClient;//cloud的负载均衡器

    @Override
    public Order selectOrderById(int id) {
        //return new Order(id,"order-001","中国",31994D,selectProductListByDiscoveryClient());
        return new Order(id,"order-001","中国",31994D,selectProductListByLoadBalancerClient());
    }

    private List selectProductListByDiscoveryClient(){
        StringBuffer sb=null;

        //获取所有注册到注册中心的服务列表
        List serviceIds=discoveryClient.getServices();

        if(CollectionUtils.isEmpty(serviceIds))//集合工具类的一个方法
            return null;
        //根据名称为service-provider提供的服务列表
        List serviceInstances=discoveryClient.getInstances("service-provider");
        ServiceInstance si=serviceInstances.get(0);
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");//远程调用
        //ResponseEntity 返回的数据
        ResponseEntity> response=restTemplate.exchange(//封装成这个类的列表
           sb.toString(),
           HttpMethod.GET,
                null,
                new ParameterizedTypeReference>(){}
        );
        return response.getBody();
    }
    private List selectProductListByLoadBalancerClient(){//使用负载均衡器
        StringBuffer sb=null;
        //根据服务名称获取服务
        ServiceInstance si=loadBalancerClient.choose("service-provider");//获取相关信息
        if(null==si)
            return null;
        sb=new StringBuffer();
        sb.append("http://"+si.getHost()+":"+si.getPort()+"/product/list");
        //ResponseEntity 返回的数据
        ResponseEntity> response=restTemplate.exchange(//封装成这个类的列表
                sb.toString(),
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>(){}
        );
        return response.getBody();
    }
}

LoadBalancerClient loadBalancerClient :使用cloud(ribbon)的负载均衡器,使得代码更加简洁,性能也更好

直接用choose函数获取指定服务的信息,然后拼接url进行调用

创建服务消费者(service-provider)——@LoadBalanced

启动类:

package com.xxxx;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//默认开启EnableEurekaClient 以注册中心使用者启动
@SpringBootApplication
public class ServiceConsumerApplication
{
    @Bean//产生一个一个bean并交给spring容器管理,要用的时候采用依赖注入的方式使用
    @LoadBalanced//负责均衡注解,让RestTemplate拥有负载均衡的能力,但是也失去了原生的能力
    public  RestTemplate restTemplate(){
        return new RestTemplate();
    }
    public static void main( String[] args )
    {
        SpringApplication.run(ServiceConsumerApplication.class,args);
    }
}

在给spring容器中添加RestTemplate类时,加上@LoadBalanced注解,使其获得负载均衡的能力

private List selectProductListByLoadBalancerAnnotation(){
        ResponseEntity> response=restTemplate.exchange(
                "http://service-provider/product/list",
                HttpMethod.GET,
                null,//没有参数
                new ParameterizedTypeReference>() {}
        );
        return response.getBody();
    }

然后再impl类中,代码也会变得极为简单。加上注解后RestTemplate类的方法被重写,url的第一项应当被设置为服务的名称(serviceId),后面接上请求的路由,即可直接调用服务。

加上注解后,原来的url功能发生了改变,因为前两种方法不能用这里spring容器中的RestTemplate类对象,可以使用自己new出来的对象。

CAP原则

C:Consistency 一致性,指多个数据源时,请求返回的数据应当是一致的

A:Availability 可用性,指快速响应用户的请求

P:Partition-Tolerance 分区容错性,指对数据源进行分布式扩展和部署

CAP原则是这三者不可兼得,只能满足其中的两个优点。大致原因是如果一个应用既满足的分布式P,又要保证数据的一致性,就要在多个数据源之间进行一致性处理,这样势必会花费一定的时间而降低可用性(用户体验)

CA:不采用分布式,一般是小型的单节点应用,后期不会进行扩展,只使用一个数据源也就避免了一致性处理。

CP:需要数据高度一致的大型应用,例如银行转账应用,数据一致性要求大于用户的体验。

AP:对一致性要求没有那么高的应用,在请求数据的时候不做一致性处理,在后面再做容错处理。(例如秒杀系统,可以先让用户加入购物车,哪怕此时已经没有商品,在结账的时候再给出响应提示)

Eureka安全性保护

微服务默认每30s向注册中心发生一次心跳包来检测微服务是否正常,如果90s都未收到心跳包,并且没有开启安全保护,注册中心就会移除这个微服务,但是有时是因为网络波动而没能及时收到心跳包,这时候删除微服务是不正确的,所以有了安全保护。

Eureka默认开启安全保护,开启安全保护时,如果没能收到心态,注册中心不会将其删除,而实保留下来并给出一行红色的提示信息,提示你检测微服务是否正常运作。

如果向关闭安全性保护,则需要在yml中添加如下配置文件:

eureka:
  server:
    enable-self-preservation: false #关闭安全性保护
    eviction-interval-timer-in-ms: 60000 #每6s清理一次

和之前的合在一起即为:

server:
  port: 8761

spring:
  application:
    name: euraka-server

eureka:
  server:
    enable-self-preservation: false #关闭安全性保护
    eviction-interval-timer-in-ms: 60000 #每6s清理一次
  instance:
    hostname: eureka01 #主机名
    prefer-ip-address: true #使用ip显示服务名
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip名称=客户端ip+端口号
  client:
    service-url:
      defaultZone: http://localhost:8762/eureka/

此时会提示错误信息:

THE SELF PRESERVATION MODE IS TURNED OFF. THIS MAY NOT PROTECT INSTANCE EXPIRY IN CASE OF NETWORK/OTHER PROBLEMS.

表示此时安全性检测已经被关闭

优雅停服

所谓优雅停服就是同时终止服务并从注册中心中删除这个服务

在pom中添加依赖:


     org.springframework.boot
     spring-boot-starter-actuator

actuator用于安全检测,这里用actuator进行优雅停服

在服务提供者(service-provider)的yml中添加如下配置:

#度量健康指标与健康检测
management:
  endpoints:
    web:
      exposure:
        include: shutdown #开启 shutdown 端点访问
  endpoint:
    shutdown:
      enabled: true       #开启shutdown优雅停服

与前面合起来:

server:
  port: 7070

spring:
  application:
    name: service-provider
  datasource:
    name: document
    url: jdbc:mysql://localhost:3306/document
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver

mybatis:
  mapper-locations: classpath:mapper
    }

这样对于所有服务都开启日志

局部:

feign:	
  client:
    config:
      service-provider:		#对特定服务开启日志
        loggerLevel: FULL

service-provider指出需要开启日志的服务。

最后的日志文件会保存在:

也可以通过设置catalina.base设置日志的保存位置:

这样就能简单地查看服务调用的情况,后续还有更直观的方法:链路追踪

超时时间

在处理不同的请求时,需要有不同的超时时间,Feign对于所有的请求的超时时间都默认是1s,但是有些耗时的服务可能需要更多的时间,所以我们需要配置它的超时时间。

我们在服务提供者中设置一下,模拟长时间的请求:

	@Override
    public Product selectProductbyID(int id) {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Product(id,"冰箱",1,166D);
    }

Thread.sleep(2000); 让当前线程睡眠两秒

如果不配置超时时间就会报500:Read Timeout

全局

为所有的服务都设置相同的超时时间

ribbon:
  ConnectTimeout: 5000 #请求连接的超时时间 默认为为1s
  ReadTimeout: 5000 #请求处理的超时时间
局部

但实际上我们需要根据不同服务的需要设置不同的超时时间,所以我们在给定服务名称的配置下设置超时时间即可

#指定服务的配置
service-provider: #服务提供者的名称
  ribbon: #选择这个服务提供者的负载均衡策略
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载均衡策略对应的类名
    OkToRetryOnAllOperations: true     #对所有超时请求开启重试
    MaxAutoRetries: 2                  #对当前实例的重试次数
    MaxAutoRetriesNextServer: 0        #切换实例的重试次数
    ConnectTimeout: 3000               #请求连接的超时时间 默认为为1s
    ReadTimeout: 3000                  #请求处理的超时时间

这样就可以简单的处理超时问题,后续可以用服务熔点和降级来处理

Hystrix服务容错 前言

Hystrix是netflix公式使用的一款开源产品,可以用于在微服务错综复杂调用关系中,提供服务熔断,服务降级,服务隔离等服务,为某个服务不可用时,提供一个备选方案,避免雪崩效应。

雪崩效应

现实当中服务的调用是错综复杂的,如果一个服务不可用时,可能会造成严重的后果。

当服务运行正常时,是这样:

当服务R出现问题时:

服务R不可用就会导致服务N不可用,服务N不可用就会导致服务H不可用,服务H不可用时,请求服务H的客户端就会阻塞在服务器里面,当有大量客户端请求这个服务H时,所有的客户端都会被阻塞在这里,服务器的线程资源很快就会被耗尽,进而导致服务器崩溃,然后进一步导致整个微服务系统不可用。这一连串的连锁反应就叫雪崩效应。

雪崩效应解决方案

我们无法从源头上杜绝雪崩效应的发生,但是可以设计好容错的逻辑,减少服务之间的强依赖,避免错误的大量传播。

  • 请求缓存:将一个请求和返回值做缓存处理,如果一个请求所得到的值长时间不发生变化,我们可以将这个值放入缓存,请求时直接从缓存中拿到返回值,从而降低服务之间的依赖。
  • 请求合并:将多个请求合并为一次请求,例如将多个根据主键查询的请求,合并为一次查询列表的请求。
  • 服务隔离:限制每个分布式服务的资源,当一个服务的请求个数过多时,不会占用其他服务的资源,这样可以当一个服务出现雪崩时,大量线程阻塞,其他服务也能够正常运行,防止雪崩的扩散,同时也能保障请求少的服务能够快速响应(不然我偶尔请求一次还让我等那么久也太不公平了)。
  • 服务熔断:牺牲局部的服务,保全整个系统的稳定性,当一个服务出现雪崩时,可以关闭该服务,防止雪崩扩散。
  • 服务降级:服务熔断后,返回一个能快速得到的缺省值,避免客户端那么没有响应。
环境搭建

创建两个eureka注册中心

product-service :提供查询商品列表,根据主键查询商品,根据多个主键查询商品

order-service-rest:调用product-service中的服务,同时自身也可能提供一些服务

模拟高并发环境

使用JMeter来模拟高并发的环境。

下载第三方的一个依赖包Apache JMeter - Download Apache JMeter

解压后,打开bin目录下的jmeter.bat,我们发现里面是英文,不方便我们使用,所以我们需要修改它的配置文件。

打开同级目录下的jmeter.properties,进行以下修改:

把语言改成zh_CN,即改成中文

language=zh_CN

sampleresult.default.encoding=UTF-8

把编码字符集改成UTF-8

然后运行jmeter.bat

文件->添加->线程->线程组

线程组->取样器->http请求

http请求->添加->监听器->查看结果树

设置线程组,将请求设置为50✖50,也就是2500个请求

设置请求的url(ip,端口,路径)

然后点击运行即可模拟出一个高并发的场景

请求缓存 前言

Hystrix自带缓存有以下问题:

缓存保存在本地,无法和集群同步

不支持第三方缓存,例如redis,MemCache

下面使用spring的缓存方案,Nosql用redis来实现,redis使用5.0.7

redis其实是一个用于第三方缓存的服务器,可以通过http协议向这个服务器发送请求,服务器提供缓存服务。

下载redis

参考博客:(29条消息) windows下Redis的安装和配置–图文教程_Zepal-CSDN博客_redis安装配置

添加pom依赖
    
      org.springframework.boot
      spring-boot-starter-data-redis
    
    
      org.apache.commons
      commons-pool2
    
添加yml配置

在服务消费者的yml文件添加如下依赖:

spring:
  application:
    name: order-service-rest
  redis:
    timeout: 10000         #超时时间:10s
    host: localhost        #redis服务器地址
    port: 6379             #redis服务器端口
    password: 123456         #redis服务器密码
    database: 0            #选择哪个库
    lettuce:
      pool:
        max-active: 1024   #最大连接数
        max-wait: 10000    #最大连接阻塞等待时间,单位ms,默认-1
        max-idle: 200      #最大空闲连接,默认8
        min-idle: 5        #最小空闲连接 ,默认0

完整的yml文件:

server:
  port: 9090 #端口
  compression:
    enabled: true #开启压缩
    #mime-types: application/json,application/xml,text/html,text/xml,text/plain #压缩类型
  tomcat:
    max-threads: 10 #设置最大线程数,为了模拟高并发才加上的,实际使用不用设置

spring:
  application:
    name: order-service-rest
  redis:
    timeout: 10000         #超时时间:10s
    host: localhost        #redis服务器地址
    port: 6379             #redis服务器端口
    password: 123456         #redis服务器密码
    database: 0            #选择哪个库
    lettuce:
      pool:
        max-active: 1024   #最大连接数
        max-wait: 10000    #最大连接阻塞等待时间,单位ms,默认-1
        max-idle: 200      #最大空闲连接,默认8
        min-idle: 5        #最小空闲连接 ,默认0

#注册中心
eureka:
  instance:
    prefer-ip-address: true #使用ip显示服务名
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #ip名称=客户端ip+端口号
  client:
    service-url:   #注册中心的url都写上
      defaultZone: http://root:123456@localhost:8761/eureka/,http://root:123456@localhost:8761/eureka/

#指定服务的配置
service-provider: #服务提供者的名称
  ribbon: #选择这个服务提供者的负载均衡策略
    NFLoadBalancerRuleClassName: com.netflix.loadbalancer.RandomRule #负载均衡策略对应的类名
    OkToRetryOnAllOperations: true     #对所有超时请求开启重试
    MaxAutoRetries: 2                  #对当前实例的重试次数
    MaxAutoRetriesNextServer: 0        #切换实例的重试次数
    ConnectTimeout: 3000               #请求连接的超时时间 默认为为1s
    ReadTimeout: 3000                  #请求处理的超时时间

feign:
  httpclient: #开启httpclient,http连接池
    enabled: true

#ribbon:
#  ConnectTimeout: 5000 #请求连接的超时时间 默认为为1s
#  ReadTimeout: 5000 #请求处理的超时时间

添加配置类

直接使用jdk自带的序列化对空间的消耗很大,是json的5倍,所以我们编写一个配置类,重写序列化方法

package com.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;

@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory)
    {
        RedisTemplate template=new RedisTemplate<>();
        //为String 类型的key设置序列化器
        template.setKeySerializer(new StringRedisSerializer());
        //为String 类型的value设置序列化器
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        //为Hash类型key设置序列化器
        template.setHashKeySerializer(new StringRedisSerializer());
        //为hash类型设置序列化器
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }
    //重写Cache序列化
    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate)
    {
        RedisConnectionFactory connectionFactory;
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory());
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30))   //设置过期时间
                //key和value的序列化
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getKeySerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()));
        return new RedisCacheManager(redisCacheWriter,redisCacheConfiguration);

    }
}
启动类

启动类上加上这个注解,表示开启缓存服务

@EnableCaching //开启缓存
业务实现类
package com.demo.serviceImpl;

import com.demo.bean.Product;
import com.demo.service.ProductService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    RestTemplate restTemplate;

    @Cacheable(cacheNames = "orderService:product:list")
    @Override
    public List selectProductList() {
        return restTemplate.exchange(
                "http://product-service/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>() {
                }
        ).getBody();
    }

    @Override
    public List selectProductListByIds(List ids) {
        StringBuffer sb=new StringBuffer();
        ids.forEach(id->sb.append("id="+id+"&"));
        return restTemplate.getForObject("http://product-service/product/listByIds?"+sb.toString(), ArrayList.class);
    }

    @Cacheable(cacheNames = "orderService:product:single",key = "#id") //id即为下面传入的参数
    @Override
    public Product selectProductById(Integer id) {
        return restTemplate.getForObject("http://product-service/product/"+id,Product.class);
    }
}

@Cacheable(cacheNames = “orderService:product:list”)

@Cacheable(cacheNames = “orderService:product:single”,key = “#id”) //id即为下面传入的参数

Redis的Cache是一个键值对,cacheNames和key共同决定键值对的名称,:是一个序列化的分隔符,:后面的名称相当于是前面名称的子属性,后面的key会作为最后一个子属性,在调用这个方法前,会先根据键值对的名称在缓存中查找是否已经有存在的记录,如果有,则返回缓存中对应的值,如果没有,则调用方法进行远程调用,调用结束后会创建键值对将返回值值存入缓存。

调用后:

可见缓存中已经有了对应的值

因为我们模拟了高并发环境,调用服务时会睡眠2s,所以第一次调用服务会比较慢(大于2s),后面调用起来就会很慢(小于1s),这是因为第一次将值存入了缓存,后面的值直接从缓存中取得。

这里演示的是GET方法的请求,但其实用POST方法也可以缓存,因为注解中的key来自调用时传入的参数,至于使用POST还是GET取决于方法体,与缓存无关。

使用缓存可以减少一些不必的远程调用,从而提高系统的整体性能。

请求合并

请求合并就是将一堆请求收集起来,等到达了某个阈值后批量处理,好处是提高了系统的吞吐量,减少了线程阻塞而大量消耗的线程资源,缺点是请求会有延迟,因为要等到达到阈值后才能响应。相当于为请求开辟了一个缓冲区。

请求合并需要一定的等待时间,如果系统要求请求要快速响应,则不能用请求合并。如果请求本身本身延迟就很高,那么请求合并所等待的时间就显得微不足道了(例如每10ms将这个时间端的请求批量处理,会有一定的延迟,但是如果请求本身就要好几秒,等待的时间就不那么重要了),另外高并发也是请求合并应用的重要场景。

服务消费者添加pom依赖:

    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    
添加注解
package com.demo.serviceImpl;

import com.demo.bean.Product;
import com.demo.service.ProductService;
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 org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    RestTemplate restTemplate;

    @Cacheable(cacheNames = "orderService:product:list")
    @Override
    public List selectProductList() {
        return restTemplate.exchange(
                "http://product-service/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>() {
                }
        ).getBody();
    }

    @HystrixCommand //声明是一个服务容错的方法
    @Override
    public List selectProductListByIds(List ids) {
        System.out.println("----selectProductListByIds---被调用----");//看看请求合并是否生效
        StringBuffer sb=new StringBuffer();
        ids.forEach(id->sb.append("id="+id+"&"));
        return restTemplate.getForObject("http://product-service/product/listByIds?"+sb.toString(), ArrayList.class);
    }

    @HystrixCollapser(batchMethod = "selectProductListByIds",//合并后用于批处理的方法
            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求方式
            collapserProperties = {//批处理的时间阈值和数量阈值,两个只要有一个达到就会进行一次批处理
                //间隔多久进行一次批处理,默认是10ms
                @HystrixProperty(name = "timerDelayInMilliseconds",value = "20") ,
                //批处理中允许的最大请求数
                @HystrixProperty(name = "maxRequestsInBatch",value = "200")
            }
    )
    @Cacheable(cacheNames = "orderService:product:single",key = "#id") //id即为下面传入的参数
    @Override//Future是异步,将批量处理后得到的结果与请求一一对应
    public Future selectProductById(Integer id) {
        System.out.println("----selectProductById---被调用----");//看看请求合并是否生效
        return null;//请求会进行合并后调用上面的方法,所以实际上没有使用这个方法,直接返回null即可
    }
}

@HystrixCommand` 用于声明这是一个用于请求合并(服务容错)的方法

@HystrixCollapser(batchMethod = “selectProductListByIds”,//合并后用于批处理的方法
scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求方式
collapserProperties = {//批处理的时间阈值和数量阈值,两个只要有一个达到就会进行一次批处理
//间隔多久进行一次批处理,默认是10ms
@HystrixProperty(name = “timerDelayInMilliseconds”,value = “20”) ,
//批处理中允许的最大请求数
@HystrixProperty(name = “maxRequestsInBatch”,value = “200”)
}
)

用于申明需要合并的方法,其中的参数:

batchMethod 用于申明用哪个方法将进行合并

scope 这个用于指定请求方式

collapserProperties 用于添加配置

@HystrixProperty(name = “timerDelayInMilliseconds”,value = “20”) 用于设置最大间隔时间

@HystrixProperty(name = “maxRequestsInBatch”,value = “200”) 用于设置最多等待线程

因为这是一个异步的请求,所以需要用到Future进行异步处理

在启动类中添加注解
package com.demo;

import feign.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//默认开启EnableEurekaClient 以注册中心使用者启动
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker //开启熔断器,也可以使用EnableHystrix
//@EnableCaching //开启缓存注解
public class OrderServiceApplication
{
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
//    @Bean
//    public RandomRule randomRule() {return new RandomRule();}
    @Bean
    public Logger.Level getLog(){//日志对象输出
        return Logger.Level.NONE;
        
    }
    public static void main( String[] args )
    {
        SpringApplication.run(OrderServiceApplication.class,args);
    }
}

@EnableCircuitBreaker 开启熔断器,也可以使用EnableHystrix

模拟多个请求
	@Override
    public Order searchOrderById(int id) {
        Future p1=productService.selectProductById(1);
        Future p2=productService.selectProductById(2);
        Future p3=productService.selectProductById(3);
        Future p4=productService.selectProductById(4);
        Future p5=productService.selectProductById(5);
        try {
            System.out.println(p1.get());//get方法拿到数据,返回值是hashmap
            System.out.println(p2.get());
            System.out.println(p3.get());
            System.out.println(p4.get());
            System.out.println(p5.get());
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();//重新设置中断信号
            e.printStackTrace();
        }
        return new Order(id,"order-003","中国",29000D,null);
    }

一次调用五个请求,判断请求合并是否成功。

线程隔离 前言

线程隔离分为信号量隔离和线程池隔离,线程池隔离适用于绝大多数情况,但是如果隔离的实例太多,进程上下文之间的切换较大,此时可以采用信号量隔离,而信号量隔离是设置一个互斥使用的信号量,如果由信号量则取走一个信号量并在线程运行完后归还,如果没有信号量则将线程阻塞,并设置一定的超时时间。相当于调用方的线程进入线程池后,再进入服务独有的线程池,因而提高服务的线程和调用方的线程是两个不同的线程,因而是异步的方法,这这两个线程切换时,可以做超时处理。

线程池隔离

一开始这两个接口使用同一个线程池(从他们前缀可以看出,并且使用相同的计数规则)

在调用服务的业务实现类中添加注解,将查询商品列表的服务和根据主键查询商品的服务所使用的线程池分开,为这俩个服务分配可以使用的线程,例如一共有10个线程,为查询商品列表的分配6个线程,为根据主键查询商品分配3个线程,还有1个线程用于调用其他服务,通过这样合理地分配资源,防止服务之间相互干扰,从而达到服务隔离地目的。

业务实现类
package com.demo.serviceImpl;

import com.demo.bean.Product;
import com.demo.service.ProductService;
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 org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    RestTemplate restTemplate;

    @HystrixCommand(groupKey = "order-productService-listPool",//服务名称,使用相同名称的服务使用同一个线程池
            commandKey = "selectProductList", //接口名称,默认为方法名
            threadPoolKey = "order-productService-listPool",//线程池名称,相同名称的服务使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"
                    )
            },
            threadPoolProperties = {
                    //线程池大小(设置的总数量为10)
                    @HystrixProperty(name = "coreSize",value = "6"),
                    //队列等待的阈值(最大队列长度),默认为-1
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略,value和上面的maxQueueSize的value保持一致
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            }, fallbackMethod = "selectProductListFallback"  //使用托底方法返回的数据
    )
    @Cacheable(cacheNames = "orderService:product:list")
    @Override
    public List selectProductList() {
        System.out.println(Thread.currentThread().getName()+"调用---ProductService---selectProductList---");
        return restTemplate.exchange(
                "http://product-service/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>() {
                }
        ).getBody();
    }

    //托底方法,参数和返回值类型要与原方法一致
    private List selectProductListFallback(){
        System.out.println("---托底数据的方法:selectProductListFallback---");
        return Arrays.asList(
                new Product(1,"托底数据-华为手机",1,5800D),
                new Product(2,"托底数据-联想笔记本",1,6888D),
                new Product(3,"托底数据-小米平板",5,2020D)
        );

    }

    //@HystrixCommand //声明是一个服务容错的方法
    @Override
    public List selectProductListByIds(List ids) {
        System.out.println("----selectProductListByIds---被调用----");//看看请求合并是否生效
        StringBuffer sb=new StringBuffer();
        ids.forEach(id->sb.append("id="+id+"&"));
        return restTemplate.getForObject("http://product-service/product/listByIds?"+sb.toString(), ArrayList.class);
    }

//    @HystrixCollapser(batchMethod = "selectProductListByIds",//合并后用于批处理的方法
//            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求方式
//            collapserProperties = {//批处理的时间阈值和数量阈值,两个只要有一个达到就会进行一次批处理
//                //间隔多久进行一次批处理,默认是10ms
//                @HystrixProperty(name = "timerDelayInMilliseconds",value = "20") ,
//                //批处理中允许的最大请求数
//                @HystrixProperty(name = "maxRequestsInBatch",value = "200")
//            }
//    )

    @HystrixCommand(groupKey = "order-productService-singlePool",   //服务名称,相同名称使用同一个线程池
            commandKey = "selectProductById",//接口名称,默认为方法名
            threadPoolKey = "order-productService-singlePool",      //线程池名称,相同名称使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"
                    )
            },
            threadPoolProperties = {
                    //线程池大小
                    @HystrixProperty(name = "coreSize",value = "3"),
                    //队列等待阈值(最大队列长度,最大等待的线程数,默认为-1)
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            }//不写fallback就使用系统默认的fallback
    )
    @Cacheable(cacheNames = "orderService:product:single",key = "#id") //id即为下面传入的参数
    @Override
    public Product selectProductById(Integer id) {
        System.out.println(Thread.currentThread().getName()+"调用---ProductService---selectProductById---");
        return restTemplate.getForObject("http://product-service/product/"+id,Product.class);
    }
}

相比之前添加了用于线程隔离的注解:

@HystrixCommand(groupKey = "order-productService-listPool",//服务名称,使用相同名称的服务使用同一个线程池
            commandKey = "selectProductList", //接口名称,默认为方法名
            threadPoolKey = "order-productService-listPool",//线程池名称,相同名称的服务使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"
                    )
            },
            threadPoolProperties = {
                    //线程池大小(设置的总数量为10)
                    @HystrixProperty(name = "coreSize",value = "6"),
                    //队列等待的阈值(最大队列长度),默认为-1
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略,value和上面的maxQueueSize的value保持一致
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            }, fallbackMethod = "selectProductListFallback"  //使用托底方法返回的数据
    )

t哦那个给设置等待队列长度和线程池大小,可以达到一个限流的目的,防止过多的请求使得服务器崩溃

selectProductListFallback为托底方法,在等待队列满了之后剩下的请求会执行这个方法:

	//托底方法
    private List selectProductListFallback(){
        System.out.println("---托底数据的方法:selectProductListFallback---");
        return Arrays.asList(
                new Product(1,"托底数据-华为手机",1,5800D),
                new Product(2,"托底数据-联想笔记本",1,6888D),
                new Product(3,"托底数据-小米平板",5,2020D)
        );
    }

线程池的参数如下:

最后记得在启动类中加上注解@EnableCircuitBreaker 或者@EnableHystrix,来启用Hystrix:

package com.demo;

import feign.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

//默认开启EnableEurekaClient 以注册中心使用者启动
@SpringBootApplication
@EnableFeignClients
@EnableCircuitBreaker //开启熔断器,也可以使用EnableHystrix
//@EnableCaching //开启缓存注解
public class OrderServiceApplication
{
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
//    @Bean
//    public RandomRule randomRule() {return new RandomRule();}
    @Bean
    public Logger.Level getLog(){//日志对象输出
        return Logger.Level.NONE;
        
    }
    public static void main( String[] args )
    {
        SpringApplication.run(OrderServiceApplication.class,args);
    }
}
信号量隔离

线程池隔离会为每个线程创建一个新的线程实例,相当于情况开始和结束时会有两个不同的线程,因而它是异步的隔离方法,当实例很多时(上千个),需要进行很多的线程上下文的切换,会耗费很多时间。而信号量隔离则可以解决这个问题,信号量隔离竞争的是信号量而不是线程,拿到一个信号量后才能进行线程的运行,节省了许多上下文切换的时间。信号量隔离是同步的隔离方法,因为竞争的是信号量,而不是线程,所以只有一个线程,所以不需要做线程切换和异步处理。

pom依赖:信号量隔离与线程池隔离都在hystrix依赖中:


    
      org.springframework.cloud
      spring-cloud-starter-netflix-hystrix
    

然后更改业务实现类的注解即可:

package com.demo.serviceImpl;

import com.demo.bean.Product;
import com.demo.service.ProductService;
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.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Future;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    RestTemplate restTemplate;

    //信号量隔离
    @HystrixCommand(commandProperties = {
            //超时时间,默认为1000ms
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds"
                    ,value = "5000"),
            //信号量隔离
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY,value = "SEMAPHORE"),
            //信号量最大并发量,调小一定便于模拟高并发
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS,
            value = "6")
    },fallbackMethod = "selectProductListFallback")//没有设置等待队列的长度(默认-1),所以超过最大并发时会直接调用拖地方法返回,可以达到一个限流的目的
    @Cacheable(cacheNames = "orderService:product:list")
    @Override
    public List selectProductList() {
        System.out.println(Thread.currentThread().getName()+"调用---ProductService---selectProductList---");
        return restTemplate.exchange(
                "http://product-service/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>() {
                }
        ).getBody();
    }

    //托底方法
    private List selectProductListFallback(){
        System.out.println("---托底数据的方法:selectProductListFallback---");
        return Arrays.asList(
                new Product(1,"托底数据-华为手机",1,5800D),
                new Product(2,"托底数据-联想笔记本",1,6888D),
                new Product(3,"托底数据-小米平板",5,2020D)
        );
    }

    //@HystrixCommand //声明是一个服务容错的方法
    @Override
    public List selectProductListByIds(List ids) {
        System.out.println("----selectProductListByIds---被调用----");//看看请求合并是否生效
        StringBuffer sb=new StringBuffer();
        ids.forEach(id->sb.append("id="+id+"&"));
        return restTemplate.getForObject("http://product-service/product/listByIds?"+sb.toString(), ArrayList.class);
    }

//    @HystrixCollapser(batchMethod = "selectProductListByIds",//合并后用于批处理的方法
//            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求方式
//            collapserProperties = {//批处理的时间阈值和数量阈值,两个只要有一个达到就会进行一次批处理
//                //间隔多久进行一次批处理,默认是10ms
//                @HystrixProperty(name = "timerDelayInMilliseconds",value = "20") ,
//                //批处理中允许的最大请求数
//                @HystrixProperty(name = "maxRequestsInBatch",value = "200")
//            }
//    )

    @HystrixCommand(groupKey = "order-productService-singlePool",   //服务名称,相同名称使用同一个线程池
            commandKey = "selectProductById",//接口名称,默认为方法名
            threadPoolKey = "order-productService-singlePool",      //线程池名称,相同名称使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"
                    )
            },
            threadPoolProperties = {
                    //线程池大小
                    @HystrixProperty(name = "coreSize",value = "3"),
                    //队列等待阈值(最大队列长度,最大等待的线程数,默认为-1)
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            }//不写fallback就使用系统默认的fallback
    )
    @Cacheable(cacheNames = "orderService:product:single",key = "#id") //id即为下面传入的参数
    @Override
    public Product selectProductById(Integer id) {
        System.out.println(Thread.currentThread().getName()+"调用---ProductService---selectProductById---");
        return restTemplate.getForObject("http://product-service/product/"+id,Product.class);
    }
}

核心部分为:

//信号量隔离
    @HystrixCommand(commandProperties = {
            //超时时间,默认为1000ms
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds"
                    ,value = "5000"),
            //信号量隔离
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY,value = "SEMAPHORE"),
            //信号量最大并发量,调小一定便于模拟高并发
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS,
            value = "6")
    },fallbackMethod = "selectProductListFallback")//没有设置等待队列的长度(默认-1),所以超过最大并发时会直接调用拖地方法返回,可以达到一个限流的目的

看到以下结果代表配置成功:

前六个线程成功拿到信号量,开始运行业务,而后面没有拿到信号量的则直接执行托底方法(因为没有配置等待队列的长度,配置方法和前面的相同),虽然只有6个信号量,但是线程id是110而不是16,这是因为信号量隔离时,各个线程使用的仍然是同一个线程池(容量为10),因而信号量隔离是同步的隔离方法。

信号量隔离vs线程池隔离

但其实绝大部分情况都使用线程池隔离即可,一般不再调用其他请求访问其他资源时,使用信号量隔离即可。

信号量的调用是同步的,也就是说,每次调用都得阻塞调用方的线程,直到结果返回。这样就致使了没法对访问作超时(只能依靠调用协议超时,没法主动释放),而线程池隔离是异步的,可以检测超时时间。

服务熔断

服务熔断相当于电闸的跳闸功能,当系统出现大量异常时,为了保全整个系统正常运行而会关闭一些服务(使它们执行托底方法)

当每过10s检测一次系统状态,如果10s中出现的异常大于所有请求的50%,则让这个服务进入服务熔断状态,在接下来的5s内让所有请求都去执行托底方法,5s过后,然后再允许请求去尝试请求服务,如果请求成功则关闭服务熔断,如果请求失败则继续服务熔断,5s后再次尝试。

服务熔断为系统提供了最基本的保护,而前面的服务隔离则是为服务提供第二层保护,前者通过统计请求数和异常数量,后者通过提供的线程来进行限流,防止出现过载。

服务降级

其实服务降级在前面已经用到,其实就是当服务处于异常情况时,让服务fallback,返回托底数据,以保证系统的稳定性。

出发服务降级的条件如下:

  • 抛出非HystrixBadRequestException的异常,例如空指针异常,数据库异常等等
  • 方法调用超时
  • 熔断器开始拦截
  • 线程池/信号量/等待队列跑满

在出现这些情况时,则会直接使得方法fallback,在保证系统安全的同时,让请求可以得到回应

结合了服务熔断,服务隔离,还有超时时间,这些都在@HystrixCommand中进行设置

package com.demo.serviceImpl;

import com.demo.bean.Product;
import com.demo.bean.Status;
import com.demo.service.ProductService;
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.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.Future;

@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    RestTemplate restTemplate;

    //信号量隔离
    @HystrixCommand(commandProperties = {
            //超时时间,默认为1000ms
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds"
                    ,value = "5000"),
            //信号量隔离
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_STRATEGY,value = "SEMAPHORE"),
            //信号量最大并发量,调小一定便于模拟高并发
            @HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_SEMAPHORE_MAX_CONCURRENT_REQUESTS,
            value = "6")
    },fallbackMethod = "selectProductListFallback")//没有设置等待队列的长度(默认-1),所以超过最大并发时会直接调用拖地方法返回,可以达到一个限流的目的
    @Cacheable(cacheNames = "orderService:product:list")
    @Override
    public List selectProductList() {//查询商品列表
        System.out.println(Thread.currentThread().getName()+"调用---ProductService---selectProductList---");
        return restTemplate.exchange(
                "http://product-service/product/list",
                HttpMethod.GET,
                null,
                new ParameterizedTypeReference>() {
                }
        ).getBody();
    }

    //托底方法
    private List selectProductListFallback(){
        System.out.println("---托底数据的方法:selectProductListFallback---");
        return Arrays.asList(
                new Product(1,"托底数据-华为手机",1,5800D),
                new Product(2,"托底数据-联想笔记本",1,6888D),
                new Product(3,"托底数据-小米平板",5,2020D)
        );
    }

    //@HystrixCommand //声明是一个服务容错的方法
    @Override
    public List selectProductListByIds(List ids) {
        System.out.println("----selectProductListByIds---被调用----");//看看请求合并是否生效
        StringBuffer sb=new StringBuffer();
        ids.forEach(id->sb.append("id="+id+"&"));
        return restTemplate.getForObject("http://product-service/product/listByIds?"+sb.toString(), ArrayList.class);
    }

//    @HystrixCollapser(batchMethod = "selectProductListByIds",//合并后用于批处理的方法
//            scope = com.netflix.hystrix.HystrixCollapser.Scope.GLOBAL,//请求方式
//            collapserProperties = {//批处理的时间阈值和数量阈值,两个只要有一个达到就会进行一次批处理
//                //间隔多久进行一次批处理,默认是10ms
//                @HystrixProperty(name = "timerDelayInMilliseconds",value = "20") ,
//                //批处理中允许的最大请求数
//                @HystrixProperty(name = "maxRequestsInBatch",value = "200")
//            }
//    )

    @HystrixCommand(
            //线程池隔离
            groupKey = "order-productService-singlePool",   //服务名称,相同名称使用同一个线程池
            commandKey = "selectProductById",//接口名称,默认为方法名
            threadPoolKey = "order-productService-singlePool",      //线程池名称,相同名称使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"),
                    //10s内请求的数量大于这个值(10个)后会启动服务熔断,默认20个
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,
                            value = "10"),
                    //请求的错误率阈值,如果请求错误的个数占总请求的50%(设置的值),则会启动服务熔断
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,
                            value = "50"),
                    //每隔多少秒重试一次,这里设置的是5s
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,
                            value = "5000")
            },
            threadPoolProperties = {
                    //线程池大小
                    @HystrixProperty(name = "coreSize",value = "3"),
                    //队列等待阈值(最大队列长度,最大等待的线程数,默认为-1)
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            },fallbackMethod = "selectedProductByIdFallback"
    )
    @Cacheable(cacheNames = "orderService:product:single",key = "#id") //id即为下面传入的参数
    @Override
    public Product selectProductById(Integer id) {//根据主键查询id
        System.out.println(Thread.currentThread().getName()+"---selectProductById---"+
                LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
        //人为抛出异常
        if(id==1)
        {
            throw new RuntimeException("查询主键为1的商品信息导致异常");
        }
        return restTemplate.getForObject("http://product-service/product/"+id,Product.class);
    }

    
    private Product selectedProductByIdFallback(Integer id)
    {
        return new Product(id,"根据主键查找-托底数据",1,2666D);
    }

    //托底方法
    private LinkedHashMap consumeProductFallback(Integer id)
    {
        LinkedHashMap result=new LinkedHashMap();
        System.out.println("---调用托底方法---");
        result.put("status", Status.ERROR);
        result.put("product",new Product());
        result.put("msg","请求失败稍后重试");
        return result;
    }


    @HystrixCommand(groupKey = "order-productConsumeService-listPool",//服务名称,使用相同名称的服务使用同一个线程池
            commandKey = "consumeProductById", //接口名称,默认为方法名
            threadPoolKey = "order-productConsumeService-listPool",//线程池名称,相同名称的服务使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"
                    )
            },
            threadPoolProperties = {
                    //线程池大小(设置的总数量为10)
                    @HystrixProperty(name = "coreSize",value = "50"),
                    //队列等待的阈值(最大队列长度),默认为-1
                    @HystrixProperty(name = "maxQueueSize",value = "200"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略,value和上面的maxQueueSize的value保持一致
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            }, fallbackMethod = "consumeProductFallback"  //使用托底方法返回的数据
    )
    @Override
    public LinkedHashMap consumeProductById(Integer id) {
        MultiValueMap paramMap = new LinkedMultiValueMap();
        paramMap.add("id", id);
        HttpEntity> httpEntity = new HttpEntity>(paramMap,new HttpHeaders());
        return restTemplate.exchange(
                "http://product-service/product/consume",
                HttpMethod.POST,
                httpEntity,
                LinkedHashMap.class
        ).getBody();
    }
}

整合后的注解:

    @HystrixCommand(
            //线程池隔离
            groupKey = "order-productService-singlePool",   //服务名称,相同名称使用同一个线程池
            commandKey = "selectProductById",//接口名称,默认为方法名
            threadPoolKey = "order-productService-singlePool",      //线程池名称,相同名称使用同一个线程池
            commandProperties = {
                    //超时时间,默认1000ms
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "5000"),
                	//服务熔断
                    //10s内请求的数量大于这个值(10个)后会启动服务熔断,默认20个
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD,
                            value = "10"),
                    //请求的错误率阈值,如果请求错误的个数占总请求的50%(设置的值),则会启动服务熔断
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE,
                            value = "50"),
                    //每隔多少秒重试一次,这里设置的是5s
                    @HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS,
                            value = "5000")
            },
            threadPoolProperties = {
                    //线程池大小
                    @HystrixProperty(name = "coreSize",value = "3"),
                    //队列等待阈值(最大队列长度,最大等待的线程数,默认为-1)
                    @HystrixProperty(name = "maxQueueSize",value = "100"),
                    //线程存活时间,默认1min
                    @HystrixProperty(name = "keepAliveTimeMinutes",value = "2"),
                    //超出队列等待阈值执行拒绝策略
                    @HystrixProperty(name = "queueSizeRejectionThreshold",value = "100")
            },fallbackMethod = "selectedProductByIdFallback"
    )

包含了三种触发条件,满足其中任意一个就会返回fallback方法,此外方法中如果出现Hystrix以外的异常也会直接出发服务降级返回fallback。

Feign实战记录

这里将一个单节点应用分成了两个结点:计算密集型(用于使用搜索算法,寻找合适的活动安排方案),存储密集型结点(用于操作数据库)

首先是编码问题:

yml应当使用UTF-8的编码,需要在setting里面单独设置,避开中文

然后是pom依赖问题,pom依赖应当参照spring的官方文档,对于各种版本都有适配的要求:

Spring Cloud

此时项目使用的版本是:

也就是springboot使用的是2.6.3,spring-cloud使用的是2021.0.1


    org.springframework.boot
    spring-boot-starter-parent
    2.6.3
 
··
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        2021.0.1
        pom
        import
      
    
  

netflix和openfeign都使用的是3.1.1

这样依赖就配合好了

对于需要连接数据库的yml配置方式:

server:
  port: 8080
  servlet:
    session:
      timeout: 3000
  tomcat:
    KeepAliveTimeOut: 30000 #多少ms后没有请求断开连接
    maxKeepAliveRequests: 10000 #多少次请求后断开连接

spring:
  application:
    name: time-manager
  datasource:
    name: time_manager
    url: jdbc:mysql://127.0.0.1:3306/time_manager?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
  aop:
    auto: true
  jpa:
    show-sql: true   # 是否启动日志SQL语句
  redis:
    timeout: 10000         #超时时间:10s
    host: localhost        #redis服务器地址
    port: 6379             #redis服务器端口
    password: 123456         #redis服务器密码
    database: 0            #选择哪个库
    lettuce:
      pool:
        max-active: 1024   #最大连接数
        max-wait: 10000    #最大连接阻塞等待时间,单位ms,默认-1
        max-idle: 200      #最大空闲连接,默认8
        min-idle: 5        #最小空闲连接 ,默认0

mybatis:
  mapper-locations: classpath:mapper
@FeignClient("task-manager")
public interface TaskManageRemoteCallService {
    
    @PostMapping("/task/manage")
    LinkedHashMap taskManage(@RequestBody FixedEventsAndTasks fixedEventsAndTasks);
}

@RequestBody 服务调用者可以不加,因为它默认将参数放到请求体中

package com.compute.controller;

import com.compute.bean.FixedEvent;
import com.compute.bean.FixedEventsAndTasks;
import com.compute.bean.Task;
import com.compute.service.EventManageService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;


@RestController
@RequestMapping("/task")
public class TaskManageController {
    @Resource
    EventManageService eventManageService;

    @PostMapping("/manage")
    LinkedHashMap taskManage(@RequestBody FixedEventsAndTasks fixedEventsAndTasks)
    {
        System.out.println(fixedEventsAndTasks);
        List alreadyIn = fixedEventsAndTasks.getFixedEvents();
        List tasks = fixedEventsAndTasks.getTasks();
        if(alreadyIn!=null&&tasks!=null) {
            return eventManageService.eventManage(alreadyIn,tasks);
        }
        else
        {
            LinkedHashMap resultMap = new LinkedHashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","参数不全");
            return resultMap;
        }
    }
}

服务提供者也要加上@RequestBody注解,否则会默认从路径中读取数据而读取不到数据

返回参数的时候:

        if(trySearchAnswer(alreadyIn,taskAndVis,0,0))
        {
            resultMap.put("status",200);
            resultMap.put("msg","安排成功");
            resultMap.put("results",alreadyIn);
        }
        else
        {
            resultMap.put("status",500);
            resultMap.put("msg","安排失败");
        }

就和正常的服务一样返回即可,alreadyIn是一个对象数组

对象数组在返回时(从Map中拿出来时),会从对象变成一个Map,因而会解析失败

所以需要我们把拿出来的内容解析成JSON格式,再进行转换(从=模式变成:模式)

		List fixedEventArray = (List) returnResult.get("results");
        ObjectMapper objectMapper=new ObjectMapper();
        List fixedEventList=objectMapper.convertValue(fixedEventArray, new TypeReference>() {});
        //添加安排后的结果
        addManagedResults(fixedEventList,userAccount,weekNum);

如果不是List类型,直接强制转换即可:

Integer status= (Integer) returnResult.get("status");

服务调用者传递参数的时候只能传递一个参数,所以我们可以传一个Map过去,或者新定义一个类

传Map:

package com.demo.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.LinkedHashMap;


@FeignClient("task-manager")
public interface TaskManageRemoteCallService {
    
    @PostMapping("/task/manage")
    LinkedHashMap taskManage(LinkedHashMap params);
}

接收map:

package com.compute.controller;

import com.compute.bean.FixedEvent;
import com.compute.bean.FixedEventsAndTasks;
import com.compute.bean.Task;
import com.compute.service.EventManageService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;


@RestController
@RequestMapping("/task")
public class TaskManageController {
    @Resource
    EventManageService eventManageService;

    @PostMapping("/manage")
    LinkedHashMap taskManage(@RequestBody LinkedHashMap params)
    {
        List alreadyIn= (List) params.get("fixedEvents");
        List fixedEvents=new ObjectMapper().convertValue(alreadyIn, new TypeReference>(){});
        List tasks = (List) params.get("tasks");
        List taskList=new ObjectMapper().convertValue(tasks, new TypeReference>() {});
        if(fixedEvents!=null&&tasks!=null) {
            return eventManageService.eventManage(fixedEvents,taskList);
        }
        else
        {
            LinkedHashMap resultMap = new LinkedHashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","参数不全");
            return resultMap;
        }
    }
}
基于Feign的服务熔断和降级

新版本的Feign不能服务熔断和降级,并且不再默认包含Hystrix(我TM……)

Spring Cloud openFeign学习【3.0.2版本】 - SegmentFault 思否

解决方案:回退版本,统一回退到2.2.4.RELEASE 和JDK 13的版本 SpringCloud HostonSR1

JDK下载:http://jdk.java.net/java-se-ri/13

把MAVEN中原有的jar删掉,然后再下新的

实现服务降级其实很简单,在原有的Feign的基础上,编写一个实现类,作为它的托底方法类,然后加上@FeignClient(value = “task-manager”,fallback = TaskManageFallBack.class) 注解即可

package com.demo.RemoteCall;

import com.demo.RemoteCall.FallBack.TaskManageFallBack;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.LinkedHashMap;


@FeignClient(value = "task-manager",fallback = TaskManageFallBack.class)
public interface TaskManageRemoteCallService {
    

    @PostMapping("/task/manage")
    LinkedHashMap taskManage(LinkedHashMap params);
}

value:服务名称 fallback:托底方法类

托底方法类:

package com.demo.RemoteCall.FallBack;

import com.demo.RemoteCall.TaskManageRemoteCallService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;

@Service
public class TaskManageFallBack implements TaskManageRemoteCallService {
    
    @Override
    public LinkedHashMap taskManage(LinkedHashMap params) {
        params.put("status",500);
        params.put("msg","网络繁忙");
        return params;
    }
}

添加FallbackFactory来实现异常信息地输出

package com.demo.RemoteCall.FallBack;

import com.demo.RemoteCall.TaskManageRemoteCallService;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class TaskManageFallBackFactory implements FallbackFactory {
    Logger logger = LoggerFactory.getLogger(TaskManageFallBackFactory.class);
    @Override
    public TaskManageRemoteCallService create(Throwable cause) {
        logger.error("出现异常,触发服务降级:"+cause.getMessage());
        cause.printStackTrace();
        return new TaskManageFallBack();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/950779.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号