1.美图
上一节:https://blog.csdn.net/qq_21383435/article/details/81076625
1.zookeeper集群
2.新建maven项目DubboTest
<groupId>com.lcc.dubbo</groupId>
<artifactId>DubboTest</artifactId>
<version>1.0-SNAPSHOT</version>
然后引入包
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.6</version>
</dependency>
<!-- 添加zk客户端依赖 -->
<dependency>
<groupId>com.github.sgroschupf</groupId>
<artifactId>zkclient</artifactId>
<version>0.1</version>
</dependency>
<!-- 添加对dubbo-common的依赖,使用UserService -->
<dependency>
<groupId>per.lx</groupId>
<artifactId>dubbo-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
然后项目结构如下
2.1 生产者,提供者,provider
package com.lcc.dubbo.test.service;
/**
* Created by lcc on 2018/7/17.
*/
public interface DemoService {
String sayHello(String name);
}
package com.lcc.dubbo.test.service.impl;
import com.lcc.dubbo.test.service.DemoService;
/**
* Created by lcc on 2018/7/17.
*/
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
package com.lcc.dubbo.test;
/**
* Created by lcc on 2018/7/17.
*/
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("provider.xml");
context.start();
System.in.read(); // 按任意键退出
}
}
/Users/lcc/IdeaProjects/DubboTest/src/main/resources/provider.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://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="lcc_provider" />
<!-- 配置zookeeper的地址,集群地址用逗号隔开 -->
<dubbo:registry protocol="zookeeper" address="lcc:2181,lcc:2182,lcc:2183" />
<!-- 用dubbo协议在29014端口暴露服务 -->
<dubbo:protocol name="dubbo" port="29014" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.lcc.dubbo.test.service.DemoService" ref="demoService" />
<!-- 具体的实现bean -->
<bean id="demoService" class="com.lcc.dubbo.test.service.impl.DemoServiceImpl" />
</beans>
2.2 消费者
package com.lcc.dubbo.test;
/**
* Created by lcc on 2018/7/17.
*/
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lcc.dubbo.test.service.DemoService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("consumer.xml");
context.start();
DemoService demoService = (DemoService)context.getBean("demoService"); // 获取远程服务代理
String hello = demoService.sayHello("lcc"); // 执行远程方法
System.out.println( hello ); // 显示调用结果
}
}
/Users/lcc/IdeaProjects/DubboTest/src/main/resources/consumer.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://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="lcc_consumer" />
<!-- 配置zookeeper的地址,集群地址用逗号隔开 -->
<dubbo:registry protocol="zookeeper" address="lcc:2181,lcc:2182,lcc:2183" />
<!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
<dubbo:reference id="demoService" interface="com.lcc.dubbo.test.service.DemoService" />
</beans>
然后分别运行provider
消费者
可以看到我们调用了服务
2.3 zookeeper查看
运行provider前
[zk: localhost:2181(CONNECTED) 35] ls /
[zookeeper]
运行provider后
[zk: localhost:2181(CONNECTED) 38] ls /
[zookeeper, dubbo]
[zk: localhost:2181(CONNECTED) 39] ls /dubbo
[com.lcc.dubbo.test.service.DemoService]
[zk: localhost:2181(CONNECTED) 40] ls /dubbo/com.lcc.dubbo.test.service.DemoService
[configurators, providers]
[zk: localhost:2181(CONNECTED) 41] ls /dubbo/com.lcc.dubbo.test.service.DemoService/providers
[dubbo%3A%2F%2F192.168.1.47%3A29014%2Fcom.lcc.dubbo.test.service.DemoService%3Fanyhost%3Dtrue%26application%3Dlcc_provider%26dubbo%3D2.5.6%26generic%3Dfalse%26interface%3Dcom.lcc.dubbo.test.service.DemoService%26methods%3DsayHello%26pid%3D4131%26side%3Dprovider%26timestamp%3D1531809097088]
[zk: localhost:2181(CONNECTED) 42] ls /
再次运行Consumer后
zk: localhost:2181(CONNECTED) 42] ls /
[zookeeper, dubbo]
[zk: localhost:2181(CONNECTED) 43] ls /dubbo
[com.lcc.dubbo.test.service.DemoService]
[zk: localhost:2181(CONNECTED) 44] ls /dubbo/com.lcc.dubbo.test.service.DemoService/
Command failed: java.lang.IllegalArgumentException: Path must not end with / character
[zk: localhost:2181(CONNECTED) 45] ls /dubbo/com.lcc.dubbo.test.service.DemoService
[consumers, configurators, routers, providers]
[zk: localhost:2181(CONNECTED) 46] ls /dubbo/com.lcc.dubbo.test.service.DemoService/consumers
[]
3.注意:
我们如果与运行一次后,修改包名后,再次运行会报错,因为已经在zookeeper中注册过了,可以修改配置文件xml或者删除掉zookeeper中的注册信息
Caused by: com.alibaba.dubbo.remoting.RemotingException: Failed to bind NettyServer on /192.168.1.47:29014, cause: Failed to bind to: /0.0.0.0:29014
at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:67)
at com.alibaba.dubbo.remoting.transport.netty.NettyServer.<init>(NettyServer.java:63)
at com.alibaba.dubbo.remoting.transport.netty.NettyTransporter.bind(NettyTransporter.java:33)
at com.alibaba.dubbo.remoting.Transporter$Adaptive.bind(Transporter$Adaptive.java)
at com.alibaba.dubbo.remoting.Transporters.bind(Transporters.java:57)
at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchanger.bind(HeaderExchanger.java:41)
at com.alibaba.dubbo.remoting.exchange.Exchangers.bind(Exchangers.java:71)
at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol.createServer(DubboProtocol.java:277)
... 24 more
Caused by: org.jboss.netty.channel.ChannelException: Failed to bind to: /0.0.0.0:29014
at org.jboss.netty.bootstrap.ServerBootstrap.bind(ServerBootstrap.java:303)
at com.alibaba.dubbo.remoting.transport.netty.NettyServer.doOpen(NettyServer.java:94)
at com.alibaba.dubbo.remoting.transport.AbstractServer.<init>(AbstractServer.java:62)
... 31 more
Caused by: java.net.BindException: Address already in use
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
删除指令:
delete path [version]
删除指定节点数据,其version参数的作用于set指定一致
delete /node_1/node_1_10000000001
整个节点全删除
注意:delete只能删除不包含子节点的节点,如果要删除的节点包含子节点,使用rmr命令
rmr /node_1