同样的,在consumer即micro-service-dubbo-web的 resource 文件下,也创建一个dubbo-consumer.xml文件。内容稍有不同。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <!-- consumer's application name, used for tracing dependency relationship (not a matching criterion), don't set it same as provider --> <dubbo:application/> <!-- use multicast registry center to discover service --> <!--<dubbo:registry address="multicast://224.5.6.7:1234"/>--> <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181" /> <!-- generate proxy for the remote service, then demoService can be used in the same way as the local regular interface --> <dubbo:reference check="false" interface="com.whforever.service.DemoService"/> </beans>从上面可以看出这两个文件的注册发现协议是zookeeper,因此在服务启动之前需要启动zookeeper,具体移步Zookeeper 注册中心安装启动
准备测试测试之前还要做点点工作。
在启动provider事需要一部分引导程序,请看如下代码:
public class ProviderMain { public static void main(String[] args) throws IOException { System.setProperty("java.net.preferIPv4Stack", "true"); ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("dubbo-provider.xml"); context.start(); System.in.read(); // press any key to exit } }consumer代码
@Controller @RequestMapping("http://www.likecs.com/") public class IndexController { @Autowired DemoService demoService; @RequestMapping("/echo") @ResponseBody public String echo() { System.out.println(">>>>>>echo"); return JSON.toJSONString(demoService.getUsers()); } } 运行先运行provider:
[06/06/18 11:56:29:029 CST] main INFO config.AbstractConfig: [DUBBO] The service ready on spring started. service: com.whforever.service.DemoService, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.whforever.service.DemoService to local registry, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Export dubbo service com.whforever.service.DemoService to url dubbo://192.168.1.120:20880/com.whforever.service.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.120&bind.port=20880&dubbo=2.6.1&generic=false&interface=com.whforever.service.DemoService&methods=sayHello,getUsers&pid=13992&side=provider×tamp=1528300589682, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO config.AbstractConfig: [DUBBO] Register dubbo service com.whforever.service.DemoService url dubbo://192.168.1.120:20880/com.whforever.service.DemoService?anyhost=true&application=demo-provider&bind.ip=192.168.1.120&bind.port=20880&dubbo=2.6.1&generic=false&interface=com.whforever.service.DemoService&methods=sayHello,getUsers&pid=13992&side=provider×tamp=1528300589682 to registry registry://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.6.1&pid=13992®istry=zookeeper×tamp=1528300589673, dubbo version: 2.6.1, current host: 192.168.1.120 [06/06/18 11:56:30:030 CST] main INFO transport.AbstractServer: [DUBBO] Start NettyServer bind /0.0.0.0:20880, export /192.168.1.120:20880, dubbo version: 2.6.1, current host: 192.168.1.120再运行sonsumer:
通过查看dubbo监控中心,可以看到如下所示的情况,具体dubbo监控中心如何安装部署请移步Simple 监控中心安装
小结对于dubbo听其大名已久,直到最近才动手写了一些demo,总体来看上手还是比较简单,官方也提供了比较详细的文档,社区也比较活跃。关于本篇博客中的代码,楼主已经放到了github,该兴趣的小伙伴,请移步Dubbo初体验Demo模板代码
号外楼主造了一个轮子,LIGHTCONF 是一个基于Netty实现的一个配置管理平台,其核心设计目标是“为业务提供统一的配置管理服务”,可以做到开箱即用。
戳这里,欢迎光临小站 -_-
基于Netty实现的轻量级分布式应用配置中心