自动配置

版本日志

v0.0.1

功能
  • 接口自动配置
接入
引入依赖
<dependency>
    <groupId>com.eazytec</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</dependency>
配置yml
tng.md:
  egateway:
    #自动注册配置
    mapping:
      #当前项目发布后的域名,不得为空
      domain: https://test-console.app.zqtong.com
      #处理自动注册的接口地址,不得为空,不同项目不同环境的地址都不同,由平台组提供,此处为宜兴政企通测试环境专用地址
      autoAddUrl: https://test-gateway.eazytec-cloud.com/YX/tng-boot/md/egateway/mdGatewayApis/add/auto
      #网关映射时的统一前缀,不得为空,必须以/开头,不得以/结尾
      prefix: /ZQT_test
      #开关(on-开,off-关),开关关闭时,项目启动将不执行自动配置逻辑,建议正式环境将开关打开,其他环境发布频繁的可以关闭,在需要配置时本地打开开关启动一次项目即可
      tap: on

#项目上下文配置(按需配置,可有可无)
server:
  servlet:
    context-path: /tng-boot
注解使用
在需要配置网关的方法上加上@GatewayMapping注解,例:

    @RestController
    @RequestMapping("/md/test")
    public class MdGatewayApisController {
        @PostMapping(value = "/add")
        @GatewayMapping
        public BizRes add() {
            return BizRes.success();
        }
    }

按如上配置,项目启动时会在网关中添加映射:
网关地址:https://test-gateway.eazytec-cloud.com/ZQT_test/tng-boot/md/test/add
映射地址:https://test-console.app.zqtong.com/tng-boot/md/test/add
Method支持:POST,OPTIONS
自定义注解参数
@RestController
@RequestMapping("/md/test")
public class MdGatewayApisController {
    @GetMapping(value = "/list")
    @GatewayMapping(name="testAuto", api_desc = "测试增删改查", upstream_url = "/tng-boot/md/test",
                uris = "/ZQT_test/tng-boot/md/test", methods = "GET,POST,DELETE,PUT,OPTIONS",
                category = "md相关接口")
    public BizRes list() {
        return BizRes.success();
    }

    @PostMapping(value = "/add")
    public BizRes add() {
        return BizRes.success();
    }

    @DeleteMapping(value = "/delete/{id}")
    public BizRes delete(@PathVariable("id") String id) {
        return BizRes.success();
    }
}
可以通过自定义注解参数来控制网关接口细化程度,此处配置到/tng-boot/md/test即将整个controller的接口配置成一个网关
注解参数说明
name: 接口名称,默认为方法名+下划线+UUID
api_desc: 接口描述,默认为默认描述
upstream_url: 映射路由,默认为当前项目上下文+当前controller的requestMapping路由+当前方法的requestMapping路由,若包含/{xxx}的路径参数,则会被截取到路径参数前
uris: 网关路由,默认为配置的统一前缀+默认映射路由
methods: 请求方式,默认为当前方法RequestMapping注解中的方式+OPTIONS
category: 所属分类,默认为空,分类若不存在会自动创建
以上参数皆可手动配置,除分类、描述外其他参数建议使用默认值

v0.0.2

功能
  • 接口自动配置
引入依赖
<dependency>
    <groupId>com.eazytec</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.2-SNAPSHOT</version>
</dependency>
新增功能
新增批量配置注解@GatewayScan,用于springboot项目启动类
注解使用
@Slf4j
@EnableSwagger2
@SpringBootApplication
@EnableAutoConfiguration
@EnableAsync
@EnableCaching
@GatewayScan(batch_uris={"/biz/permission","/biz/admin/statistics"}, batch_category = "应用权限", batch_desc = "权限相关")
public class TngBootApplication {
  public static void main(String[] args) throws UnknownHostException {
    ConfigurableApplicationContext application = SpringApplication.run(TngBootApplication.class, args);
    Environment env = application.getEnvironment();
    String ip = InetAddress.getLocalHost().getHostAddress();
    String port = env.getProperty("server.port");
    String path = env.getProperty("server.servlet.context-path");
  }
}

在启动类上配置该注解,效果等同于对所有路由中包含"/biz/permission"或"/biz/admin/statistics"路由的方法加上@GatewayMapping(category = "应用权限", api_desc = "权限相关")
注解参数说明
batch_uris: 路由匹配值,支持多项配置
batch_desc: 接口描述,默认为默认描述
batch_category: 所属分类,默认为空,分类若不存在会自动创建
江苏卓易信息科技股份有限公司-TNG基础平台组 all right reserved,powered by Gitbook该文件最后修改时间: 2021-01-27 06:23:28

results matching ""

    No results matching ""