From 067c16b7ec8862bc8cd9fc7b42af7002a45f810a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=96=8C?= <259278618@qq.com> Date: Fri, 28 Nov 2025 08:52:22 +0800 Subject: [PATCH] =?UTF-8?q?swagger=E8=81=9A=E5=90=88=E5=B0=81=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- consul/consul.go | 14 ++++++ http/http.go | 4 +- swagger/swagger.go | 105 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 swagger/swagger.go diff --git a/consul/consul.go b/consul/consul.go index bc72dd2..699f582 100644 --- a/consul/consul.go +++ b/consul/consul.go @@ -2,10 +2,12 @@ package consul import ( "context" + "errors" "github.com/gogf/gf/contrib/registry/consul/v2" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/net/gsel" "github.com/gogf/gf/v2/net/gsvc" + "github.com/gogf/gf/v2/util/grand" ) func init() { @@ -21,3 +23,15 @@ func init() { gsvc.SetRegistry(registry) gsel.SetBuilder(gsel.NewBuilderRoundRobin()) } +func GetInstanceAddr(ctx context.Context, name string) (addr string, err error) { + watch, err := gsvc.GetRegistry().Watch(ctx, name) + service, err := watch.Proceed() + if err != nil || service == nil { + err = errors.New("获取customerService服务实例失败!") + return + } + maxService := grand.N(0, len(service)-1) + maxEndpoints := grand.N(0, len(service[maxService].GetEndpoints())-1) + addr = service[maxService].GetEndpoints()[maxEndpoints].String() + return +} diff --git a/http/http.go b/http/http.go index afcad70..6a18287 100644 --- a/http/http.go +++ b/http/http.go @@ -55,7 +55,7 @@ func init() {