-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: call services from different registry
connect to 2 zks, and call to service in specific zk as needed
- Loading branch information
1 parent
4e0230c
commit da357a5
Showing
4 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters