前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >gRPC: 如何在启动多个端口?

gRPC: 如何在启动多个端口?

原创
作者头像
尹东勋
修改2021-12-13 00:18:37
1.4K0
修改2021-12-13 00:18:37
举报

介绍

本文介绍如何通过 rk-boot 在一个进程里启动多个 gRPC 端口。

为什么要启动多个端口?

大部分情况下,我们是不需要的。如果我们希望在一个进程里通过 flag 启动不同端口时,会用到。

我们会在下面的例子里给出用法。

请访问如下地址获取完整教程:

安装

代码语言:txt
复制
go get github.com/rookie-ninja/rk-boot
go get github.com/rookie-ninja/rk-grpc

快速开始

1.创建 boot.yaml

为了验证,我们启动了 commonServicecommonService 里包含了一系列常用 API,例如 /rk/v1/healthy。

代码语言:txt
复制
---
grpc:
  - name: alice
    port: 1949
    enabled: true
    commonService:
      enabled: true
  - name: bob
    port: 2008
    enabled: true
    commonService:
      enabled: true

2.创建 main.go

通过 boot.GetGrpcEntry("xxx") 来获取 gRPCEntry 进行 API 注册等操作。

代码语言:txt
复制
package main

import (
	"context"
	"github.com/rookie-ninja/rk-boot"
	"github.com/rookie-ninja/rk-grpc/boot"
)

func main() {
	// Create a new boot instance.
	boot := rkboot.NewBoot()

	// Get alice Entry via: 
	boot.GetEntry("alice").(*rkgrpc.GrpcEntry)
	// 
	// Get bob Entry via: 
	boot.GetEntry("bob").(*rkgrpc.GrpcEntry)

	// Bootstrap
	boot.Bootstrap(context.Background())
	
	// Wait for shutdown sig
	boot.WaitForShutdownSig(context.Background())
}

3.文件夹结构

代码语言:txt
复制
$ tree
.
├── boot.yaml
├── go.mod
├── go.sum
└── main.go

0 directories, 4 files

4.启动 main.go

代码语言:txt
复制
$ go run main.go

5.验证

  • 发送请求到 1949$ curl -X GET localhost:1949/rk/v1/healthy {"healthy":true}
  • 发送请求到 2008$ curl -X GET localhost:2008/rk/v1/healthy {"healthy":true}

只启动 1949 端口

我们可以通过 --rkset 命令行参数来 disable 掉 2008 端口的 gRPC Entry。

boot.yaml 里 gRPC 是一个数组,所以使用 grpcx 来表示。

1.启动 main.go with --rkset

代码语言:txt
复制
$ go build main.go
$ ./main --rkset "grpc[1].enabled=false"

2.验证

  • 发送请求到 1949$ curl -X GET localhost:1949/rk/v1/healthy {"healthy":true}
  • 发送请求到 2008$ curl -X GET localhost:2008/rk/v1/healthy curl: (7) Failed to connect to localhost port 2008: Connection refused

rkset 覆盖 boot.yaml 里的值

boot.yaml 里的值,使用 --rkset 作为 flag。

使用逗号(注意,是英文逗号)来覆盖多个参数。

类型

例子

Map

app.description="This is description"

List

grpc0.name="alice",grpc0.port=8081

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 介绍
  • 安装
  • 快速开始
    • 1.创建 boot.yaml
      • 2.创建 main.go
        • 3.文件夹结构
          • 4.启动 main.go
            • 5.验证
            • 只启动 1949 端口
              • 1.启动 main.go with --rkset
                • 2.验证
                • rkset 覆盖 boot.yaml 里的值
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档