springboot下的dubbo、zookeeper 结合使用
近期在研究dubbo框架
相信看到这篇博客的,dubbo的基础应该都有了
zookeeper的搭建走了点弯路,配置起来各种麻烦,妈的搞的好烦。
正好一直想用一下docker,但对docker只是有个简单的概念。
用了一晚上去研究docker,之后发现真的好用
搭建个zookeeper就跟玩似的。
这里记录一下遇到的一些坑!
1、springboot引入dubbo的配置文件
网上搜索了一下,大概的两种方式
1、 这种方式是通过 ClassPathXmlApplicationContext 加载xml来获取上下文Context启动
1 @SpringBootApplication 2 3 public class WreserveApplication { 4 5 public static void main(String[] args) throws IOException, InterruptedException { 6 ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"dubbo-provider.xml"}); 7 context.start(); 8 System.in.read(); // press any key to exit 9 } 10 11 }
2、通过 @ImportResource({ "classpath:dubbo-provider.xml" }) 加载。
1 @SpringBootApplication 2 @ImportResource({ "classpath:dubbo-provider.xml" }) 3 public class WreserveApplication { 4 @Bean 5 public CountDownLatch closeLatch() { 6 return new CountDownLatch(1); 7 } 8 9 public static void main(String[] args) throws IOException, InterruptedException { 10 ConfigurableApplicationContext context = SpringApplication.run(WreserveApplication.class, args); 11 CountDownLatch closeLatch = context.getBean(CountDownLatch.class); 12 closeLatch.await(); 13 14 } 15 16 }
2、dubbo-provider.xml和dubbo-consumer.xml中需要注意的一些问题
dubbo-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-2.5.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <dubbo:application name="demo-provider"/> <!-- use multicast registry center to export service --> <dubbo:registry address="zookeeper://192.168.99.100:2181"/> <!-- use dubbo protocol to export service on port 20880 --> <dubbo:protocol name="dubbo" port="20880"/> <!-- service implementation, as same as regular local bean --> <bean id="helloService" class="com.zhyonk.service.Impl.HelloServiceImpl"/> <!-- declare the service interface to be exported --> <dubbo:service interface="com.zhyonk.service.HelloService" ref="helloService"/> </beans>
dubbo-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="wreserve-wechat"></dubbo:application> <!--zookeeper注册中心 --> <dubbo:registry address="zookeeper://192.168.99.100:2181"></dubbo:registry> <dubbo:consumer check="false" timeout="60000" /> <!-- 生成远程服务代理,可以和本地bean一样使用demoService --> <dubbo:reference id="helloService" interface="com.zhyonk.wreserve.service.HelloService" protocol="dubbo" timeout="15000" /> </beans>
1、消费者要访问提供者,
<dubbo:reference id="helloService" interface="com.zhyonk.wreserve.service.HelloService" protocol="dubbo"
<!-- service implementation, as same as regular local bean --> <bean id="helloService" class="com.zhyonk.service.Impl.HelloServiceImpl"/> <!-- declare the service interface to be exported --> <dubbo:service interface="com.zhyonk.service.HelloService" ref="helloService"/>
其中的interface必须要求同一个路径之下,不然对应不上,会出现下面的情况
正常情况下dubbo-admin中会显示
2、如果嫌自己搭建麻烦的话可以直接用用网站自动生成 http://start.dubbo.io/
就先到这,睡了