当前位置:网站首页>Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
2022-04-23 12:56:00 【Play ha ha 527】
go iris Multi service implementation framework Demo: adopt ( monitor 8083 Port of ) service 1 Interface startup in ( monitor 8084 Port of ) service 2
Preface
Usually , Multiple services on different ports will be monitored in an application , such as : service 1 Realize its corresponding interface functions , service 2 Realize its corresponding interface functions . this demo Example , The purpose is : By serving 1 To start the service 2.One 、DEMO Example
1. Import and stock in
The code is as follows ( Example ):
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(" Started http://localhost:8084")
sig = "restart"
} else if sig == "restart" {
// lsnrctl stop
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(" Has stopped http://localhost:8084")
// Start listening
g.Go(startApp2)
//app2.Listen(":8084")
fmt.Println(" Started http://localhost:8084")
sig = "restart"
}
c.WriteString(" Restarted ")
}
func hello1(c iris.Context) {
c.WriteString("hello app1")
}
func hello2(c iris.Context) {
c.WriteString("hello app2")
}
summary
Completed function flow :1. Start the program , service 1 Listening port http://localhost:8083, Contains interfaces :http://localhost:8083/hello1 and http://localhost:8083/restart
2. adopt http://localhost:8083/restart Interface start service 2
3. service 2 After starting , Its interface http://localhost:8084/hello2 Normal use
版权声明
本文为[Play ha ha 527]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230615231119.html
边栏推荐
- 网站首页文件被攻击篡改的形式有哪些
- 有趣的IDEA插件推荐,给你的开发工作增添色彩
- Calculate the past date and days online, and calculate the number of live days
- BUUCTF WEB [BUUCTF 2018]Online Tool
- 数据库中的日期时间类型
- Bert base Chinese Download (SMART)
- Record the problems encountered in using v-print
- Recommended website for drawing result map
- Luogu p5540 [balkanoi2011] timeismoney | minimum product spanning tree problem solution
- There is no need to crack the markdown editing tool typora
猜你喜欢
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
风尚云网学习-input属性总结
31. Next arrangement
The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
Record some NPM related problems (messy records)
Kubernets Getting started tutoriel
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
精度、速度完美平衡,最新图像分割SOTA模型重磅发布!!!
Huawei cloud MVP email
随机推荐
0基础可以考CPDA数据分析师证书吗
Fashion cloud learning - input attribute summary
21 天学习MongoDB笔记
22. 括号生成
Buuctf Web [bjdctf2020] zjctf, but so
Jiachen chapter Genesis "inner universe" joint Edition
Huawei cloud MVP email
Baserecyclerviewadapterhelper realizes pull-down refresh and pull-up loading
梳理网络IP代理的几大用途
31. 下一个排列
大家帮我看一下这是啥情况,MySQL5.5的。谢了
标签与路径
98. Error s.e.errormvcautoconfiguration $staticview reported by freemaker framework: cannot render error page for request
Calculate the past date and days online, and calculate the number of live days
unity常见的问题(一)
Remote access to raspberry pie at home (Part 1)
If you were a golang interviewer, what questions would you ask?
PHP generates JSON to process Chinese
Packet capturing and sorting -- TCP protocol [8]
云原生KubeSphere部署Redis