关于java:Soul-API-网关源码解析-02

3次阅读

共计 8234 个字符,预计需要花费 21 分钟才能阅读完成。

如何读开源我的项目:对着文档跑 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 形式接入
  • divide 插件手动配置形式,参见 选择器规定介绍
  • 自定义 http-client 形式,参见 多语言 Http 客户端开发

4、用户申请

申请形式有两点变动:

  • 批改 ip + 端口

    须要将 间接指向用户的申请,改为通过网关发动拜访。

  • 加上 context path

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

例如:

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

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

  • http://localhost:9195 为网关 ip + 端口,留神默认端口为 9195
  • /order 为配置的 contextpath

二、官网示例剖析

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。

参考文章

  • soul 官网文档
  • soul 网关 http 用户接入应用教程

集体知识库

高性能微服务 API 网关 -Soul

正文完
 0