springboot下的dubbo、zookeeper 结合使用

214 阅读2分钟

近期在研究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

<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"/>
dubbo-consumer.xml

1、消费者要访问提供者,

<dubbo:reference id="helloService" interface="com.zhyonk.wreserve.service.HelloService" protocol="dubbo" <dubbo:service interface="com.zhyonk.service.HelloService" ref="helloService"/> 其中的interface必须要求同一个路径之下,不然对应不上,会出现下面的情况

图片描述(最多50字)

正常情况下dubbo-admin中会显示

图片描述(最多50字)

2、如果嫌自己搭建麻烦的话可以直接用用网站自动生成 start.dubbo.io/

图片描述(最多50字)

喜欢的可以点个赞哦