当前位置:网站首页>go iris框架实现多服务Demo:通过(监听8083端口的)服务1中的接口启动(监听8084端口的)服务2
go iris框架实现多服务Demo:通过(监听8083端口的)服务1中的接口启动(监听8084端口的)服务2
2022-04-23 06:15:00 【玩哈哈527】
go iris框架实现多服务Demo:通过(监听8083端口的)服务1中的接口启动(监听8084端口的)服务2
前言
常见情况下,在一个应用程序中会监听不同端口的多个服务,比如:服务1实现其相应接口功能,服务2实现其相应接口功能。 此demo示例,目的是:通过在服务1中的某个接口来启动服务2。一、DEMO示例
1.引入库
代码如下(示例):
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/kataras/iris/v12"
"golang.org/x/sync/errgroup"
)
var g errgroup.Group
var sig string
var app2 *iris.Application
func main() {
sig = "start"
g.Go(startApp1)
if err := g.Wait(); err != nil {
fmt.Println(fmt.Sprintf("startup service failed, err:%v\n", err))
}
}
func startApp1() error {
app := iris.New()
app.Handle("GET", "/hello1", hello1)
app.Handle("GET", "/restart", restart)
log.Println("http://localhost:8083/hello1")
log.Println("http://localhost:8084/hello2")
log.Println("http://localhost:8083/restart")
err := app.Listen(":8083")
return err
}
func startApp2() error {
var err error
app2 = iris.New()
app2.Handle("GET", "/hello2", hello2)
err = app2.Listen(":8084")
return err
}
func restart(c iris.Context) {
if sig == "start" {
g.Go(startApp2)
fmt.Println("已启动http://localhost:8084")
sig = "restart"
} else if sig == "restart" {
//关闭监听
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := app2.Shutdown(ctx); nil != err {
log.Fatalf("server shutdown failed, err: %v\n", err)
}
log.Println("server gracefully shutdown")
fmt.Println("已停止http://localhost:8084")
//启动监听
g.Go(startApp2)
//app2.Listen(":8084")
fmt.Println("已启动http://localhost:8084")
sig = "restart"
}
c.WriteString("已重启")
}
func hello1(c iris.Context) {
c.WriteString("hello app1")
}
func hello2(c iris.Context) {
c.WriteString("hello app2")
}
总结
完成的功能流程:1.启动程序,服务1 监听端口http://localhost:8083,包含接口:http://localhost:8083/hello1 和 http://localhost:8083/restart
2.通过http://localhost:8083/restart接口启动服务2
3.服务2启动后,其接口http://localhost:8084/hello2正常使用
版权声明
本文为[玩哈哈527]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_28058509/article/details/122713278
边栏推荐
- 传输层重要知识(面试,复试,期末)
- 北峰油气田自组网无线通信对讲系统解决方案
- imx6ull-qemu 裸机教程1:GPIO,IOMUX,I2C
- onnxruntime-gpu 1.7 出现的警告“Force fallback to CPU execution for node: Gather_191”等
- 【点云系列】 场景识别类导读
- GIS实战应用案例100篇(五十二)-ArcGIS中用栅格裁剪栅格,如何保持行列数量一致并且对齐?
- 公专融合对讲机是如何实现多模式通信下的协同工作?
- 各类日期转化的utils
- excel实战应用案例100讲(八)-Excel的报表连接功能
- 基于51单片机的体脂检测系统设计(51+oled+hx711+us100)
猜你喜欢

美摄助力百度“度咔剪辑”,让知识创作更容易

Detailed explanation of unwind stack backtracking

项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题

北峰油气田自组网无线通信对讲系统解决方案

城市应急管理|城市突发事故应急通信指挥调度系统

美摄科技云剪辑,助力哔哩哔哩使用体验再升级

北峰通信助力湛江市消防支队构建PDT无线通信系统

FATFS FAT32学习小记

美摄科技受邀LVSon2020大会 分享《AI合成虚拟人物的技术框架与挑战》

Chapter 5 fundamentals of machine learning
随机推荐
go语言切片操作
Résolution du système
Detailed explanation of unwind stack backtracking
PyTorch 19. Differences and relations of similar operations in pytorch
STM32多路测温无线传输报警系统设计(工业定时测温/机舱温度定时检测等)
Device Tree 详解
面试总结之特征工程
直观理解 torch.nn.Unfold
Face_ Recognition face detection
scons 搭建嵌入式arm编译
Hanlp分词器(通过spark)
Chapter 4 pytoch data processing toolbox
各类日期转化的utils
PyTorch 20. PyTorch技巧(持续更新)
项目文件“ ”已被重命名或已不在解决方案中、未能找到与解决方案关联的源代码管理提供程序——两个工程问题
WinForm scroll bar beautification
Pep517 error during pycuda installation
PyTorch 12. hook的用法
JDBC连接池
go语言:在函数间传递切片