如何读开源项目:对着文档跑demo,对着demo看代码,懂一点就开始试,有问题了问社区。

今日目标:

1.运行examples下面的 http服务

2.学习文档,结合divde插件,发起http请求soul网关,体验http代理

3.记录心得,写博客分享。

一、从官方文档开始

打开 用户使用文档 - http用户 页面,开始整理关键要素。

1、接入说明:

  • 接入前,需要先启动 soul-admin

  • soul 使用 divde 插件来处理 http 请求,插件在 admin 后台开启。

2、网关需要引入代理插件

网关 pom 增加依赖:

  <!--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>

重启网关使配置生效。

3、Http 服务接入网关

接入前需确保 soul-admin 已开启 divide 插件

3.1 soul-client方式接入

适用于 SpringMVC 体系用户,分为:

  • spring 版本

  • spring-boot 版本

两种版本依赖和配置有所差异,但在服务配置上都使用 @SoulSpringMVCClient,其用法并无差异。

3.1.1 spring 版本 SpringMVC 接入

1)被代理服务 pom.xml 引入如下依赖:

     <dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springmvc</artifactId>
<version>${last.version}</version>
</dependency>

2)xml 中新增如下 bean 配置:

   <bean id ="springMvcClientBeanPostProcessor" class ="org.dromara.soul.client.springmvc.init.SpringMvcClientBeanPostProcessor">
<constructor-arg ref="soulSpringMvcConfig"/>
</bean> <bean id="soulSpringMvcConfig" class="org.dromara.soul.client.springmvc.config.SoulSpringMvcConfig">
<property name="adminUrl" value="http://localhost:9095"/>
<property name="port" value="你的端口"/>
<property name="contextPath" value="/你的contextPath"/>
<property name="appName" value="你的名字"/>
<property name="full" value="false"/>
</bean>

3)Controller 加上 @SoulSpringMVCClient 注解,针对其 path 属性有两种配置场景

  • 该 Controller 下所有服务均需被网关代理

    注解加在 Controller 类上,path 配置为 /前缀路径/**

  • 该 Controller 下部分服务需被网关代理

    类上注解的 path 配置为 /前缀路径,方法上注解的 path 配置为 /对应的服务路径

4)启动项目,服务接入到网关。

3.1.2 spring-boot 版本 SpringMVC 接入

1)被代理服务 pom.xml 引入如下依赖:

     <dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springmvc</artifactId>
<version>${last.version}</version>
</dependency>

2)yml 中增加如下配置:

   soul:
http:
adminUrl: http://localhost:9095
port: 你本项目的启动端口
contextPath: /http
appName: http
full: false
  • adminUrl:soul-admin 项目的ip + 端口,注意要加http://

  • port:本项目的启动端口,需要与实际启动端口一致

  • contextPath:本项目在soul网关的路由前缀, 比如/order ,/product 等等,网关根据这个前缀来进行路由.

  • appName:你的应用名称,不配置则默认取 spring.application.name 的值

  • full:true 表示代表代理整个服务,false 表示代理其中某几个controller

3)Controller 加上 @SoulSpringMVCClient 注解,配置场景同 spring 版本

4)启动项目,服务接入到网关。

3.2 非 soul-client 方式接入

4、用户请求

请求方式有两点变化:

  • 修改 ip + 端口

    需要将 直接指向用户的请求,改为通过网关发起访问。

  • 加上 context path

    在 ip + 端口 后面,紧跟目标项目中配置的路由前缀

例如:

原请求 url:http://localhost:8080/test/save

现请求 url:http://localhost:9195/order/test/save_

二、官方示例分析

1、打开 soul-examples 项目

位于 soul 项目下,未集成到 soul 项目 maven 中统一管理,需要单独打开

2、定位待分析目标示例模块

待分析目标示例为 http 示例,选择并展开

3、依赖分析

打开 pom.xml,依赖关系如下:

        <!-- soul-client -->
<dependency>
<groupId>org.dromara</groupId>
<artifactId>soul-spring-boot-starter-client-springmvc</artifactId>
<version>${soul.version}</version>
</dependency>
<!-- webflix -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>

很明显是 spring-boot 项目,除了官方文档中要求引入的 soul-spring-boot-starter-client-springmvc 外,还引入了 webflix

4、配置分析

打开 application.yml,配置内容如下:

# 服务信息
server:
port: 8188
address: 0.0.0.0 # soul-client 配置
soul:
http:
adminUrl: http://localhost:9095
port: 8188
contextPath: /http
appName: http
full: false # 日志配置
logging:
level:
root: info
org.springframework.boot: info
org.apache.ibatis: info
org.dromara.soul.test.bonuspoint: info
org.dromara.soul.test.lottery: debug
org.dromara.soul.test: debug

soul-client 部分的配置格式与官方文档一致,注意核对下 soul-admin 的 ip + 端口是否与实际一致。

5、Controller 分析

展开 org.dromara.soul.examples.http.controller 包,自带两个controller

1)HttpTestController.java
/**
* TestController.
*
* @author xiaoyu
*/
@RestController
@RequestMapping("/test")
@SoulSpringMvcClient(path = "/test/**")
public class HttpTestController { /**
* Post user dto.
*
* @param userDTO the user dto
* @return the user dto
*/
@PostMapping("/payment")
public UserDTO post(@RequestBody final UserDTO userDTO) {
return userDTO;
} /**
* Find by user id string.
*
* @param userId the user id
* @return the string
*/
@GetMapping("/findByUserId")
public UserDTO findByUserId(@RequestParam("userId") final String userId) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(userId);
userDTO.setUserName("hello world");
return userDTO;
} /**
* Gets path variable.
*
* @param id the id
* @param name the name
* @return the path variable
*/
@GetMapping("/path/{id}")
public UserDTO getPathVariable(@PathVariable("id") final String id, @RequestParam("name") final String name) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
} /**
* Test rest ful string.
*
* @param id the id
* @return the string
*/
@GetMapping("/path/{id}/name")
public UserDTO testRestFul(@PathVariable("id") final String id) {
UserDTO userDTO = new UserDTO();
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
} /**
* Put path variable and body string.
*
* @param id the id
* @param userDTO the user dto
* @return the string
*/
@PutMapping("/putPathBody/{id}")
public UserDTO putPathVariableAndBody(@PathVariable("id") final String id, @RequestBody final UserDTO userDTO) {
userDTO.setUserId(id);
userDTO.setUserName("hello world");
return userDTO;
} }

类上使用了 @SoulSpringMvcClient(path = "/test/**") 表示注册该 Controller 下所有服务。

注册的服务清单:

  • /payment

  • /findByUserId

  • /path/{id}

  • /path/{id}/name

  • /putPathBody/{id}

都是简单的 mock 服务

2)OrderController.java
/**
* TestController.
*
* @author xiaoyu
*/
@RestController
@RequestMapping("/order")
@SoulSpringMvcClient(path = "/order")
public class OrderController { /**
* Save order dto.
*
* @param orderDTO the order dto
* @return the order dto
*/
@PostMapping("/save")
@SoulSpringMvcClient(path = "/save" , desc = "Save order")
public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
orderDTO.setName("hello world save order");
return orderDTO;
} /**
* Find by id order dto.
*
* @param id the id
* @return the order dto
*/
@GetMapping("/findById")
@SoulSpringMvcClient(path = "/findById", desc = "Find by id")
public OrderDTO findById(@RequestParam("id") final String id) {
OrderDTO orderDTO = new OrderDTO();
orderDTO.setId(id);
orderDTO.setName("hello world findById");
return orderDTO;
} /**
* Gets path variable.
*
* @param id the id
* @param name the name
* @return the path variable
*/
@GetMapping("/path/{id}/{name}")
@SoulSpringMvcClient(path = "/path/**")
public OrderDTO getPathVariable(@PathVariable("id") final String id, @PathVariable("name") final String name) {
OrderDTO orderDTO = new OrderDTO();
orderDTO.setId(id);
orderDTO.setName("hello world restful: " + name);
return orderDTO;
} /**
* Test rest ful order dto.
*
* @param id the id
* @return the order dto
*/
@GetMapping("/path/{id}/name")
@SoulSpringMvcClient(path = "/path/**/name")
public OrderDTO testRestFul(@PathVariable("id") final String id) {
OrderDTO orderDTO = new OrderDTO();
orderDTO.setId(id);
orderDTO.setName("hello world restful inline " + id);
return orderDTO;
}
}

类上使用了 @SoulSpringMvcClient(path = "/order"),表示该 Controller 下部分服务需要注册,配合方法上的 SoulSpringMvcClient 指定具体注册的服务。

注册的服务清单:

  • /save

  • /findById

  • /path/{id}/{name}

  • /path/{id}/name

注意到注册 GET 请求的服务时,url 中的参数需要用 ** 代替

6、启动示例模块

官方文档里提到:

需要在网关的 pom 里增加 soul-spring-boot-starter-plugin-divide 和 soul-spring-boot-starter-plugin-httpclient 依赖,并重启网关。

需要这么麻烦么?

百度查阅相关文章后,了解到 soul 本身提供的 soul-bootstrap 模块已经集成了这两个插件,所以启动 soul-admin 后,再启动 soul-bootstrap 就可以了。

接下来愉快地启动示例吧

运行 SoulTestHttpApplication.java,启动示例模块

通过控制台日志可以看到,模块启动后向网关注册了5条 http-client 元数据。

其中 HttpTestController 统一注册了1条元数据 /http/test/**,而 OrderController 则分别注册了4条元数据。

猜测元数据记录与 @SoulSpringMvcClient 注解一一对应。

登录 http://localhost:9095/#/plug/divide,在 divide 插件一栏可以看到 http 服务注册信息

点击 Modify 按钮可以查看或修改注册的元数据信息

7、http 请求 网关

此处使用 Postman 发起 http 请求,与浏览器直接发起请求等价。

1)先尝试请求原 http 服务

待访问 http 服务: /test/findByUserId,请求url:localhost:8188/test/findByUserId?userId=001

访问成功,耗时 12ms

2)再通过网关访问代理的 http 服务

网关 ip + 端口:http://localhost:9195

contextPath:/http

待访问 http 服务: /test/findByUserId

要素拼装后请求url:localhost:9195/http/test/findByUserId?userId=001

访问成功,耗时 50ms

至此,网关成功代理 http 服务,注意到网关代理服务耗时确实比原 http 服务长,毕竟多了一次路由转发,性能方面待后续压测。

三、总结

http 服务接入 soul 网关:

  • 先启动 soul-admin

  • 网关需要引入相关代理插件并重启,若使用 soul-bootstrap 则直接 soul-bootstrap 启动即可

  • http 服务接入网关方式:

    • soul-client方式,自动注册元信息

    • divide 插件方式,手动配置元信息

    • 自定义 http-client 方式

  • 将直接指向用户的请求,改为通过网关发起访问,并加上相应的 contextPath。

参考文章

个人知识库

高性能微服务API网关-Soul

Soul API 网关源码解析 02的更多相关文章

  1. Soul API 网关源码解析 03

    目标 使用 soul 代理 dubbo 服务 dubbo 服务如何注册到网关的? dubbo 插件是如何工作的? 理清 http --> 网关--> dubbo provider 整条链路 ...

  2. zuul网关源码解析

    zuul网关源码解析 zuul请求的生命周期 ZuulServlet ZuulServlet定义了对zuul整个过程的处理,如下: public void service(javax.servlet. ...

  3. Spring中AOP相关的API及源码解析

    Spring中AOP相关的API及源码解析 本系列文章: 读源码,我们可以从第一行读起 你知道Spring是怎么解析配置类的吗? 配置类为什么要添加@Configuration注解? 谈谈Spring ...

  4. Spring源码解析02:Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

  5. Soul 网关 Nacos 数据同步源码解析

    学习目标: 学习Soul 网关 Nacos 数据同步源码解析 学习内容: 环境配置 Soul 网关 Nacos 数据同步基本概念 源码分析 学习时间:2020年1月28号 早7点 学习产出: 环境配置 ...

  6. sqler sql 转rest api 源码解析(一)应用的启动入口

    sqler sql 转rest api 的源码还是比较简单的,没有比较复杂的设计,大部分都是基于开源 模块实现的. 说明: 当前的版本为2.0,代码使用go mod 进行包管理,如果本地运行注意gol ...

  7. Istio技术与实践02:源码解析之Istio on Kubernetes 统一服务发现

    前言 文章Istio技术与实践01: 源码解析之Pilot多云平台服务发现机制结合Pilot的代码实现介绍了Istio的抽象服务模型和基于该模型的数据结构定义,了解到Istio上只是定义的服务发现的接 ...

  8. EventBus源码解析 源码阅读记录

    EventBus源码阅读记录 repo地址: greenrobot/EventBus EventBus的构造 双重加锁的单例. static volatile EventBus defaultInst ...

  9. Java 集合系列13之 WeakHashMap详细介绍(源码解析)和使用示例

    概要 这一章,我们对WeakHashMap进行学习.我们先对WeakHashMap有个整体认识,然后再学习它的源码,最后再通过实例来学会使用WeakHashMap.第1部分 WeakHashMap介绍 ...

随机推荐

  1. Autofac的基本使用---5、常用配置

    Autofac的基本使用---目录 创建实例方法 参考:http://www.cnblogs.com/manglu/p/4115128.html InstancePerDependency 对每一个依 ...

  2. python初学者-使用for循环用四位数组成不同的数

    digits = (1,2,3,4) for i in digits: for j in digits: if j==i: continuefor k in digits: if k==i or k= ...

  3. 关于GDPR,你需要了解的的5件事

    GDPR要求组织确保对用户数据进行良好的保护,而不是滥用,使用户获得知情同意,并且违规行为将受到巨额罚款. 欧盟通用数据保护条例(GDPR)于2018年5月25日开始执行.然而直到如今,还是有不少人对 ...

  4. SpringBoot 的多数据源配置

    最近在项目开发中,需要为一个使用 MySQL 数据库的 SpringBoot 项目,新添加一个 PLSQL 数据库数据源,那么就需要进行 SpringBoot 的多数据源开发.代码很简单,下面是实现的 ...

  5. 【Azure Application Insights】在Azure Function中启用Application Insights后,如何配置不输出某些日志到AI 的Trace中

    问题描述 基于.NET Core的Function App如果配置了Application Insights之后,每有一个函数被执行,则在Application Insights中的Logs中的tra ...

  6. 宿主机网络中其它机器与Docker容器网络互通配置

    前言 目前项目采用微服务架构进行开发,Nacos和其它服务部署到Docker中,Docker中容器采用的网络默认是桥接模式(Bridge),默认的子网码是172.17.0.1/16:宿主机是192.1 ...

  7. NIO非阻塞式编程

    /** * NIO非阻塞式编程<p> * 服务端和客户端各自维护一个管理通道的对象,我们称之为selector,该对象能检测一个或多个通道 (channel) 上的事件. * 我们以服务端 ...

  8. 记一次由于引用第三方服务导致的GC overhead limit exceeded异常

    最近笔者遇到一个问题  监控平台忽然告警 GC overhead limit exceeded 这个异常 第一反应估计是堆溢出了.于是各种各种jmap  jstack下载堆栈文件和堆日志文件. 以下是 ...

  9. SonarQube学习(三)- 项目代码扫描

    一.前言 元旦三天假,两天半都在玩86版本DNF,不得不说,这个服真的粘度太高了,但是真的很良心. 说明: 注册账号上线100w点券,一身+15红字史诗装备以及+21强化新手武器.在线泡点一分钟888 ...

  10. YuebonCore 2021第一版发布,重构功能菜单模块

    YuebonCore 2021.1.6 版本发布了,这是该软件 2021 年的首个版本更新. 对功能菜单模块进行重构,简化功能模块的新增配置,删除了原有的sys_function功能表,,优化sys_ ...