4
0

update grpcx

This commit is contained in:
dc.To 2025-04-27 14:38:09 +08:00
parent 7827731423
commit 6648f2d530
2 changed files with 45 additions and 18 deletions

View File

@ -1,18 +0,0 @@
package config
import (
"os"
"github.com/gogf/gf/contrib/registry/etcd/v2"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
)
func Endpoint() string {
return os.Getenv("ETCD_ENDPOINTS")
}
func Etcd() {
if Endpoint() != "" {
grpcx.Resolver.Register(etcd.New(Endpoint()))
}
}

45
with/grpcx.go Normal file
View File

@ -0,0 +1,45 @@
package config
import (
"os"
"github.com/gogf/gf/contrib/registry/etcd/v2"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"google.golang.org/grpc/reflection"
)
var Endpoint = os.Getenv("ETCD_ENDPOINTS")
func Etcd() *etcd.Registry {
return etcd.New(Endpoint)
}
func Registry(f ...func()) {
if Endpoint != "" {
grpcx.Resolver.Register(Etcd())
}
for _, fn := range f {
fn()
}
}
/**
* grpc reflect
* @author dc.To
* @version 20250427
*/
func Reflect(s reflection.GRPCServer, f ...func()) {
if Endpoint == "" {
reflection.Register(s)
}
for _, fn := range f {
fn()
}
}
func Resolve[T any](value T, f func(T) T) T {
if f == nil {
return value
}
return f(value)
}