swagger 微服务_笔记本微服务和Swagger

news/2024/7/10 3:17:27 标签: python, java, docker, vue, go

swagger 微服务

In previous posts we learned how to create a microservice in a notebook using the Jupyter kernel gateway. This will be the foundation for today’s post where we will be creating a notebook microservice with Swagger, a set of tools for representing REST APIs. With this this approach, notebook authors can create and deploy APIs that are easy-to-comsume by other developers.

在以前的文章中,我们学习了如何使用Jupyter内核网关在笔记本中创建微服务。 这将是今天这篇文章的基础,我们将使用Swagger创建笔记本微服务, Swagger是一套用于表示REST API的工具。 使用这种方法,笔记本作者可以创建和部署其他开发人员易于使用的API。

There are many components to Swagger, but today we will be using three. The first component, the Swagger Specification, is a specification we will use to describe out microservice. The second is a code generation plugin to generate our notebook. And finally, we will use the Swagger UI to interact with our newly deployed service.

Swagger有很多组件,但是今天我们将使用三个组件。 第一个组件Swagger规范是我们将用来描述微服务的规范。 第二个是用于生成笔记本的代码生成插件。 最后,我们将使用Swagger UI与我们新部署的服务进行交互。

设置和介绍 (Setup and Introduction)

There is some initial setup needed for this demo:

该演示需要一些初始设置:

While all of that is installing, let’s look at reasons for integrating Swagger in our microservice workflow.

在所有这些安装之后,让我们看一下将Swagger集成到我们的微服务工作流程中的原因。

  • Clear API Definition – The Swagger Specification is a standard way for describing our microservice API. This allows for the rapid and collaborative design of what our API will look like.
  • Quick Code Generation – Using a standard way to describe our microservice opens up a suite of tools for generating code. Today we will generate a notebook, but there are also tools for generating other client and server side pieces of code.
  • Easy API Testing – Another advantage of describing our microservice in a standard way is the use of tools for testing. An example of this is the Swagger UI, which we will use today, for manually verifying the behavior of API endpoints.
  • 清晰的API定义 – Swagger规范是描述我们的微服务API的标准方法。 这样可以快速,协作地设计我们的API外观。
  • 快速代码生成 –使用一种标准的方式来描述我们的微服务,从而打开了一套用于生成代码的工具。 今天,我们将生成一个笔记本,但是还有一些工具可用于生成其他客户端和服务器端代码。
  • 轻松进行API测试 –以标准方式描述我们的微服务的另一个优势是使用测试工具。 一个示例是Swagger UI,我们今天将使用它来手动验证API端点的行为。

设计微服务 (Designing The Microservice)

Our microservice will be used to create and get random quotes. Our service is defined by the following API endpoints:

我们的微服务将用于创建和获取随机报价。 我们的服务由以下API端点定义:

  • An endpoint to display random quote
  • An endpoint to add new quotes
  • And endpoint to get a quote
  • An endpoint to update a quote
  • 显示随机报价的端点
  • 端点以添加新报价
  • 和端点获取报价
  • 端点更新报价

We now need a way to describe this API so Swagger can generate a specification for us. I used the Swagger Editor to design this API in YAML. The code snippet below is the definition for the full API specification.

现在,我们需要一种描述此API的方法,以便Swagger可以为我们生成规范。 我使用Swagger编辑器在YAML中设计了此API。 以下代码段是完整API规范的定义。

1

1

2

2

3

3

4

4

5

5

6

6

7

7

8

8

9

9

10

10

11

11

12

12

13

13

14

14

15

15

16

16

17

17

18

18

19

19

20

20

21

21

22

22

23

23

24

24

25

25

26

26

27

27

28

28

29

29

30

30

31

31

32

32

33

33

34

34

35

35

36

36

37

37

38

38

39

39

40

40

41

41

42

42

43

43

44

44

45

45

46

46

47

47

48

48

49

49

50

50

51

51

52

52

53

53

54

54

55

55

56

56

57

57

58

58

59

59

60

60

61

61

62

62

63

63

64

64

swagger: ‘2.0’

swagger : ‘2.0’

info:

info :

  version: 0.0.1

   version : 0.0.1

  title: ‘Words Of Wisdom’

   title : ‘Words Of Wisdom’

  description: An API for recording and reflecting quotes.

   description : An API for recording and reflecting quotes.

  contact:

   contact :

    name: Corey Stubbs

     name : Corey Stubbs

    email: cstubbs@us.ibm.com

     email : cstubbs@us.ibm.com

    url: ‘https://github.com/Lull3rSkat3r’

     url : ‘https ://github.com/Lull3rSkat3r’

paths:

paths :

  /random:

  / random :

    get:

     get :

      description: Returns a random quote

       description : Returns a random quote

      responses:

       responses :

        ‘200’:

         ‘200’ :

          description: A string containing the quote

           description : A string containing the quote

          schema:

           schema :

            type: string

             type : string

  /quote:

  / quote :

    post:

     post :

      description: Adds a quote

       description : Adds a quote

      parameters:

       parameters :

        – name: body

         – name : body

          in: body

           in : body

          description: The quote to be added to the collection

           description : The quote to be added to the collection

          required: true

           required : true

          schema:

           schema :

            type: string

             type : string

      responses:

       responses :

        ‘201’:

         ‘201’ :

          description: Returns the id of the newly create quote, which can be used to access the quote

           description : Returns the id of the newly create quote , which can be used to access the quote

          schema:

           schema :

            type: integer

             type : integer

  /quote/{id}:

  /quote/ { id } :

    get:

     get :

      description: Gets the quote

       description : Gets the quote

      parameters:

       parameters :

        – name: id

         – name : id

          in: path

           in : path

          description: The quote to be added to the collection

           description : The quote to be added to the collection

          required: true

           required : true

          type: string

           type : string

      responses:

       responses :

        ‘201’:

         ‘201’ :

          description: Returns the id of the quote, which can be used to access the quote

           description : Returns the id of the quote , which can be used to access the quote

          schema:

           schema :

            type: integer

             type : integer

    put:

     put :

      description: Updates the content of quote.

       description : Updates the content of quote.

      parameters:

       parameters :

        – name: id

         – name : id

          in: path

           in : path

          description: The quote to be added to the collection

           description : The quote to be added to the collection

          required: true

           required : true

          type: string

           type : string

        – name: body

         – name : body

          in: body

           in : body

          description: The new value of the quote

           description : The new value of the quote

          required: true

           required : true

          schema:

           schema :

            type: string

             type : string

      responses:

       responses :

        ‘204’:

         ‘204’ :

          description: The quote has successfully been updated.

           description : The quote has successfully been updated.

With the microservice defined, we can export it to a JSON file. You can do this in the Swagger Editor by going to File -> ‘Download JSON’.

定义了微服务后,我们可以将其导出到JSON文件。 您可以在Swagger编辑器中执行此操作,方法是转到文件->'下载JSON'。

生成笔记本 (Generating The Notebook)

Once you have the JSON file describing the microservice, navigate to the kernel gateway demos project on your system. You will need to open a command line utility here and go to the directory swaggernotebookservice/jupyterswaggercodegen. From this directory we will generate the notebook to implement our microservice. The language for the generated notebook defaults to Python 3. You can generate the notebook with the following command:

拥有描述微服务的JSON文件后,导航至系统上的内核网关演示项目。 您将需要在这里打开命令行实用程序,并转到目录招摇 - 笔记本 - 服务 / jupyter - 招摇 - CODEGEN。 从该目录中,我们将生成笔记本以实现我们的微服务。 生成的笔记本的语言默认为Python3。您可以使用以下命令生成笔记本:

1

1

SWAGGER_SPEC=path/to/swagger.json make gen

SWAGGER_SPEC = path / to / swagger .json make gen

path/to/swagger.json  is the JSON file you downloaded earlier from the Swagger Editor. If you did not create your own swagger.json file, use this url to download a completed copy. The command should have generated the following files in target/swagger/python3/WordsOfWisdom/src/:

路径 / / 招摇 json是您先前从Swagger编辑器下载的JSON文件。 如果您没有创建自己的swagger.json文件,请使用此URL下载完整的副本。 该命令应该已经在target / swagger / python3 / WordsOfWisdom / src /中生成了以下文件:

  • WordsOfWisdomApi.ipynb – The notebook where the service will be implemented
  • package.sh – Bundles the notebook with the kernel gateway into a Docker container
  • run.sh –  Runs the Docker container built by package.sh
  • Dockerfile – A Dockerfile that defines Docker container image
  • WordsOfWisdomApi.ipynb –将在其中实现服务的笔记本
  • package.sh –将带有内核网关的笔记本捆绑到Docker容器中
  • run.sh –运行由package.sh构建的Docker容器
  • Dockerfile –定义Docker容器映像的Dockerfile

创作和运行微服务 (Authoring And Running The Microservice)

For the sake of brevity, there is a sample notebook with the implemented API endpoints. You can download this file and put the contents into the   WordsOfWisdomApi.ipynb file.

为简洁起见,这里有一个示例笔记本,其中包含已实现的API端点。 您可以下载此文件,并将内容放入WordsOfWisdomApi中 ipynb文件。

From here you can build and deploy the microservice locally with Docker by running:

在这里,您可以通过运行以下命令使用Docker在本地构建和部署微服务:

# Run the commands below within: target/swagger/python3/WordsOfWisdom/src/

# Run the commands below within: target/swagger/python3/WordsOfWisdom/src/

# Build the docker container

# Build the docker container

sh package.sh

sh package .sh

# Run the built container

# Run the built container

sh run.sh

sh run .sh

When everything is built correctly, you should see output similar to the following:

一切构建正确后,您应该会看到类似以下的输出:

[KernelGatewayApp] Registering endpoint_path: /random, methods: ([‘GET’])

[ KernelGatewayApp ] Registering endpoint_path : / random , methods : ( [ ‘GET’ ] )

[KernelGatewayApp] Registering endpoint_path: /quote, methods: ([‘POST’])

[ KernelGatewayApp ] Registering endpoint_path : / quote , methods : ( [ ‘POST’ ] )

[KernelGatewayApp] Registering endpoint_path: /quote/(?P<id>[^/]+), methods: ([‘PUT’, ‘GET’])

[ KernelGatewayApp ] Registering endpoint_path : / quote / ( ? P < id > [ ^ / ] + ) , methods : ( [ ‘PUT’ , ‘GET’ ] )

[KernelGatewayApp] The Jupyter Kernel Gateway is running at: http://0.0.0.0:8888

[ KernelGatewayApp ] The Jupyter Kernel Gateway is running at : http : //0.0.0.0:8888

With the service successfully running, you will be able to access the API endpoints. Use the ip address for your Docker host and go to:  http://YOUR_DOCKER_HOST:8888/random. This endpoint will display a random quote, served from our notebook.

服务成功运行后,您将能够访问API端点。 使用您的Docker主机的IP地址并转到: http // YOUR_DOCKER_HOST:8888 / random 。 该端点将显示一个随机报价,由我们的笔记本提供。

消耗Swagger规格 (Consuming The Swagger Spec)

Our final step will be to use the Swagger UI to interact with our API. The kernel gateway will automatically generate a simple Swagger Specification for consumption through the Swagger UI. The specification can be found at  http://YOUR_DOCKER_HOST:8888/_api/spec/swagger.json. You will need to enter the spec URL into the Swagger UI. The picture below shows how to use the Swagger UI to make requests to the microservice.

Swagger UI MicroserviceNote: The generated swagger spec is still under development and you will not be able to interact with all endpoints. As a temporary alternative, you can use the
Swagger Editor for interacting with the microservice. If you have a need for a requirement pull requests and issues are welcome.

我们的最后一步将是使用Swagger UI与我们的API进行交互。 内核网关将自动生成一个简单的Swagger规范,以通过Swagger UI进行使用。 可以在http // YOUR_DOCKER_HOST 8888 / _api / spec / swagger.json中找到该规范。 您将需要在Swagger UI中输入规范URL。 下图显示了如何使用Swagger UI向微服务发出请求。 注意:生成的swagger规范仍在开发中,您将无法与所有端点进行交互。 作为临时替代方案,您可以使用Swagger编辑器与微服务进行交互。 如果您有需求,欢迎提出要求和问题 。

摘要 (Summary)

In this post you have seen how integrating Swagger into the microservice workflow has helped our development process. If you have any questions or want to get involved with the kernel gateway visit our github repo.

在本文中,您了解了将Swagger集成到微服务工作流程中如何帮助我们的开发过程。 如果您有任何疑问或想参与内核网关,请访问我们的github存储库 。

学到更多 (Learn More)

Corey Stubbs
Corey Stubbs
Corey Stubbs

Lull3rSkat3r

Lull3rSkat3r

翻译自: https://www.pybloggers.com/2016/01/notebook-microservice-and-swagger/

swagger 微服务


http://www.niftyadmin.cn/n/984479.html

相关文章

后缀数组模板 SuffixArray

后缀数组模板 SuffixArray 1 #include<bits/stdc.h>2 #define rep(i,a,b) for(ia;i<b;i)3 #define drep(i,a,b) for(ib;i>a;--i)4 using namespace std;5 int p[N],wc[N],wv[N],wa[N],wb[N],rank[N],sa[N];6 char s[N];7 inline bool cmp(int *r,int a,int b,int l…

Hadoop - OutputFormat中OutputCommitter解析

在 Hadoop中&#xff0c;OutputFormat和InputFormat是相对应的两个东西。相比于InputFormat&#xff0c;OutputFormat似乎没 有那么多细节。InputFormat涉及到对输入数据的解析和划分&#xff0c;继而影响到Map任务的数目&#xff0c;以及Map任务的调度。而OutputFormat似乎像其…

jquery.md5.js

2019独角兽企业重金招聘Python工程师标准>>> <script type"text/javascript" src"./js/jquery.mini.js"></script> <script type"text/javascript" src"./js/jquery.md5.js"></script> <script…

fedora 开发_在Fedora上开发和部署Cookiecutter-Django

fedora 开发Last time we set up a Django Project using Cookiecutter, managed the application environment via Docker, and then deployed the app to Digital Ocean. In this tutorial, we’ll shift away from Docker and detail a development to deployment workflow …

HTTP - HTTP协议之chunk介绍

当客户端向服务器请求一个静态页面或者一张图片时&#xff0c;服务器可以很清楚的知道内容大小&#xff0c;然后通过Content-Length消息首部字段告诉客户端需要接收多少数据。但是如果是动态页面等时&#xff0c;服务器是不可能预先知道内容大小&#xff0c;这时就可以使用Tran…

extern字符串常量,宏定义字符串常量,怎么选

在使用常量的时候&#xff0c;我看到主要有两种写法&#xff1a; #define RKLICURegexEnumerationOptionsErrorKey "RKLICURegexEnumerationOptionsErrorKey" extern NSString * const RKLICURegexEnumerationOptionsErrorKey; 这两种写法孰优孰劣&#xff1f;或者在…

笔记之用户组及其权限·

一、3A认证&#xff1a; authentication:认证Authorization &#xff1a;授权audition&#xff1a;审计读取、使用权限&#xff0c;对于自己的资源有全部的权限两级分配机制&#xff0c;管理功能薄弱属主&#xff0c;一般是资源的拥有者&#xff0c;向文件标识他的属主&#x…

sql汇总子级数据汇总_用SQL汇总数据

sql汇总子级数据汇总About Matt: Matt DeLand is Co-Founder and Data Scientist at Wagon. His team is building a collaborative SQL editor for analysts and engineers. He studied algebraic geometry at Columbia University, taught at the University of Michigan, an…