Soul的高性能网关搭建教程
前言
刚刚Soul 网关发布里程碑式的 2.3.0 版本;最为体验和尝鲜的态度,试着体验的新的版本;同时也是因为之前的转载文章[转载]芋道 Soul 极简入门(国产微服务网关)实在是有点久了;一些方式发生了改变,就记录下过程。
一、2.3 更新内容?
通过 2.3 releases可以了解;做了如下的更新;这里简单的中文介绍:
soul-admin(dashboard)
admin
是整个网关的控制面板,掌管所有的流量,规则的匹配。
- 整合
shior
框架,完成了用户按钮级别的权限控制。 - 模板化插件,让用户无需感知前端页面,只专注于数据的配置。
- admin整个后端的国际化,支持中英文切换。
- 新增支持
H2
来存储数据。 - admin界面的美观优化(表格,按钮)。
- 新增单元测试,覆盖率达到百分之七十。
soul网关插件
插件新增
- 新增
GRPC
插件,全面支持GRPC
协议。 - 新增
Tars
插件,支持腾讯tars RPC
协议。 - 新增
Sofa
插件,支持sofa RPC
协议。 - 新增
Sentinel
插件,整合sentinel
框架的熔断限流功能。 - 新增
Resilience4j
插件,整合Resilience4j
框架的熔断限流功能。 - 新增
Rediect
插件,支持用户的重定向。 - 新增
Context-path
插件,支持用户自定义context-path
插件优化
-
Divide
插件 : 节点探活方式的优化,流量预热方式的优化。 -
Ratelimiter
插件 :新增并发,漏桶等不同的限流算法,供用户选择。 -
Sgin
插件 : 修复必须设置 url的bug,新增是否验证 标记,可以用来做开放平台的URI认证。 -
Dubbo
插件 : 新增 form表单,URI参数请求, 新增注册中心直连,参数校验等功能。
Soul Client
soul-client只是提供一种快速接入网关的客户端,不是必须的。如果用户不使用,可以在soul-admin自行配置规则即可。
-
spring-mvc
客户端的优化,支持spring
,spring-boot
所有版本。 -
spring-cloud
客户端的优化,支持spring
,spring-boot
所有版本。 -
dubbo
客户端的优化,支持spring
,spring-boot
所有版本。 - 新增
soul-grpc-client
支持grpc-java
用户接入。 - 新增
soul-tars-client
支持tars-java
用户接入。 - 新增
soul-sofa-client
支持sofa-java
用户接入。
在之前的版本中,只支持 http 方式接入 而这次新增注册中心方式接入。
- 新增
zookeeper
作为注册中心的方式接入soul
网关。 - 新增
Nacos
作为注册中心的方式接入soul
网关。 - 新增
Consul
作为注册中心的方式接入soul
网关。 - 新增
Etcd
作为注册中心的方式接入soul
网关。
使用方式请参考: https://dromara.org/projects/soul/register-center-access/
Soul 数据同步
- 修复
Nacos
配置中心同步未设置NameSpace
的Bug。 - 优化
Websocket
同步方式。 - 解决
soul-admin
集群部署时候,Http
长轮询方式同步数据Bug。
二、动手运行--环境搭建
官方文档中的 环境搭建 · dromara(Open source organization) 本身详细的说明,启动搭建的方式:
- Docker
- 远程下载
- 本地搭建
本着学习的目的;这里推荐使用使用本地搭建的方式(笔者也是通过本地搭建的方式搭建的);其余两种方式适合快速搭建;大家可以试试。
1. Soul Admin 安装
> git clone https://github.com/dromara/soul.git > cd soul
注意:启动 SoulAdminBootstrap
。
- 如果使用h2来存储,设置变量
--spring.profiles.active = h2
- 如果使用mysql来存储,修改
application.yaml
中的mysql
配置。
这里我们使用 h2
的内存数据库。
启动 SoulAdminBootstrap
类;看到如下内容启动成功:
访问 http://localhost:9095 用户名密码为: admin/123456
2. Soul Bootstrap 安装
> git clone https://github.com/dromara/soul.git > cd soul
启动 SoulBootstrapApplication
类;看到 websocket
成功连接网关管理台的内容:
接入 Spring Boot 应用
写简单测试案例的时候;这次我们使用 示例代码 代码运行。
<properties> <soul.version>2.3.0-RELEASE</soul.version> </properties> <!--if you use http proxy start this--> <dependency> <groupId>org.dromara</groupId> <artifactId>soul-spring-boot-starter-plugin-divide</artifactId> <version>${last.version}</version> </dependency> <dependency> <groupId>org.dromara</groupId> <artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId> <version>${last.version}</version> </dependency>
application.yaml
关于soul的配置如下:
soul: client: registerType: http #zookeeper #etcd #nacos #consul serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848 # Soul Admin 地址目前也支持注册中心 props: contextPath: /http # 设置在 Soul 网关的路由前缀,例如说 /order、/product 等等。 # 后续,网关会根据该 context-path 来进行路由 appName: http #应用名。未配置情况下,默认使用 `spring.application.name` 配置项 port: 8189 #应用名端口;和配置server.port保持一致;作为路由端口 nacosNameSpace: SoulRegisterCenter
OrderController
内容:
@RestController @RequestMapping("/order") @SoulSpringMvcClient(path = "/order") public class OrderController { //... @PostMapping("/save") @SoulSpringMvcClient(path = "/save" , desc = "Save order") public OrderDTO save(@RequestBody final OrderDTO orderDTO) { orderDTO.setName("hello world save order"); return orderDTO; } //... }
@SoulSpringMvcClient
注解;有如下内容:
-
path
:映射的 HTTP 接口的请求路径。 -
desc
:接口的描述,便于知道其用途。 -
enable
:是否开启,默认为true
开启。
启动 SoulTestHttpApplication
main方法启动项目。
访问 http://localhost:9195/http/order/save ;值得说明:9195
是soul网关地址;http
是配置的 contextpath
通过简单案例理解如下数据同步原理:
三、自定义网关搭建
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Licensed to the Apache Software Foundation (ASF) under one or more
~ contributor license agreements. See the NOTICE file distributed with
~ this work for additional information regarding copyright ownership.
~ The ASF licenses this file to You under the Apache License, Version 2.0
~ (the "License"); you may not use this file except in compliance with
~ the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.xxxx.soul</groupId>
<artifactId>soul-gateway</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>soul-gateway</name>
<description>Soul服务网关</description>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<mybatis-starter.version>2.1.1</mybatis-starter.version>
<druid-starter.version>1.1.21</druid-starter.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
<client.version>1.0.0-SNAPSHOT</client.version>
<spring-cloud-nacos-version>2.2.1.RELEASE</spring-cloud-nacos-version>
<soul-version>2.3.0-RELEASE</soul-version>
<curator.version>4.0.1</curator.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--soul gateway start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-gateway</artifactId>
<version>${soul-version}</version>
</dependency>
<!--if you use http proxy start this-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-divide</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul httpclient plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul httpclient plugin end-->
<!-- soul global plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-global</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul global plugin end-->
<!-- soul ratelimiter plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-ratelimiter</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul ratelimiter plugin end-->
<!-- soul hystrix plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-hystrix</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul hystrix plugin end-->
<!-- soul waf plugin starter-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-waf</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul waf plugin end-->
<!-- soul monitor plugin starter-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-monitor</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul monitor plugin end-->
<!-- soul sign plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-sign</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul sign plugin end-->
<!-- soul resilience4j plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-resilience4j</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul resilience4j plugin end-->
<!-- soul sentinel plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-sentinel</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul sentinel plugin end-->
<!-- soul context-path plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-context-path</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul context-path plugin end-->
<!-- soul redirect plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-redirect</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul redirect plugin end-->
<!-- soul redirect plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-rewrite</artifactId>
<version>${soul-version}</version>
</dependency>
<!-- soul redirect plugin end-->
<!--soul tars plugin end-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-tars</artifactId>
<version>${soul-version}</version>
</dependency>
<dependency>
<groupId>com.tencent.tars</groupId>
<artifactId>tars-client</artifactId>
<version>1.7.2</version>
</dependency>
<!--soul tars plugin end-->
<!--soul grpc plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-grpc</artifactId>
<version>${soul-version}</version>
</dependency>
<!--soul grpc plugin end-->
<!--soul sofa plugin start-->
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-rpc-all</artifactId>
<version>5.7.6</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-sofa</artifactId>
<version>${soul-version}</version>
</dependency>
<!--soul sofa plugin end-->
<!--soul springCloud plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-springcloud</artifactId>
<version>${soul-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-commons</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<!-- springCloud if you config register center is nacos please dependency this-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
<!-- springCloud if you config register center is eureka please dependency end-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>-->
<!-- <version>2.2.0.RELEASE</version>-->
<!-- </dependency>-->
<!--soul springCloud plugin start end-->
<!--soul apache dubbo plugin start-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-plugin-apache-dubbo</artifactId>
<version>${soul-version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.5</version>
</dependency>
<!-- Dubbo Nacos registry dependency -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
<version>2.7.5</version>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
<version>1.1.4</version>
</dependency>
<!-- Dubbo zookeeper registry dependency start -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>
<!-- Dubbo zookeeper registry dependency end -->
<!-- soul apache dubbo plugin end-->
<!--soul alibaba dubbo plugin start-->
<!-- <dependency>-->
<!-- <groupId>org.dromara</groupId>-->
<!-- <artifactId>soul-spring-boot-starter-plugin-alibaba-dubbo</artifactId>-->
<!-- <version>${soul-version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>com.alibaba</groupId>-->
<!-- <artifactId>dubbo</artifactId>-->
<!-- <version>${alibaba.dubbo.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.curator</groupId>-->
<!-- <artifactId>curator-client</artifactId>-->
<!-- <version>${curator.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.curator</groupId>-->
<!-- <artifactId>curator-framework</artifactId>-->
<!-- <version>${curator.version}</version>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.apache.curator</groupId>-->
<!-- <artifactId>curator-recipes</artifactId>-->
<!-- <version>${curator.version}</version>-->
<!-- </dependency>-->
<!-- soul alibaba dubbo plugin end-->
<!--soul data sync start use zookeeper-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-zookeeper</artifactId>
<version>${soul-version}</version>
</dependency>
<!--soul data sync start use websocket-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-websocket</artifactId>
<version>${soul-version}</version>
</dependency>
<!--soul data sync start use http-->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-sync-data-http</artifactId>
<version>${soul-version}</version>
</dependency>
</dependencies>
<build>
<finalName>dubhe-gateway</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.dromara.soul.bootstrap.SoulBootstrapApplication</mainClass>
<executable>true</executable>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
<resources>
<!--加载多环境配置-->
<resource>
<directory>${basedir}/src/main/resources</directory>
<excludes>
<exclude>daily/*</exclude>
<exclude>project/*</exclude>
<exclude>product/*</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/resources/${profileActive}</directory>
</resource>
</resources>
</build>
<profiles>
<profile>
<id>daily</id>
<properties>
<profileActive>daily</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<profile>
<id>project</id>
<properties>
<profileActive>project</profileActive>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>product</id>
<properties>
<profileActive>product</profileActive>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
</project>