diff --git a/dubbogodemo/cmd/client/clients/user_service.go b/dubbogodemo/cmd/client/clients/user_service.go new file mode 100644 index 0000000..0ce9aee --- /dev/null +++ b/dubbogodemo/cmd/client/clients/user_service.go @@ -0,0 +1,39 @@ +package clients + +import ( + "dubbo-demo/pkg/dto" + "dubbo-demo/pkg/service" + "dubbo.apache.org/dubbo-go/v3/config" + hessian "github.com/apache/dubbo-go-hessian2" +) + +var( + UsZk1 = &UserServiceEnv1{} + UsZk2 = &UserServiceEnv2{} +) + +func InitUserServiceClient(){ + dto.RegisterDtos(func(pojos ...dto.POJO){ + for _,p := range pojos{ + hessian.RegisterPOJO(p) + } + }) + config.SetConsumerService(UsZk1) + config.SetConsumerService(UsZk2) +} + +type UserServiceEnv1 struct { + service.UserService +} + +type UserServiceEnv2 struct { + service.UserService +} + +func(a *UserServiceEnv1)Reference()string{ + return "UserServiceEnv1" +} + +func(a *UserServiceEnv2)Reference()string{ + return "UserServiceEnv2" +} diff --git a/dubbogodemo/cmd/client/conf/dubbo.yml b/dubbogodemo/cmd/client/conf/dubbo.yml new file mode 100644 index 0000000..e73a431 --- /dev/null +++ b/dubbogodemo/cmd/client/conf/dubbo.yml @@ -0,0 +1,39 @@ +# dubbo server yaml configure file + +dubbo: + application: + name: dubbogo-demo-consumer + module: dubbogo + version: 1.0.0 + owner: demo + registries: + zk1: + protocol: zookeeper + timeout: 3s + address: 192.168.96.129:2181 + zk2: + protocol: zookeeper + timeout: 3s + address: 192.168.96.129:2182 + protocols: + dubbo: + name: dubbo + consumer: + request-timeout: 10s + check: false + references: + UserServiceEnv1: + retries: 0 + registry-ids: + - zk1 + protocol: dubbo + interface: com.demo.exp.service.UserService + UserServiceEnv2: + retries: 0 + registry-ids: + - zk2 + protocol: dubbo + interface: com.demo.exp.service.UserService + logger: + zap-config: + level: error \ No newline at end of file diff --git a/dubbogodemo/cmd/client/main.go b/dubbogodemo/cmd/client/main.go new file mode 100644 index 0000000..a0f56fc --- /dev/null +++ b/dubbogodemo/cmd/client/main.go @@ -0,0 +1,55 @@ +package main + +import ( + "dubbo-demo/cmd/client/clients" + "dubbo-demo/pkg/dto" + "dubbo-demo/util" + "dubbo.apache.org/dubbo-go/v3/config" + _ "dubbo.apache.org/dubbo-go/v3/imports" + "github.com/sirupsen/logrus" +) + +func main(){ + clients.InitUserServiceClient() + err := config.Load(config.WithPath("./conf/dubbo.yml")) + if err != nil{ + panic(err) + } + req := &dto.UserRequest{ + UserName: "你好", + UserId: "123", + } + ctx, traceId := util.GetDubboContextWithAppName("dubbo-consumer") + //time.Sleep(10 * time.Second) + rsp, err := clients.UsZk1.QueryUser(ctx, req) + if err != nil{ + logrus.WithFields(logrus.Fields{ + "traceId": traceId, + "rsp": rsp, + "err": err, + }).Error("query zk1 userservice response") + }else{ + logrus.WithFields(logrus.Fields{ + "traceId": traceId, + "rsp": rsp, + }).Info("query zk1 userservice response") + } + + req.UserId = "123456789" + ctx, traceId = util.GetDubboContextWithAppName("dubbo-consumer") + rsp, err = clients.UsZk2.QueryUser(ctx, req) + if err != nil{ + logrus.WithFields(logrus.Fields{ + "traceId": traceId, + "rsp": rsp, + "err": err, + }).Error("query zk2 userservice response") + }else{ + logrus.WithFields(logrus.Fields{ + "traceId": traceId, + "rsp": rsp, + }).Info("query zk2 userservice response") + } + select { + } +} diff --git a/dubbogodemo/cmd/server/conf/dubbo.yml b/dubbogodemo/cmd/server/conf/dubbo.yml index 3d9f45c..17a0371 100644 --- a/dubbogodemo/cmd/server/conf/dubbo.yml +++ b/dubbogodemo/cmd/server/conf/dubbo.yml @@ -10,11 +10,11 @@ dubbo: demoZK: protocol: zookeeper timeout: 3s - address: 192.168.96.129:2182 + address: 192.168.96.129:2181 protocols: dubbo: name: dubbo - port: 20000 + port: 20001 provider: register: true registryIDs: