Skip to main content
版本:2.4.0

Http服务接入

本文档旨在帮助 http 服务接入到 Apache ShenYu 网关。Apache ShenYu 网关使用 divide 插件来处理 http 请求。

接入前,请正确启动 shenyu-admin,并开启divide插件,在网关端和Http服务端引入相关依赖。可以参考前面的 Http快速开始

应用客户端接入的相关配置请参考:客户端接入配置

数据同步的相关配置请参考:数据同步配置

在网关中引入 divide 插件

  • 在网关的 pom.xml 文件中增加如下依赖:
    <dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-plugin-divide</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-spring-boot-starter-plugin-httpclient</artifactId>
<version>${project.version}</version>
</dependency>

Http请求接入网关(springMvc 体系用户)

可以参考:shenyu-examples-http

  • SpringBoot 用户

    1. 在你的http服务中的 pom.xml文件 新增如下依赖:
      <dependency>
    <groupId>org.apache.shenyu</groupId>
    <artifactId>shenyu-spring-boot-starter-client-springmvc</artifactId>
    <version>${shenyu.version}</version>
    </dependency>
    1. 在 application.yaml 增加如下配置:
    shenyu:
    client:
    registerType: http #zookeeper #etcd #nacos #consul
    serverLists: http://localhost:9095 #localhost:2181 #http://localhost:2379 #localhost:8848
    props:
    contextPath: /http
    appName: http
    port: 8189
    nacosNameSpace: ShenyuRegisterCenter
  • SpringMvc 用户

    在你的http服务中的 pom.xml文件 新增如下依赖:

       <dependency>
<groupId>org.apache.shenyu</groupId>
<artifactId>shenyu-client-springmvc</artifactId>
<version>${shenyu.version}</version>
</dependency>

并在你的 bean 定义的 xml 文件中新增如下:

   <bean id ="springMvcClientBeanPostProcessor" class ="org.apache.shenyu.client.springmvc.init.SpringMvcClientBeanPostProcessor">
<constructor-arg ref="shenyuRegisterCenterConfig"/>
</bean>

<bean id="shenyuRegisterCenterConfig" class="org.apache.shenyu.register.common.config.ShenyuRegisterCenterConfig">
<property name="registerType" value="http"/>
<property name="serverList" value="http://localhost:9095"/>
<property name="props">
<map>
<entry key="contextPath" value="/你的contextPath"/>
<entry key="appName" value="你的名字"/>
<entry key="port" value="你的端口"/>
<entry key="isFull" value="false"/>
</map>
</property>
</bean>

在你的 controller 的接口上加上 @ShenyuSpringMvcClient 注解。

你可以把注解加到 Controller 类上面,里面的path属性则为前缀,如果含有 /** 代表你的整个接口需要被网关代理。

示例一

下面表示的是 /test/payment/test/findByUserId 都会被网关代理。

  @RestController
@RequestMapping("/test")
@ShenyuSpringMvcClient(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;
}
}

示例二

下面表示的是: /order/save 会被网关代理,而 /order/findById 则不会。

  @RestController
@RequestMapping("/order")
@ShenyuSpringMvcClient(path = "/order")
public class OrderController {

@PostMapping("/save")
@ShenyuSpringMvcClient(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;
}
}
  • 启动你的项目,你的服务接口接入到了网关,进入shenyu-admin后台管理系统的插件列表 -> http process -> divide,看到自动创建的选择器和规则。

Http请求接入网关(其他语言,非springMvc体系)

  • 首先在 shenyu-admin 找到 divide 插件,进行选择器,和规则的添加,进行流量的匹配筛选。

  • 如果不懂怎么配置,请参考 选择器和规则管理

  • 您也可以自定义开发属于你的 http-client,参考 多语言 Http 客户端开发

用户请求

当你的Http服务接入到Apache ShenYu网关后,请求方式没有很大的变动,小的改动有两点。

  • 第一点,你之前请求的域名是你自己的服务,现在要换成网关的域名。

  • 第二点,Apache ShenYu 网关需要有一个路由前缀,这个路由前缀就是你接入项目进行配置 contextPath,如果熟的话,可以在 shenyu-admin 中的divide插件进行自由更改。

    • 比如你有一个 order 服务 它有一个接口,请求路径 http://localhost:8080/test/save

    • 现在就需要换成:http://localhost:9195/order/test/save

    • 其中 localhost:9195 为网关的ip端口,默认端口是9195/order 是你接入网关配置的 contextPath

    • 其他参数,请求方式不变。

然后你就可以进行访问了,如此的方便与简单。