首页
  • 2024.1.1.0
  • 2024.1.1.1
  • 2024.1.2.0
  • 2024.1.3.0
  • Java
  • Linux
  • Deploy
  • Application
关于
友情链接
GitHub (opens new window)

Xiao ku

板砖师傅
首页
  • 2024.1.1.0
  • 2024.1.1.1
  • 2024.1.2.0
  • 2024.1.3.0
  • Java
  • Linux
  • Deploy
  • Application
关于
友情链接
GitHub (opens new window)
  • 版本日志
  • 贡献清单
  • 2024.1.3.0

  • 2024.1.2.0

    • Simple-Starter概述
    • 自动装配模块

      • simple-starter-web
      • simple-starter-redisson
      • simple-starter-s3
      • simple-starter-socketio
      • simple-starter-grpc
      • simple-starter-datasource
      • simple-starter-freemarker
      • simple-starter-cloud
      • simple-starter-encrypt
      • simple-starter-dict
      • simple-starter-email
      • simple-starter-gateway
        • 简介
        • 网关聚合文档效果
          • 支持网关配置过滤器为->将请求的路径前缀去除
        • 引入依赖
        • 配置项
          • Nacos远程配置中心
          • 本地工程配置
          • Add VM options
          • 效果图
          • 网关聚合微服务列表
          • 网关聚合微服务接口
      • simple-starter-package
    • 服务类模块

    • 工具类模块

  • 2024.1.1.1

  • 2024.1.1.0

  • OpenSource
  • 2024.1.2.0
  • 自动装配模块
xiaoku
2023-07-07
目录

simple-starter-gateway

# 简介

将常用微服务网关的依赖集成,便于使用
增加网关聚合文档兼容性,支持网关配置过滤器为->将请求的路径前缀去除

# 网关聚合文档效果

# 支持网关配置过滤器为->将请求的路径前缀去除

网关效果如下:

单体服务 接口地址 访问http://localhost:11100/simple-demo-cloud/hello/world
通过网关 接口地址 访问http://localhost:11000/simple-demo-cloud/hello/world
1
2

网关配置

spring:
  application:
    name: simple-demo-gateway
  cloud:
    nacos:
    # 路由网关配置
    gateway:
      # 启用了自动根据服务名建立路由
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
          # 配置过滤器为->将请求的路径前缀去除
          filters:
            - name: StripPrefix
              args:
                parts: 0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

聚合文档兼容效果:
打开网关接口文档地址: http://localhost:11000/doc.html
展示接口地址内容与微服务文档地址内容一致:http://localhost:11100/doc.html

注:微服务接口代码,Mapping中需包含微服务名称,网关才能正确转发:

@RestController
@RequestMapping("/simple-demo-cloud/hello")
@Tag(name = "你好")
public class HelloController {

    @Operation(summary = "世界")
    @GetMapping(value = "/world")
    public String world() {
        return "simple-demo-cloud: hello world";
    }

}
1
2
3
4
5
6
7
8
9
10
11
12

不在配置文件中写 server.servlet.context-path=simple-demo-cloud 的原因为:

当所有微服务融合为单体工程时,接口地址将发生根本性改变,从而增加修改调用接口地址的工作量。

微服务是否需要融合为单体工程,可视服务器资源情况决定

兼容该功能涉及代码

simple-starter-web (opens new window)

simple-starter-gateway (opens new window)

# 引入依赖

    <dependencies>
        <dependency>
            <groupId>cn.iosd</groupId>
            <artifactId>simple-starter-gateway</artifactId>
            <version>Version</version>
        </dependency>
    </dependencies>
1
2
3
4
5
6
7

# 配置项

# Nacos远程配置中心

示例:application.yml 公共-通用配置

simple:
  openApi:
    version: 1.0.0
    contact:
      name: ok1996
      url: https://ok96.cn
      email: ku29@qq.com
1
2
3
4
5
6
7

# 本地工程配置

application.yml

server:
  port: 11000
spring:
  application:
    name: simple-demo-gateway
  cloud:
    nacos:
      server-addr: http://127.0.0.1:8848
      username:
      password:
      discovery:
        registerEnabled: true
        namespace:
      config:
        namespace:
        file-extension: yml
    # 路由网关配置
    gateway:
      # 启用了自动根据服务名建立路由
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
  config:
    import:
      - nacos:application.yml
knife4j:
  gateway:
    enabled: true
    strategy: discover
    discover:
      enabled: true
      version: openapi3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# Add VM options

-Dspring.cloud.nacos.server-addr=
-Dspring.cloud.nacos.username=nacos
-Dspring.cloud.nacos.password=
-Dspring.cloud.nacos.discovery.namespace=
-Dspring.cloud.nacos.config.namespace=
1
2
3
4
5

# 效果图

# 网关聚合微服务列表

效果图

# 网关聚合微服务接口

网关页面调用接口-端口11000 效果图 单体服务调用接口-端口11010 效果图

上次更新: 2024/03/25, 02:28:08
simple-starter-email
simple-starter-package

← simple-starter-email simple-starter-package→

Copyright © 2019-2024
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式