如何使用 swagger 和 spring 引导框架将客户端代码集成到服务器内部

How to integrate client code inside server with swagger and spring boot framework

我是整个微服务世界的新手,但我的任务是开发微服务。规范是用 swagger(现在是 OpenAPI)创建的,生成的代码在 spring 引导框架中。

我的具体问题可以转换为更通用的问题。假设有两个微服务。让我们称它们为 "Token service" 和 "Blacklist Service"。客户端从 "Token service" 请求令牌,然后它将检查 "Blacklist Service" 以了解用户是否已被列入黑名单。如果没有被列入黑名单,客户端将获得一个令牌;否则请求被拒绝。我可以使用 swagger 创建这两个微服务,它们 运行 独立正常。

我的问题如下。一旦客户端请求令牌,"Token service" 需要与 "Blacklist Service" 进行核对。 swagger 规范生成客户端和服务器代码来执行此操作。我将如何将 "Blacklist Service" 的客户端代码与 "Token service" 的服务器代码集成。我找不到任何好的资源来解释这一点。下面给出了我的示例 yml 文件。

token.yml

swagger: '2.0'
info:
  description: Get token
  version: 0.0.1
  title: Token Service
host: 'localhost:8080'
basePath: /test/v1.0
schemes:
  - http
paths:
  /Create:
    get:
      summary: Get token
      parameters:
        - name: payload
          in: body
          description: The person requesting for token
          schema:
            type: object
            properties:
              name:
                type: string
      responses:
        '200':
          description: OK
          schema:
            type: object
            properties:
              name:
                type: string
              token:
                type: string
        '403':
          description: Forbidden

blacklist.yml

swagger: '2.0'
info:
  description: Blacklist Service
  version: 0.0.1
  title: Blacklist Service
host: 'localhost:8081'
basePath: /test/v1.0
schemes:
  - http
paths:
  /blacklist:
    get:
      summary: Check whether the user is blacklisted
      parameters:
        - name: payload
          in: body
          description: The name of user
          schema:
            type: object
            properties:
              name:
                type: string
      responses:
        '200':
          description: OK
        '404':
          description: Not found

或者有什么方法可以在 yml 文件本身中执行此操作,让 swagger 知道另一个服务的客户端代码需要与服务器一起生成。

可以说在这种情况下不需要 "Blacklist Service"。但我原来的问题需要与另一项服务核实。 在 Windows 10

上将 OpenAPI 2.0 与 Java 8 一起使用

您可以使用Rest Template调用另一个微服务。 请参考这个休息电话。

https://howtodoinjava.com/spring/spring-restful/spring-restful-client-resttemplate-example/