可扩展、高性能、响应迅速的API网关——Soul进行http协议转换SpringCloud服务(五)

本文章将成系列介绍:包含但不限于高性能微服务API网关Soul的环境搭建、源码设计

本章节介绍使用soul进行http协议转换SpringCloud服务

配置方式

网关接入方式
  • 在网关的 pom.xml 文件中引入如下依赖。
  <!--soul springCloud plugin start-->
  <dependency>
       <groupId>org.dromara</groupId>
       <artifactId>soul-spring-boot-starter-plugin-springcloud</artifactId>
        <version>${last.version}</version>
  </dependency>

  <dependency>
       <groupId>org.dromara</groupId>
       <artifactId>soul-spring-boot-starter-plugin-httpclient</artifactId>
       <version>${last.version}</version>
   </dependency>
   <!--soul springCloud plugin end-->

   <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>
  • 如果你使用 eureka 作为 springCloud的注册中心
    • 新增如下依赖:
  <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
       <version>2.2.0.RELEASE</version>
  </dependency>
  • 在网关的yml文件中 新增如下配置:
   eureka:
     client:
       serviceUrl:
         defaultZone: http://localhost:8761/eureka/ # 你的eureka地址
     instance:
       prefer-ip-address: true
  • 如果你使用 nacos 作为 springCloud的注册中心
    • 新增如下依赖:
 <dependency>
       <groupId>com.alibaba.cloud</groupId>
       <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
       <version>2.1.0.RELEASE</version>
 </dependency>
  • 在网关的yml文件中 新增如下配置:
  spring:
     cloud:
       nacos:
         discovery:
            server-addr: 127.0.0.1:8848 # 你的nacos地址
  • 重启你的网关服务。
SpringCloud服务接入网关
  • 在你提供服务的项目中,引入如下依赖:
 <dependency>
      <groupId>org.dromara</groupId>
      <artifactId>soul-spring-boot-starter-client-springcloud</artifactId>
      <version>${last.version}</version>
 </dependency>
  • 在你的yml文件中新增如下配置:
soul:
  springcloud:
    admin-url: http://localhost:9095
    context-path: /springcloud
    full: true
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加http://
# contextPath: 为你的这个mvc项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# full: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
  • 在你的 controller的接口上加上 @SoulSpringCloudClient 注解
  • 你可以把注解加到 Controller 类上面, 里面的path属性则为前缀,如果含有 /** 代表你的整个接口需要被网关代理
    • 举列子 (1): 代表 /test/payment, /test/findByUserId 都会被网关代理。
 @RestController
 @RequestMapping("/test")
 @SoulSpringCloudClient(path = "/test/**")
 public class HttpTestController {

     @PostMapping("/payment")
     public UserDTO post(@RequestBody final UserDTO userDTO) {
         return userDTO;
     }

     @GetMapping("/findByUserId")
     public UserDTO findByUserId(@RequestParam("userId") final String userId) {
         UserDTO userDTO = new UserDTO();
         userDTO.setUserId(userId);
         userDTO.setUserName("hello world");
         return userDTO;
     }
  }
  • 举列子 (2): 代表 /order/save,会被网关代理,而/order/findById 则不会。
 @RestController
 @RequestMapping("/order")
 @SoulSpringCloudClient(path = "/order")
 public class OrderController {

     @PostMapping("/save")
     @SoulSpringMvcClient(path = "/save")
     public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
         orderDTO.setName("hello world save order");
         return orderDTO;
     }

     @GetMapping("/findById")
     public OrderDTO findById(@RequestParam("id") final String id) {
         OrderDTO orderDTO = new OrderDTO();
         orderDTO.setId(id);
         orderDTO.setName("hello world findById");
         return orderDTO;
     }
 }
  • 举列子 (3): full:true 代表 /sb-demo7-api/**,整个服务会被网关代理
soul:
  springcloud:
    admin-url: http://localhost:9095
    context-path: /sb-demo7-api
    full: true
# adminUrl: 为你启动的soul-admin 项目的ip + 端口,注意要加http://
# contextPath: 为你的这个mvc项目在soul网关的路由前缀,这个你应该懂意思把? 比如/order ,/product 等等,网关会根据你的这个前缀来进行路由.
# full: 设置true 代表代理你的整个服务,false表示代理你其中某几个controller
 @RestController
 @RequestMapping("/order")
 public class OrderController {

     @PostMapping("/save")
     @SoulSpringMvcClient(path = "/save")
     public OrderDTO save(@RequestBody final OrderDTO orderDTO) {
         orderDTO.setName("hello world save order");
         return orderDTO;
     }

     @GetMapping("/findById")
     public OrderDTO findById(@RequestParam("id") final String id) {
         OrderDTO orderDTO = new OrderDTO();
         orderDTO.setId(id);
         orderDTO.setName("hello world findById");
         return orderDTO;
     }
 }
  • 启动你的服务,如果输出以下日志:http client register success,证明你的接口已经被注册到soul网关。

源码跟踪

今天跟踪从网关启动,数据初始化开始

启动网关时:SoulConfiguration加载bean(顺序问题);

List<SoulPlugin> pluginList = plugins.getIfAvailable(Collections::emptyList);
//根据pom.xml引入多少个插件决定

SoulWebHandler.handle–>new DefaultSoulPluginChain(plugins).execute–>plugin.execute(按照pom文件配置的插件链式执行),在AbstractSoulPlugin的execute方法判断(pluginData != null && pluginData.getEnabled()),若为true,则获取plugin的选择器及规则数据进行uri匹配,之后执行对应plugin的doExecute方法(负载均衡到真实URL),之后链式回调结束。

注:

  • @RequiredArgsConstructor:https://www.cnblogs.com/ch3ny/p/13901715.html

    ObjectProvider:

    Spring 4.3版本,不仅隐式的注入了单构造参数的属性。还引入了ObjectProvider接口。

    ObjectProvider接口是ObjectFactory接口的扩展,专门为注入点设计的,可以让注入变得更加宽松和更具有可选项。

    public interface ObjectProvider extends ObjectFactory, Iterable {…

    那么什么时候使用ObjectProvider接口?

    如果待注入参数的Bean为空或有多个时,便是ObjectProvider发挥作用的时候了。

    • 如果注入实例为空时,使用ObjectProvider则避免了强依赖导致的依赖对象不存在异常;
    • 如果有多个实例,ObjectProvider的方法会根据Bean实现的Ordered接口或@Order注解指定的先后顺序获取一个Bean。从而了提供了一个更加宽松的依赖注入方式。

    ObjectProvider解决的问题:

    问题一:

    容器中没有Bean时,抛出Parameter 0 of constructor in com.example.demo.FooServicerequired a bean of type ‘com.example.demo.FooRepository’ that could not be found.

    问题二:

    容器中存在多个Bean时,抛出No qualifying bean of type ‘com.example.demo.FooRepository’’ available: expected single matching bean but found 2

    当容器存在多个Bean,我们可以调用它的流式方法获取一个自己想要的依赖。

  • 根据网关pom.xml引入的jar加载

    GlobalPlugin
    SignPlugin
    WafPlugin
    RateLimiterPlugin
    HystrixPlugin
    Resilience4JPlugin
    DividePlugin
    SpringCloudPlugin
    WebClientPlugin
    WebSocketPlugin
    MonitorPlugin
    WebClientResponsePlugin

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值