使用 Zookeeper 作为注册中心

使用 Zookeeper 作为注册中心

This example shows dubbo-go’s service discovery feature with Zookeeper as registry.

使用方式

通过以下方式指定注册中心地址:

  1. ins, _ := dubbo.NewInstance(
  2. dubbo.WithName("dubbo_registry_nacos_server"),
  3. dubbo.WithRegistry(
  4. registry.WithZookeeper(),
  5. registry.WithAddress("127.0.0.1:2181"),
  6. ),
  7. dubbo.WithProtocol(
  8. protocol.WithTriple(),
  9. protocol.WithPort(20000),
  10. ),
  11. )
  12. srv, err := ins.NewServer()

How to run

Start Zookeeper server

This example relies on zookeeper as registry, follow the steps below to start a zookeeper server first.

  1. Start zookeeper with docker, run docker run --rm -p 2181:2181 zookeeper or make -f $DUBBO_GO_SAMPLES_ROOT_PATH/build/Makefile docker-up.
  2. Download and start zookeeper locally on your machine.

Run server

  1. $ go run ./go-server/cmd/server.go

test rpc server work as expected:

  1. $ curl \
  2. --header "Content-Type: application/json" \
  3. --data '{"name": "Dubbo"}' \
  4. http://localhost:20000/greet.GreetService/Greet

check url address successfully registered into zookeeper:

  1. # enter zookeeper bin directory, for example '$HOST_PATH/apache-zookeeper-3.5.9-bin/bin'
  2. $ ./zkCli.sh
  3. [zk: localhost:2181(CONNECTED) 0] ls /services/dubbo_registry_zookeeper_server
  4. [30.221.147.198:20000]

Run client

  1. $ go run ./go-client/cmd/client.go
  2. hello world

最后修改 September 13, 2024: Refactor website structure (#2860) (1a4b998f54b)