Skip to content

Commit

Permalink
feat: call services from different registry
Browse files Browse the repository at this point in the history
connect to 2 zks, and call to service in specific zk as needed
  • Loading branch information
huangjun0124 committed Apr 3, 2022
1 parent 4e0230c commit da357a5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
39 changes: 39 additions & 0 deletions dubbogodemo/cmd/client/clients/user_service.go
Original file line number Diff line number Diff line change
@@ -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"
}
39 changes: 39 additions & 0 deletions dubbogodemo/cmd/client/conf/dubbo.yml
Original file line number Diff line number Diff line change
@@ -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
55 changes: 55 additions & 0 deletions dubbogodemo/cmd/client/main.go
Original file line number Diff line number Diff line change
@@ -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 {
}
}
4 changes: 2 additions & 2 deletions dubbogodemo/cmd/server/conf/dubbo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit da357a5

Please sign in to comment.