使用zookeeper 作为注册中心 ,此处一以spring xml配置的方式实现

provider

pom.xml 引入的依赖

<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.julong</groupId>
<artifactId>alibaba-dubbo-learn-examples</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.julong</groupId>
<artifactId>alibaba-dubbo-learn-zookeeper-xml-server-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>alibaba-dubbo-learn-zookeeper-xml-server-example</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>


<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.9</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.32.Final</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>

</dependencies>
</project>

dubbo配置文件配置参数信息 dubbo-server.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<!-- 提供方应用信息,用于计算依赖关系 -->
<dubbo:application name="alibaba-dubbo-learn-zookeeper-xml-server-example" owner="julong" />

<!-- 使用multicast广播注册中心暴露服务地址 N/A 代表不需要注册中心 multicast://224.5.6.7:1234 zookeeper://192.168.10.222:12181-->
<dubbo:registry protocol="zookeeper" address="192.168.10.222:2181" client="zkclient" />
<!-- 集群配置:
address="10.20.153.10:2181,10.20.153.11:2181,10.20.153.12:2181"
address="zookeeper://10.20.153.10:2181?backup=10.20.153.11:2181,10.20.153.12:2181"
-->

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.julong.dubbo.service.HelloService" ref="helloService" />

<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.julong.dubbo.service.impl.HelloServiceImpl" />
</beans>
接口类
package com.julong.dubbo.service;

public interface HelloService {

public abstract String sayHello(String message);
}










实现类
package com.julong.dubbo.service.impl;

import com.julong.dubbo.service.HelloService;

public class HelloServiceImpl implements HelloService {

@Override
public String sayHello(String message) {
// TODO Auto-generated method stub
return "dubbo :" +message;
}

}

服务启动类:

package com.julong.dubbo.main;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* 服务提供方
* @author julong
* @date 2021年10月22日 下午9:57:34
* @desc
*/
public class ProviderMain {

public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-server.xml"});
context.start();
try {
System.out.println("服务启动");
System.in.read();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 按任意键退出
}

}

consumer

服务调用方和服务方差不多 主要配置如下 dubbo-client.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo
http://dubbo.apache.org/schema/dubbo/dubbo.xsd">



<!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
<dubbo:application name="alibaba-dubbo-learn-zookeper-xml-client-example" owner="julong" />

<!-- 使用multicast广播注册中心暴露发现服务地址 multicast://224.5.6.7:1234 zookeeper://192.168.10.222:12181 -->
<dubbo:registry address="zookeeper://192.168.10.222:2181" client="zkclient"/>

<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="20880" />

<!--
生成远程服务代理,可以和本地bean一样使用demoService 无注册中心的方法 使用url 配置
生成远程服务代理,dubbo:reference 可以和本地bean一样使用helloService
check="false" 忽略检查 服务提供者不存在 则不报错
check="true" 检查 服务提供者 如果不存在则报错
timeout 服务方法调用超时时间(毫秒)
retries 远程服务调用重试次数,不包括第一次调用,不需要重试请设为0 新增不要设置 重试次数
version 服务版本,与服务提供者的版本一致 *代表 随机
-->
<dubbo:reference id="helloService" interface="com.julong.dubbo.service.HelloService" />
</beans>

服务调用方只需要有接口定义信息即可

package com.julong.dubbo.service;

public interface HelloService {

public abstract String sayHello(String message);
}

启动方法:

package com.julong.dubbo.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.julong.dubbo.service.HelloService;

/**
* 服务调用方
* @author julong
* @date 2021年10月22日 下午9:59:52
* @desc
*/
public class CusumerMain {

public static void main(String[] args) {
// TODO Auto-generated method stub
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"dubbo-client.xml"});
context.start();
HelloService helloService = (HelloService)context.getBean("helloService"); // 获取远程服务代理
String hello = helloService.sayHello("hello world"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}

}

至此 dubbo 使用 zookeeper作为注册中心 基本的使用完成

登录zookeeper 客户端 查看 服务端启动后是否注册成功

[zk: 192.168.10.222:2181(CONNECTED) 5] ls /dubbo 
[com.julong.dubbo.service.HelloService]
[zk: 192.168.10.222:2181(CONNECTED) 6]