consul

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 2 Imported by: 0

README

go-consul

go-consulfirefly 体系中基于 Consul 的基础接入库,当前主要提供两类能力:

  • Consul 客户端初始化(根包)
  • 服务注册与网关发现(registry 子包)

包结构

  • conf.go:Consul 客户端配置模型
  • core.go:根据配置创建 *api.Client
  • registry/:注册中心实现(注册、注销、发现、监听)

客户端初始化

package main

import (
	consulx "github.com/fireflycore/go-consul"
)

func main() {
	client, err := consulx.New(&consulx.Conf{
		Address:    "127.0.0.1:8500",
		Scheme:     "http",
		Datacenter: "dc1",
	})
	if err != nil {
		panic(err)
	}

	_ = client
}

注册中心能力

registry 子包主要面向以下两类角色:

  • 微服务进程:使用 RegisterInstanceInstall/Uninstall
  • 网关进程:使用 DiscoverInstance 做方法路由发现

详细使用方式与模型说明见:

  • registry/README.md

设计约束

  • 业务服务不做服务发现,只做服务注册
  • 服务发现能力由网关统一使用
  • Consul 特有能力(健康检查、阻塞查询、事件模型)放在 go-consul/registry 内部维护

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(c *Conf) (*api.Client, error)

New 根据入参创建 Consul API 客户端。

Types

type Conf

type Conf struct {
	// Address 是 Consul HTTP API 地址,例如 127.0.0.1:8500。
	Address string `json:"address"`
	// Scheme 是访问协议,常见值为 http 或 https。
	Scheme string `json:"scheme"`
	// Datacenter 指定目标数据中心。
	Datacenter string `json:"datacenter"`
	// Token 是 ACL 访问令牌。
	Token string `json:"token"`

	// TLS 配置 HTTPS 连接参数。
	TLS *TLS `json:"tls"`
}

Conf 定义 Consul 客户端初始化配置。

type TLS

type TLS struct {
	// Enable 表示是否启用 TLS 配置。
	Enable bool `json:"enable"`
	// CaFile 指向 CA 证书文件路径。
	CaFile string `json:"ca_file"`
	// CertFile 指向客户端证书文件路径。
	CertFile string `json:"cert_file"`
	// KeyFile 指向客户端私钥文件路径。
	KeyFile string `json:"key_file"`
	// InsecureSkipVerify 控制是否跳过服务端证书校验。
	InsecureSkipVerify bool `json:"insecure_skip_verify"`
}

TLS 定义 Consul TLS 连接选项。

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL