当前位置:网站首页>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的src子文件下无法创建servlet
- The continuous construction of the Internet industry platform is not only able to collect traffic
- Servlet监听器&过滤器介绍
- [csnote] ER diagram
- 标签与路径
- Baserecyclerviewadapterhelper realizes pull-down refresh and pull-up loading
- leetcode-791. Custom string sorting
- box-sizing
- Plato Farm-以柏拉图为目标的农场元宇宙游戏
- 实现一个盒子在父盒子中水平垂直居中的几种“姿势”
猜你喜欢
Deploying MySQL in cloud native kubesphere
解决disagrees about version of symbol device_create
Customize the shortcut options in El date picker, and dynamically set the disabled date
NPDP | how can product managers not be excluded by programmers?
22. 括号生成
产品开发都应该知道的8个网站,增强工作体验
大家帮我看一下这是啥情况,MySQL5.5的。谢了
[vulnhub range] - DC2
box-sizing
Object. The disorder of key value array after keys
随机推荐
Learning materials
C, calculation code of parameter points of two-dimensional Bezier curve
Redis deployment of cloud native kubesphere
Recommended website for drawing result map
Bert base Chinese Download (SMART)
mysql中 innoDB执行过程分析
解决disagrees about version of symbol device_create
Introduction to servlet listener & filter
MySQL supports IP access
标签与路径
How to prevent the website from being hacked and tampered with
洛谷P5540 [BalkanOI2011] timeismoney | 最小乘积生成树 题解
进程虚拟地址空间区域划分
What are the forms of attack and tampering on the home page of the website
Idea的src子文件下无法创建servlet
[Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
Unable to create servlet under SRC subfile of idea
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
网站首页文件被攻击篡改的形式有哪些
[csnote] ER diagram