首页 > 其他分享 >Spring Cloud Alibaba系列(四)微服务网关Gateway及在网关上用sentinel进行限流熔断

Spring Cloud Alibaba系列(四)微服务网关Gateway及在网关上用sentinel进行限流熔断

时间:2023-03-25 18:55:44浏览次数:37  
标签:网关 Spring boot springframework 限流 spring org com cloud

Spring Cloud Gateway是基于Netty,使用Reactor连接方式的,采用非阻塞方式,相比zuul支持更高的并发,且zuul已经停止维护,我们应该使用Gateway来构建微服务应用。

一. 使用nacos实现负载均衡

实际业务场景中,Gateway会和Nacos配合使用来做到负载均衡,nacos的搭建请参考文章 https://www.cnblogs.com/fxhui/p/17248503.html

创建一个名为mygateway的项目,pom.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>mygateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mygateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.11</spring-boot.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.mygateway.MygatewayApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

启动类新增注解@EnableDiscoveryClient

package com.example.mygateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class MygatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(MygatewayApplication.class, args);
    }

}

resource里添加application.yml配置文件,分别新增的gateway路由配置和nacos注册中心配置,lb是loadbalance简称,网关接收到/hello后会将请求转发到lb://serviceProvider上,以负载均衡方式访问服务serviceProvider

server:
  port: 7079
spring:
  cloud:
    gateway:
      routes:
        - id: loadbalance_route
          uri: lb://serviceProvider
          predicates:
            - Path=/hello
    nacos:
      discovery:
        server-addr: 127.0.0.1:8867,127.0.0.1:8869
  application:
    name: mygateway

 我们通过访问网关http://localhost:7079/hello就可以访问到serviceProvider的接口

 

. 使用sentinel实现限流熔断防护

限流防护的使用方法在前文中已经给出,具体见这篇文章https://www.cnblogs.com/fxhui/p/17238253.html

之前只是在方法上实现,本文讲讲如何在网关层通过路由实现防护

 在pom.xml新增sentinel依赖包,整体文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>mygateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>mygateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.11</spring-boot.version>
        <spring-cloud.version>2021.0.5</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.mygateway.MygatewayApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

在application.yml中添加sentinel配置,整体文件如下:

server:
  port: 7079
spring:
  cloud:
    sentinel:
      transport:
        # 与sentinel控制台交互的端口
        port: 9000
        # sentinel控制台的工作地址和端口
        dashboard: localhost:8090
    gateway:
      routes:
        - id: loadbalance_route
          uri: lb://serviceProvider
          predicates:
            - Path=/hello
    nacos:
      discovery:
        server-addr: 127.0.0.1:8867,127.0.0.1:8869
  application:
    name: mygateway

启动服务后打开sentinel控制台,访问网关接口http://localhost:7079/hello

 

 打开mygateway下拉,点击簇点链路

 

 点击资源/hello的流控按钮,输入单机阈值

 

 访问网关的接口http://localhost:7079/hello,不停刷新,发现经常出现以下错误,资源已经被控制为单机阈值1了

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:网关,Spring,boot,springframework,限流,spring,org,com,cloud
From: https://www.cnblogs.com/fxhui/p/17254829.html

相关文章

  • java——spring boot集成kafka——单节点示例
    首先安装一个zk。然后再安装kafka:   执⾏以下命令创建名为“test”的topic,这个topic只有⼀个partition,并且备份因⼦也设置为1: 然后在kafka节点下,执行如下命令:......
  • SpringBoot集成SpringBootAdmin实现监控
    效果展示客户端maven引用<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifa......
  • Spring
    1.简介Spring是一个免费的开源框架(容器)Spring是一个轻量级、非入侵式的框架控制反转(IOC),面向切面(AOP)编程支持事务处理,对框架整合也支持2.组成-七大模块3.IOC理论推......
  • SpringMVC快速入门与详解
    一、SpringMVC简介1.概念 SpringMVC是一种基于Java的实现MVC设计模式的Web框架,该框架通过将Web应用程序划分为模型、视图和控制器来简化Web应用程序的开发......
  • SpringBoot集成SpringBootAdmin实现监控
    效果展示客户端maven引用<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artif......
  • Sentinel 限流、熔断降级
    一、什么是Sentinel?随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。S......
  • 使用Docker 一键部署SpringBoot和SpringCloud项目
    Docker是一个非常流行的容器化技术,可以方便地将应用程序和服务打包成容器并运行在不同的环境中。在本篇博客中,我将向您展示如何使用Docker一键部署SpringBoot和SpringCloud......
  • spring
      ......
  • easypoi基于springboot实现多sheet导出
    maven<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-spring-boot-starter</artifactId><version>4.4.0</ve......
  • SpringBoot整合elasticsearch实操
    大家好,我是小悟ElasticSearch是一个分布式的开源搜索和分析引擎,适用于所有类型的数据,包括文本、数字、地理空间、结构化和非结构化数据。Elasticsearch以其简单的REST风......