本文介绍如何将符合 Kitex 架构的 Go 应用接入微服务引擎。
说明
完整代码示例参见文末。
添加代码库。
"github.com/kitex-contrib/obs-opentelemetry/tracing" // tracing sdk "github.com/kitex-contrib/registry-nacos/registry" // nacos registry sdk
说明
添加初始化。
registry, err := registry.NewDefaultNacosRegistry() if err != nil { panic(err) }
添加服务端配置。
// add registey middleware to register service info to nacos server server.WithRegistry(registry), // add tracing middleware suit to propagate baggage server.WithSuite(tracing.NewServerSuite()),
说明
完整代码示例参见文末。
添加代码库。
"github.com/cloudwego/kitex/pkg/xds" // xds sdk from kitex "github.com/kitex-contrib/obs-opentelemetry/tracing" // tracing sdk xdsmanager "github.com/kitex-contrib/xds" // xds suite "github.com/kitex-contrib/xds/xdssuite" // xds suite
添加客户端配置。
// add tracing middleware suit to propagate baggage kclient.WithSuite(tracing.NewClientSuite()), // add xDS suite to connect to xDS server and route requests by the // config received from xDS server. client.WithXDSSuite(xds.ClientSuite{ RouterMiddleware: xdssuite.NewXDSRouterMiddleware( xdssuite.WithRouterMetaExtractor(tracing.ExtractFromPropagator), ), Resolver: xdssuite.NewXDSResolver(), }),
说明
详细操作参加 接入 Go 应用。
在 Deployment 的 Template 中加入以下标签,实现服务配置信息自动注入。
spec: template: metadata: labels: sidecar.mesh.io/data-plane-mode: "kitex" sidecar.mesh.io/lane: "lane-****" sidecar.mesh.io/mse-namespace: "****-group"
类型 | 参数 | 说明 | 示例 |
---|---|---|---|
labels | sidecar.mesh.io/data-plane-mode | 微服务引擎数据面组件。 | sidecar.mesh.io/data-plane-mode: "kitex" |
sidecar.mesh.io/lane | 服务所处泳道名称。 | sidecar.mesh.io/lane: "mse-base" | |
sidecar.mesh.io/mse-namespace | 服务所处治理中心中服务网格命名空间。服务网格命名空间的名称由 Nacos 注册中心名称、Nacos 命名空间名称和 NacosGroup 名称拼接而成。创建同步任务时,自动生成治理中心中服务网格命名空间。 | sidecar.mesh.io/mse-namespace: "nacos-test-public-default-group" |
package main import ( "context" "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" "github.com/cloudwego/kitex/pkg/klog" "github.com/cloudwego/kitex/pkg/rpcinfo" "github.com/cloudwego/kitex/server" "github.com/kitex-contrib/obs-opentelemetry/tracing" // tracing sdk "github.com/kitex-contrib/registry-nacos/registry" // nacos registry sdk ) var otherClient echo.Client type echoImpl struct{} func (e *echoImpl) Echo(ctx context.Context, req *api.Request) (*api.Response, error) { // the client should use the ctx from the input request return otherClient.Echo(ctx, &api.Request{Message: req.Message}) } func main() { // initialize nacos register registry, err := registry.NewDefaultNacosRegistry() if err != nil { panic(err) } svr := echo.NewServer( new(echoImpl), server.WithServerBasicInfo(&rpcinfo.EndpointBasicInfo{ServiceName: "echo"}), // add registey middleware to register service info to nacos server. server.WithRegistry(registry), // add tracing middleware suit to propagate baggage server.WithSuite(tracing.NewServerSuite()), ) if err := svr.Run(); err != nil { klog.Infof("server stopped with error:", err) } else { klog.Infof("server stopped") } }
package main import ( "context" "fmt" "github.com/cloudwego/kitex/client" "github.com/cloudwego/kitex/pkg/xds" // xds sdk from kitex "github.com/kitex-contrib/obs-opentelemetry/tracing" // tracing sdk xdsmanager "github.com/kitex-contrib/xds" // xds suite "github.com/kitex-contrib/xds/xdssuite" // xds suite api "github.com/cloudwego/kitex-examples/kitex_gen/api" "github.com/cloudwego/kitex-examples/kitex_gen/api/echo" kclient "github.com/cloudwego/kitex/client" ) func main() { // initialize xds module err := xdsmanager.Init() if err != nil { return } destService := "echo" // initialize the client cli, err := echo.NewClient( destService, // add tracing middleware suit to propagate baggage kclient.WithSuite(tracing.NewClientSuite()), // add xDS suite to connect to xDS server and route requests by the // config received from xDS server. client.WithXDSSuite(xds.ClientSuite{ RouterMiddleware: xdssuite.NewXDSRouterMiddleware( xdssuite.WithRouterMetaExtractor(tracing.ExtractFromPropagator), ), Resolver: xdssuite.NewXDSResolver(), }), ) for { resp, err := cli.Echo(context.TODO(), &api.Request{}) if err != nil { panic(err) } fmt.Println("resp:", resp.Message) } }