当前位置: 首页 > 文档资料 > Cloudadc 帮助文档 >

DUBBO

优质
小牛编辑
130浏览
2023-12-01

dubble example arch v2.png

As above figure, the dubble sample contains 4 components:

  • Registry - Zookeeper

  • User Service Provider - Dubbo Service Provider, which register the User Service to Zookeeper

  • User Service Consumer - Dubbo Service Provider, which will lookup Dubbo Service from Registry and consume the service

  • Order Service - REST based service, which will invoke the User Service Consumer

编译
$ mvn clean install
...
[INFO] Dubbo Parent ....................................... SUCCESS [  0.258 s]
[INFO] Dubbo User Service API ............................. SUCCESS [  1.055 s]
[INFO] Dubbo User Service Provider ........................ SUCCESS [  0.752 s]
[INFO] Dubbo User Service Consumer ........................ SUCCESS [  0.549 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Note编译成功后会出现如上 BUILD SUCCESS 提示。

注册中心

参照 链接 安装三节点集群 ZooKeeper。如下为但节点安装示意:

1. 安装 & 启动
tar -xvf apache-zookeeper-3.6.2-bin.tar.gz
cd apache-zookeeper-3.6.2-bin/conf/
cp zoo_sample.cfg zoo.cfg
cd ../bin/
./zkServer.sh start
2. 测试
$ ./zkCli.sh
[zk: localhost:2181(CONNECTED) 0] version
ZooKeeper CLI version: 3.6.2--803c7f1a12f85978cb049af5e4ef23bd8b688715, built on 09/04/2020 12:44 GMT

本地运行

服务提供者

1. 启动 3 个 Provider 节点以保证高可用(3 个 Provider 在同一台服务器上)
java -Ddubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -Ddubbo.protocol.host=192.168.100.1 -Ddubbo.protocol.port=20880  -jar provider/target/dubbo-user-service-provider-0.0.1.jar
java -Ddubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -Ddubbo.protocol.host=192.168.100.1 -Ddubbo.protocol.port=20881  -jar provider/target/dubbo-user-service-provider-0.0.1.jar
java -Ddubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -Ddubbo.protocol.host=192.168.100.1 -Ddubbo.protocol.port=20882  -jar provider/target/dubbo-user-service-provider-0.0.1.jar
2. 连接到其中一个Provider 测试
$ telnet 192.168.100.1 20881

dubbo>ls
PROVIDER:
io.cloudadc.dubbo.userservice.api.UserService:1.0.0

dubbo>invoke io.cloudadc.dubbo.userservice.api.UserService.getUserAddress(100)
Use default service io.cloudadc.dubbo.userservice.api.UserService.
result: [{"id":100,"phone":"18611908049","userAddress":"Beijing, Chaoyang, Building #17"},{"id":100,"phone":"18611908049","userAddress":"Beijing, Xicheng, Building #09"}]
elapsed: 1 ms.

服务消费者

1. 启动
java -Ddubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -jar consumer/target/dubbo-user-service-consumer-0.0.1.jar

2. 测试

Either test via http://localhost:8080/swagger-ui.html

or execute from cli

$ curl -X GET "http://localhost:8080/api/initOrder?uid=10101" -H "accept: application/json"
{
  "id": "orderID",
  "name": "order",
  "userAddress": [
    {
      "id": 10101,
      "userAddress": "Beijing, Chaoyang, Building #17",
      "phone": "18611908049"
    },
    {
      "id": 10101,
      "userAddress": "Beijing, Xicheng, Building #09",
      "phone": "18611908049"
    }
  ]
}

// To test load balancer
for i in {1..20} ; do curl -X GET "http://localhost:8080/api/initOrder?uid=10101" -H "accept: application/json" ; echo; done
3. 查看 Consumer 和 Provider 之间的连接(Consumer 和 所有 Provider 建立连接,并维护连接表)
$ netstat -anv | grep 3165
tcp46      0      0  *.8080                 *.*                    LISTEN      131072 131072   3165      0 0x0000 0x00000006
tcp4       0      0  192.168.100.1.53511    192.168.100.1.20882    ESTABLISHED 407215 146988   3165      0 0x0102 0x00000008
tcp4       0      0  192.168.100.1.53510    192.168.100.1.20880    ESTABLISHED 406349 146988   3165      0 0x0102 0x00000008
tcp4       0      0  192.168.100.1.53509    192.168.100.1.20881    ESTABLISHED 406781 146988   3165      0 0x0102 0x00000008
tcp4       0      0  10.1.10.1.53508        10.1.10.6.2181         ESTABLISHED 131072 131768   3165      0 0x0102 0x00000000

容器化运行服务提供者

服务提供者

1. 构建服务提供者容器镜像
cd provider/
docker build -t dubbo-user-service-provider .
2. 启动服务提供者
docker run -e DUBBO_IP_TO_REGISTRY=192.168.1.6 -e DUBBO_PORT_TO_REGISTRY=20881 -e dubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -p 192.168.1.6:20881:20880 dubbo-user-service-provider
3. 连接到其中一个Provider 测试
$ telnet 192.168.1.6 20881

dubbo>ls
PROVIDER:
io.cloudadc.dubbo.userservice.api.UserService:1.0.0

dubbo>invoke io.cloudadc.dubbo.userservice.api.UserService.getUserAddress(100)
Use default service io.cloudadc.dubbo.userservice.api.UserService.
result: [{"id":100,"phone":"18611908049","userAddress":"Beijing, Chaoyang, Building #17"},{"id":100,"phone":"18611908049","userAddress":"Beijing, Xicheng, Building #09"}]
elapsed: 0 ms.
4. Push image to a docker registry
docker tag dubbo-user-service-provider:latest cloudadc/dubbo-user-service-provider:0.0.1
docker push cloudadc/dubbo-user-service-provider:0.0.1

服务消费者

1. 启动
java -Ddubbo.registry.address=zookeeper://10.1.10.6:2181,10.1.10.7:2181,10.1.10.8:2181 -jar consumer/target/dubbo-user-service-consumer-0.0.1.jar

2. 测试

Either test via http://localhost:8080/swagger-ui.html

or execute from cli

$ curl -X GET "http://localhost:8080/api/initOrder?uid=10101" -H "accept: application/json"
{
  "id": "orderID",
  "name": "order",
  "userAddress": [
    {
      "id": 10101,
      "userAddress": "Beijing, Chaoyang, Building #17",
      "phone": "18611908049"
    },
    {
      "id": 10101,
      "userAddress": "Beijing, Xicheng, Building #09",
      "phone": "18611908049"
    }
  ]
}
3. 查看 Consumer 和 Provider 之间的连接(Consumer 和 docker 运行的宿主机建立连接)
$ netstat -anv | grep 3057
tcp46      0      0  *.8080                 *.*                    LISTEN      131072 131072   3057      0 0x0000 0x00000006
tcp4       0      0  192.168.1.6.53248      192.168.1.6.20881      ESTABLISHED 403745 146988   3057      0 0x0102 0x00000008
tcp4       0      0  10.1.10.1.53247        10.1.10.7.2181         ESTABLISHED 131072 131768   3057      0 0x0102 0x00000000

K8S 上运行服务提供者

服务提供者

1. 部署
kubectl apply -f dubbo-user-service-provider.yaml
2. 连接到 F5 上 Dubbo Provider VIP 测试
$ telnet 192.168.7.56 20880
Trying 192.168.7.56...
Connected to 192.168.7.56.
Escape character is '^]'.

dubbo>ls
PROVIDER:
io.cloudadc.dubbo.userservice.api.UserService:1.0.0

dubbo>invoke io.cloudadc.dubbo.userservice.api.UserService.getUserAddress(100)
Use default service io.cloudadc.dubbo.userservice.api.UserService.
result: [{"id":100,"phone":"18611908049","userAddress":"Beijing, Chaoyang, Building #17"},{"id":100,"phone":"18611908049","userAddress":"Beijing, Xicheng, Building #09"}]
elapsed: 2 ms.

服务消费者

1. 启动
java -Ddubbo.registry.address=zookeeper://192.168.7.60:2181 -jar consumer/target/dubbo-user-service-consumer-0.0.1.jar

2. 测试

Either test via http://localhost:8080/swagger-ui.html

or execute from cli

$ curl -X GET "http://localhost:8080/api/initOrder?uid=10101" -H "accept: application/json"
{
  "id": "orderID",
  "name": "order",
  "userAddress": [
    {
      "id": 10101,
      "userAddress": "Beijing, Chaoyang, Building #17",
      "phone": "18611908049"
    },
    {
      "id": 10101,
      "userAddress": "Beijing, Xicheng, Building #09",
      "phone": "18611908049"
    }
  ]
}

// test loadbalancer
for i in {1..20} ; do curl -X GET "http://localhost:8080/api/initOrder?uid=10101" -H "accept: application/json" ; echo; done
3. 查看 Consumer 和 Provider 之间的连接(Consumer 和 F5 VIP 建立连接)
$ netstat -anv | grep 2448
tcp46      0      0  *.8080                 *.*                    LISTEN      131072 131072   2448      0 0x0000 0x00000006
tcp4       0      0  10.10.0.25.52654       192.168.7.56.20880     ESTABLISHED 131072 131100   2448      0 0x0102 0x00000008
tcp4       0      0  10.10.0.25.52653       192.168.7.60.2181      ESTABLISHED 131072 131100   2448      0 0x0102 0x00000000