Check out full sample code here
<properties>
<dubbo.version>3.0.8</dubbo.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- This dependency helps to introduce Curator and Zookeeper dependencies that are necessary for Dubbo to work with zookeeper as transitive dependencies -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
dubbo-dependencies-zookeeper
will automatically add Zookeeper-related client dependencies to the application, reducing the cost of using Zookeeper for users. If there is a version compatibility problem during use, users can also add it by themselves instead of using dubbo-dependencies-zookeeper
Curator, Zookeeper Client and other dependencies.
Since Dubbo uses Curator as a programming client interacting with Zookeeper Server, special attention should be paid to the compatibility between Zookeeper Server and Dubbo version dependencies
Zookeeper Server Version | Dubbo Version | Dubbo Zookeeper Dependency Package | Description |
---|---|---|---|
3.4.x and below | 3.0.x and above | dubbo-dependencies-zookeeper | transitive dependencies Curator 4.x, Zookeeper 3.4.x |
3.5.x and above | 3.0.x and above | dubbo-dependencies-zookeeper-curator5 | transitive dependencies Curator 5.x, Zookeeper 3.7.x |
3.4.x and above | 2.7.x and below | dubbo-dependencies-zookeeper | transitive dependencies Curator 4.x, Zookeeper 3.4.x |
3.5.x and above | 2.7.x and below | None | Need to add Curator, Zookeeper and other related client dependencies |
# application.yml
dubbo
registry
address: zookeeper://localhost:2181
or
# dubbo.properties
dubbo.registry.address=zookeeper://localhost:2181
or
<dubbo:registry address="zookeeper://localhost:2181" />
address
is the only attribute that must be specified to enable the zookeeper registration center, and in a production environment, address
is usually specified as the cluster address, such as
address=zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181
It is also possible to configure protocol and address separately, such as
<dubbo:registry protocol="zookeeper" address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181" />
If Zookeeper enables authentication, Dubbo supports passing in the identity by specifying username and password.
# application.yml
dubbo
registry
address: zookeeper://localhost:2181
username: hello
password: 1234
You can also directly expand the parameters on the address address=zookeeper://hello:1234@localhost:2181
By specifying the group
attribute, the logical isolation of microservice addresses can be achieved within the same Zookeeper cluster. For example, multiple sets of development environments can be isolated in one cluster, and isolation can be achieved at the address discovery level.
# application.yml
dubbo
registry
address: zookeeper://localhost:2181
group: daily1
Configure connection and session expiration time
# application.yml
dubbo
registry
address: zookeeper://localhost:2181
timeout: 30 * 1000* # Connection timeout, default 30s
session: 60 * 1000* # Session timeout, default 60s
The Zookeeper registry also supports some other control parameters, for details, see Registry configuration item manual
process:
/dubbo/com.foo.BarService/providers
directory./dubbo/com.foo.BarService/providers
directory. And write your own URL address to /dubbo/com.foo.BarService/consumers
directory/dubbo/com.foo.BarService
directory.The following functions are supported:
<dubbo:registry check="false" />
is set, the failed registration and subscription requests will be recorded, and the background will retry periodically<dubbo:registry username="admin" password="1234" />
<dubbo:registry group="dubbo" />
, if not configured, the default root node will be used.*
wildcard <dubbo:reference group="*" version="*" />
, you can subscribe to all groups of services and providers of all versions