余瑜的博客 余瑜的博客
首页
  • 并发
  • 线程池
  • spring
  • maven
  • 其他
  • redis
  • mysql
  • linux
  • zookeeper
  • docker
  • terminal
  • kong插件开发
  • 资料
  • leetCode-简单
  • blog
  • 其他
关于
GitHub (opens new window)
首页
  • 并发
  • 线程池
  • spring
  • maven
  • 其他
  • redis
  • mysql
  • linux
  • zookeeper
  • docker
  • terminal
  • kong插件开发
  • 资料
  • leetCode-简单
  • blog
  • 其他
关于
GitHub (opens new window)
  • linux

  • zookeeper

  • docker

  • terminal

  • kong插件开发

    • 开发环境搭建
    • go注册consul
      • 前言
      • 注册consul
      • main方法
      • 查看consul注册状态
    • konga配置
    • 插件开发
    • 调用c文件
    • 踩坑
  • 运维
  • kong插件开发
余瑜
2021-05-13
目录

go注册consul

# 前言

插件开发需要同时启动多个端口号不同的项目注册到consul , 并提供http接口服务

但是, java项目实在本地启动实在太慢了, 而且对同时启动多个相同的项目并不友好, 所以我使用go写了一份

# 注册consul

package consulcon

import (
	"fmt"
	consulApi "github.com/hashicorp/consul/api"
	"myIris/logger"
	"strconv"
)

const (
	consulAddress = "localhost:8500"
	localIp       = "docker.for.mac.host.internal"
)

// 注册服务到consul
func Register(port int) {
	portStr := strconv.Itoa(port)

	// 连接consul服务
	config := consulApi.DefaultConfig()
	// 宿主机访问容器中的consul
	config.Address = consulAddress
	client, err := consulApi.NewClient(config)
	if err != nil {
		logger.Log.Error("consul client error : ", err)
	}

	// 注册服务到consul
	registration := new(consulApi.AgentServiceRegistration)
	registration.ID = portStr
	registration.Name = "go-consul-test-" + portStr
	registration.Port = port
	registration.Tags = []string{"primary"}
	registration.Address = "localhost"
	// 容器中的consul访问宿主机 localhost 使用 docker.for.mac.host.internal 进行替换
	registration.Check = healthCheck(port)

	err = client.Agent().ServiceRegister(registration)
	if err != nil {
		logger.Log.Error("register: \n", err)
	}
	logger.Log.Info(" register successful")
}

// consul健康检查回调函数
func healthCheck(port int) *consulApi.AgentServiceCheck {
	check := new(consulApi.AgentServiceCheck)
	// 容器中的consul访问宿主机 localhost 使用 docker.for.mac.host.internal 进行替换
	check.HTTP = fmt.Sprintf("http://%s:%d/health", localIp, port)
	logger.Log.Info(check.HTTP)
	check.Timeout = "5s"
	check.Interval = "5s"
	check.DeregisterCriticalServiceAfter = "30s" // 故障检查失败30s后 consul自动将注册服务删除
	return check
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

# main方法

package main

import (
	"github.com/kataras/iris/v12"
	"myIris/consulcon"
	"myIris/logger"
	"strconv"
)

func main() {
	logger.Init()
	go Init(8201)
	Init(8202)
}

func Init(port int) {
	// 注册服务到consul
	consulcon.Register(port)
	app := newApp()
	app.Listen(":" + strconv.Itoa(port))

}

func newApp() *iris.Application {
	app := iris.New()
	route(app)
	return app
}

func route(app *iris.Application) {
	// consul健康检查回调函数
	app.Handle("GET", "/health", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"status": "ok"})
	})

	app.Handle("GET", "/query", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"code": 200, "msg": "ok", "data": true})
	})

	app.Handle("POST", "/queryPOST", func(ctx iris.Context) {
		ctx.JSON(iris.Map{"code": 200, "msg": "ok", "data": true})
	})
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

# 查看consul注册状态

访问 http://127.0.0.1:8500/ui/dc1/services 如下图

image-20210513172616748

上次更新: 2021/06/03, 17:25:03

← 开发环境搭建 konga配置→

Theme by Vdoing | Copyright © 2018-2022 逆光世间 | 备案号: 京ICP备19016086号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式