【springcloud alibaba】 一条龙服务实现微服务案例(下)

本文涉及的产品
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 【springcloud alibaba】 一条龙服务实现微服务案例(下)

第六章 Feign 实现服务调用


RestTemplate服务调用问题:


1.代码可读性不好

2.代码风格不一样

3.使用fegin就像调用本地的服务一样,注入服务即可。

4.feign很好的兼容了nacos

5.feign默认继承了ribbon负载均衡


6.1集成Feign


1.pom引入依赖

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

2.启动类开启注解支持

@EnableFeignClients


6.2 修改order服务调用


1.创建ProductService接口

//用于指定 调用nacos的哪一个微服务
@FeignClient(value = "service-product")
public interface ProductService {
    @RequestMapping("/product/{pid}")
    public Product findByPid(@PathVariable("pid") Integer pid);
}


2.OrderController服务进行feign调用

@RestController
public class OrderController {
    @Autowired
    private ProductService productService;
    @RequestMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid){
        // 1.查询商品服务
        Product product = productService.findByPid(pid);

3.重启order服务进行测试


http://localhost:8091/order/prod/1


返回结果:{“oid”:7,“uid”:1,“username”:“测试用户”,“pid”:1,“pname”:“小米”,“pprice”:1000.0,“number”:1}


第七章 Sentinel 服务容错


7.1 简介


Sentinel (分布式系统的流量防卫兵) 是阿里开源的一套用于服务容错的综合性解决方案。它以流量为切入点, 从流量控制、熔断降级、系统负载保护等多个维度来保护服务的稳定性。


Sentinel 分为两个部分

核心库(Java 客户端)不依赖任何框架/库,能够运行于所有 Java 运行时环境,同时对 Dubbo / Spring Cloud 等框架也有较好的支持。


控制台(Dashboard)基于 Spring Boot 开发,打包后可以直接运行,不需要额外的 Tomcat 等应用容器。


7.2 集成Sentinel


1.order上游服务引入pom依赖

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

2.编写一个Controller测试使用

@RestController
public class OrderController3 { 
    @RequestMapping("/order/message1") 
    public String message1() {
    return "message1";
    }
    @RequestMapping("/order/message2") 
    public String message2() {
        return "message2";
    }
}

3.修改shop-order配置文件


spring:
 cloud:
      sentinel:
      transport:
        port: 9999 #跟控制台交流的端口,随意指定一个未使用的端口即可
        dashboard: localhost:8080 # 指定控制台服务的地址


7.3 安装Sentinel控制台


Sentinel 提供一个轻量级的控制台, 它提供机器发现、单机资源实时监控以及规则管理等功能。


Sentinel的控制台其实就是一个SpringBoot编写的程序。我们需要将我们的微服务程序注册到控制台上, 即在微服务中指定控制台的地址, 并且还要开启一个跟控制台传递数据的端口, 控制台也可以通过此端口调用微服务中的监控程序获取微服务的各种信息。


1 下载jar包,解压到文件夹https://github.com/alibaba/Sentinel/releases


2 启动项目

# 直接使用jar命令启动项目(控制台本身是一个SpringBoot项目)
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.7.0.jar

3.访问控制台

通过浏览器访问 localhost:8080 进入控制台

( 默认用户名密码是 sentinel/sentinel )


7.4 Sentinel规则配置


7.4.1 流控规则


流量控制在网络传输中是一个常用的概念,它用于调整网络包的数据。任意时间到来的请求往往是 随机不可控的,而系统的处理能力是有限的。我们需要根据系统的处理能力对流量进行控制。Sentinel 作为一个调配器,可以根据需要把随机的请求调整成合适的形状。


流量控制,其原理是监控应用流量的QPS(每秒查询率) 或并发线程数等指标,当达到指定的阈值时对流量进行控制,以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性。


第1步: 点击簇点链路,我们就可以看到访问过的接口地址,然后点击对应的流控按钮,进入流控规则配置页面。


资源名:唯一名称,默认是请求路径,可自定义


针对来源:指定对哪个微服务进行限流,默认指default,意思是不区分来源,全部限制 阈


sentinel共有三种流控模式,分别是:


直接(默认):接口达到限流条件时,开启限流


关联:当关联的资源达到限流条件时,开启限流 [适合做应用让步]


链路:当从某个接口过来的资源达到限流条件时,开启限流下面呢分别演示三种模式:


直接流控模式

直接流控模式是最简单的模式,当指定的接口达到限流条件时开启限流。上面案例使用的就是直接流控 模式。


关联流控模式

关联流控模式指的是,当指定接口关联的接口达到限流条件时,开启对指定接口开启限流。


链路流控模式

链路流控模式指的是,当从某个接口过来的资源达到限流条件时,开启限流。它的功能有点类似于针对 来源配置项,区别在于:针对来源是针对上级微服务,而链路流控是针对上级接口,也就是说它的粒度更细。


7.4.2 降级规则


降级规则就是设置当满足什么条件的时候,对服务进行降级。Sentinel提供了三个衡量条件:


平均响应时间 :当资源的平均响应时间超过阈值(以 ms 为单位)之后,资源进入准降级状态。如果接下来 1s 内持续进入 5 个请求,它们的 RT都持续超过这个阈值,那么在接下的时间窗口(以 s 为单位)之内,就会对这个方法进行服务降级。


异常比例:当资源的每秒异常总数占通过量的比值超过阈值之后,资源进入降级状态,即在接下的 时间窗口(以 s 为单位)之内,对这个方法的调用都会自动地返回。异常比率的阈值范围是 [0.0, 1.0]。


异常数 :当资源近 1 分钟的异常数目超过阈值之后会进行服务降级。注意由于统计时间窗口是分钟级别的,若时间窗口小于 60s,则结束熔断状态后仍可能再进入熔断状态


7.4.3 热点规则


热点参数流控规则是一种更细粒度的流控规则, 它允许将规则具体到参数上。


7.5 统一异常

@Component
public class ExceptionHandlerPage implements UrlBlockHandler {
    @Override
    public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException e) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        ResponseData data = null;
        if (e instanceof FlowException) {
            data = new ResponseData(-1, "接口被限流了...");
        } else if (e instanceof DegradeException) {
            data = new ResponseData(-2, "接口被降级了...");
        }
        response.getWriter().write(JSON.toJSONString(data));
        }
    }
@Data
@AllArgsConstructor
@NoArgsConstructor
class ResponseData {
    private int code;
    private String message;
}


7.6 Feign整合Sentinel实现容错

7.6.1 配置文件开启feign对sentinel的支持

feign:
  sentinel:
    enabled: true

7.6.2 创建容错类


@Service
public class ProductServiceFallback implements ProductService {
    @Override
    public Product findByPid(Integer pid) {
        Product product = new Product();
        product.setPid(-1);
        product.setPname("出现异常,进入容错模式");
        return product;
    }
}


7.6.3 服务中心指定容错类

@FeignClient(value = "service-product", fallback = ProductServiceFallback.class) 
public interface ProductService {
  @RequestMapping("/product/{pid}")//指定请求的URI部分
  Product findByPid(@PathVariable Integer pid);
}


如下测试Controller即可


if (product.getPid() == -1) { 
  Order order = new Order(); 
  order.setPname("下单失败"); 
  return order;
}


第八章 Gateway 服务网关


所谓的API网关,就是指系统的统一入口,它封装了应用程序的内部结构,为客户端提供统一服务,一些与业务本身功能无关的公共逻辑可以在这里实现,诸如认证、鉴权、监控、路由转发等等。 它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。它的目标是替代Netflix Zuul,其不仅提供统一的路由方式,并且基于 Filter 链的方式提供了网关基本的功能,例如:安全,监控和限流。


要求:只能打成jar包,springboot2.0以上的版本


8.1 创建api-gateway网关服务模块


1.pom依赖

<dependencies>
    <!--不能引入web相关的-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
</dependencies>

2.启动类开启服务发现

@EnableDiscoveryClient

3.配置文件yml参数

server:
  port: 7000
spring:
  application:
    name: api-gateway
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    gateway:
      discovery:
        locator:
          enabled: true  # 在nacos中注册
      routes:
        - id: product_route
          uri: lb://service-product
          order: 1
          predicates:
            - Path=/product-serv/**
          filters:
            - StripPrefix=1

4.测试访问


http://localhost:7000/product-serv/product/2


5.默认路由---->如果路由规则 routes全部注释掉


默认路由名称为nacos中服务节点的名称


测试访问


http://localhost:7000/service-product/product/2


8.2 网关限流


网关是所有请求的公共入口,所以可以在网关进行限流,而且限流的方式也很多,我们本次采用前 面学过的Sentinel组件来实现网关的限流。Sentinel支持对SpringCloud Gateway、Zuul等主流网关进行限流。


实现:略

1.pom

2.配置类

3.测试


第九章 Sleuth 链路追踪


SpringCloud Sleuth主要功能就是在分布式系统中提供追踪解决方案。它大量借用了Google Dapper的设计, 先来了解一下Sleuth中的术语和相关概念。


9.1 集成Sleuth


1.父工程的pom依赖


<!--链路追踪 Sleuth-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>

重新启动微服务,调用之后,我们可以在控制台观察到sleuth的日志输出。


查看日志文件并不是一个很好的方法,当微服务越来越多日志文件也会越来越多,通过Zipkin可以 将日志聚合,并进行可视化展示和全文检索。


Zipkin 是 Twitter 的一个开源项目,它基于Google Dapper实现,它致力于收集服务的定时数据, 以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。


我们可以使用它来收集各个服务器上请求链路的跟踪数据,并通过它提供的REST API接口来辅助我们查询跟踪数据以实现对分布式系统的监控程序,从而及时地发现系统中出现的延迟升高问题并找出系 统性能瓶颈的根源。


9.2 集成Zipkin


9.2.1 Zipkin服务端安装

1.下载jar包


https://search.maven.org/remote_content?g=io.zipkin.java&a=zipkin- server&v=LATEST&c=exec


2.执行jar包


java -jar zipkin-server-2.12.9-exec.jar


3.访问


http://localhost:9411


9.2.2 Zipkin客户端安装


1.每个微服务的pom

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

2.配置文件

spring: 
  zipkin:
    base-url: http://127.0.0.1:9411/ #zipkin server的请求地址
    discoveryClientEnabled: false #让nacos把它当成一个URL,而不要当做服务名
  sleuth:
  sampler:
    probability: 1.0 #采样的百分比

3.测试访问

http://localhost:7000/order-serv/order/prod/1

4.登录zipkin的界面观看变化

http://localhost:9411


9.3 持久化


mysql elasticsearch 略


第十章 RocketMQ 消息队列


RocketMQ是阿里巴巴开源的分布式消息中间件,现在是Apache的一个顶级项目。在阿里内部使用 非常广泛,已经经过了"双11"这种万亿级的消息流转。


安装略,请见安装策略。注意安装配置JVM参数调小即可。


接下来我们模拟一种场景: 下单成功之后,向下单用户发送短信。设计图如下:


20200819153618957.png

10.1 订单微服务发送消息


1.shop-order中添加依赖

<!--rocketmq-->
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-spring-boot-starter</artifactId>
    <version>2.0.2</version>
</dependency>
<dependency>
    <groupId>org.apache.rocketmq</groupId>
    <artifactId>rocketmq-client</artifactId>
    <version>4.4.0</version>
</dependency>

2.yml配置

rocketmq:
  name-server: 47.94.39.202:9876   #rocketMQ服务的地址
  producer:
    group: shop-order # 生产者组

3.controller

@RestController 
@Slf4j
public class OrderController2 { 
  @Autowired
    private OrderService orderService; 
    @Autowired
    private ProductService productService; 
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    //准备买1件商品@GetMapping("/order/prod/{pid}")
    public Order order(@PathVariable("pid") Integer pid) { 
    log.info(">>客户下单,这时候要调用商品微服务查询商品信息");
    //通过fegin调用商品微服务
    Product product = productService.findByPid(pid);
    if (product == null){
    Order order = new Order(); order.setPname("下单失败");
      return order;
    }
    log.info(">>商品信息,查询结果:" + JSON.toJSONString(product));
      Order order = new Order(); order.setUid(1);
        order.setUsername(" 测 试 用 户 ");       
        order.setPid(product.getPid()); 
        order.setPname(product.getPname()); 
        order.setPprice(product.getPprice()); 
        order.setNumber(1); 
        orderService.save(order);
    //下单成功之后,将消息放到mq中
    rocketMQTemplate.convertAndSend("order-topic", order);
    return order;
    }
}

10.2 用户微服务订阅消息


1.shop-user pom依赖

<dependencies>
    <dependency>
        <groupId>com.itheima</groupId>
        <artifactId>shop-common</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos- discovery</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.rocketmq</groupId>
      <artifactId>rocketmq-spring-boot-starter</artifactId>
      <version>2.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.rocketmq</groupId>
      <artifactId>rocketmq-client</artifactId>
      <version>4.4.0</version>
    </dependency>
</dependencies>

2.主启动类添加注解


@EnableDiscoveryClient


3.配置文件

server: 
  port: 8071
spring:
  application:
    name: service-user 
  datasource:
    driver-class-name: com.mysql.jdbc.Driver 
    url: jdbc:mysql:///shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true 
    username: root
    password: root jpa:
properties: 
  hibernate:
  hbm2ddl:
    auto: update
  dialect: org.hibernate.dialect.MySQL5InnoDBDialect cloud:
nacos:
  discovery:
     server-addr: 127.0.0.1:8848
rocketmq:
  name-server: 192.168.109.131:9876

4.编写消息接收服务

//发送短信的服务
@Slf4j 
@Service
@RocketMQMessageListener(consumerGroup = "shop-user", topic = "order-topic") 
public class SmsService implements RocketMQListener<Order> {
    @Override
    public void onMessage(Order order) {
      log.info("收到一个订单信息{},接下来发送短信", JSON.toJSONString(order));
    }
}

5.执行下单操作,观看shop-user服务的控制台输出。


第十一章 Seata 分布式事务控制


相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
5天前
|
负载均衡 监控 算法
【微服务 SpringCloud】实用篇 · Eureka注册中心
【微服务 SpringCloud】实用篇 · Eureka注册中心
17 1
|
5天前
|
存储 SpringCloudAlibaba Java
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
【微服务 SpringCloud】实用篇 · 服务拆分和远程调用
20 2
|
5天前
|
Prometheus 监控 负载均衡
【SpringCloud】微服务重点解析
【SpringCloud】微服务重点解析
20 0
|
5天前
|
SpringCloudAlibaba 负载均衡 Java
【微服务 SpringCloudAlibaba】实用篇 · Gateway服务网关
【微服务 SpringCloudAlibaba】实用篇 · Gateway服务网关
16 0
|
5天前
|
JSON SpringCloudAlibaba Java
【微服务 SpringCloudAlibaba】实用篇 · Feign服务远程调用
【微服务 SpringCloudAlibaba】实用篇 · Feign服务远程调用
19 0
|
5天前
|
缓存 负载均衡 算法
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
【微服务 SpringCloud】实用篇 · Ribbon负载均衡
23 0
|
5天前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
261 0
|
5天前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
205 0
|
5天前
|
Java API Nacos
第十二章 Spring Cloud Alibaba Sentinel
第十二章 Spring Cloud Alibaba Sentinel
30 0
|
5天前
|
存储 前端开发 Java
第十一章 Spring Cloud Alibaba nacos配置中心
第十一章 Spring Cloud Alibaba nacos配置中心
29 0