Hertz 学习笔记(15)

184 阅读1分钟

今天学多端口的例子

使用 Hertz 启动多端口服务的示例

/*
 * Copyright 2022 CloudWeGo Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package main

import (
	"context"
	"sync"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

var wg sync.WaitGroup

func hertz1() {
	h := server.Default(server.WithHostPorts(":8080"))
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping": "pong1"})
	})
	h.Spin()
}

func hertz2() {
	h := server.Default(server.WithHostPorts(":8081"))
	h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
		ctx.JSON(consts.StatusOK, utils.H{"ping": "pong2"})
	})
	h.Spin()
}

func main() {
	wg.Add(2)
	go func() {
		defer wg.Done()
		hertz1()
	}()
	go func() {
		defer wg.Done()
		hertz2()
	}()
	wg.Wait()
}

这个代码很简单,开了两个 goroutine 就没啥新东西了。

要运行也是一行:

go run multiple_service/main.go

然后访问两个不同的端口:

curl --location --request GET '127.0.0.1:8080/ping'
curl --location --request GET '127.0.0.1:8081/ping'

剩下的几个服务端的例子对目前的我来说还有点难顶,让我憋个大招,例子的学习就先到此为止。