1.创建maven项目并导入相关依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.7</version> </dependency> <dependency> <groupId>com.github.sgroschupf</groupId> <artifactId>zkclient</artifactId> <version>0.1</version> </dependency>
2.创建一个服务项目
1.创建类和接口
接口:
public interface GreetingsService {
String sayHi(String name);
}
实现类:
public class GreetingsServiceImpl implements GreetingsService { public String sayHi(String name) { return "hi,"+name; } }
2.在xml文件中配置注册服务信息
<?xml version="1.0" encoding="UTF-8"?>
<!-- xsi:xml schema instance xml模式实例 -->
<!-- xmlns *.xml文件中用到的命名空间 -->
<!-- xmlns:xsi *.xml遵守xml规范 -->
<!-- xmlns:dubbo 引入dubbo前缀 -->
<!-- xsi:schemaLocation 具体用到的schema资源,必须加上dubbo的资源路径 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置当前应用的信息 -->
<dubbo:application name="demo-provider" version="1.0"/>
<!-- 服务将会注册到zookeeper上 ,端口必须与zookeeper配置的端口一致-->
<dubbo:registry address="zookeeper://127.0.0.1:2183"/>
<!-- 指明程序名和发布的端口 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 把实现类放入到spring的容器中 -->
<bean id="greetingsService" class="com.ns.interfact.impl.GreetingsServiceImpl"/>
<!-- 注册到服务的注册表上 -->
<dubbo:service interface="com.ns.interfact.GreetingsService" ref="greetingsService"/>
</beans>
3.启动服务
public static void main(String[] args){
//读取xml配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationProvider.xml");
context.start();
System.out.println("服务启动");
try{
//使服务一直处于运行状态
System.in.read();
}catch(Exception e){
e.printStackTrace();
}
}
3.创建一个消费端
1.创建要调用的接口
public interface DemoService {
String sayHello(String name);
}
2.配置服务端的配置文件
<?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://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!-- 配置当前应用的信息 -->
<dubbo:application name="demo-customer" version="1.0"/>
<!-- 请求的服务注册的zookeeper的地址 -->
<dubbo:registry address="zookeeper://127.0.0.1:2183"/>
<!-- 注册到服务的注册表上 -->
<dubbo:reference interface="com.ns.interfact.GreetingsService" id="greetingsService" check="false"/>
</beans>
3.调用服务
public static void main(String[] args){
//读取xml配置文件
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationProvider.xml");
context.start();
GreetingsService greetingsService=context.getBean(GreetingsService.class);
System.out.println(greetingsService.sayHi("王霞"));
}
4.安装配置并启动zookeeper
1.从网站上下载zookeeper的压缩包https://jingyan.baidu.com/article/335530dafd4b3619cb41c3d8.html
2.解压下载的zookeeper的压缩包
3.修改配置文件
4.通过zkServer.cmd启动Zookeeper的服务器
5.通过zkCli.cmd启动Zookeeper的客户端,查看已经注册的服务
5.出现的错误
1.dubbo的标签找不到
错误原因: 找不到dubbo.xsd文件,可能是因为网络原因,判断方法:点击xml文件的资源链接,看是否能够打开.
解决办法: 将本地的xsd文件导入
2.zookeeper服务启动失败
错误原因:zookeeper的配置文件从出错
判断方式:用cmd运行zkServer.cmd的文件查看出错原因
![](https://i-blog.csdnimg.cn/blog_migrate/ac6b20bfe3161e487960e8c9bef02e05.png)