使用maven集成Dubbo
来源:互联网 发布:办公软件视频教程全集 编辑:程序博客网 时间:2024/06/15 15:06
最近项目要使用Dubbo来实现微服务,简单来说就是将你写好的服务用在其它模块中,当然是同一个业务流程中的多个模块。
使用的开发工具是IntelliJ IDEA。需要的库是zookeeper。
准备工作是这个样子的。New Project -> Maven -> Create from archetype (maven-archetype-webapp) -> 项目名为DubboDemo。
完成后,新建一个module。在DubboDemo项目上右键, New -> Module -> Maven -> Create from archetype (maven-archetype-webapp) -> 模块名为DubboService。
同样的步骤,再新建一个名为DubboConsumer的模块。
在DubboService和DubboConsumer的pom.xml文件中加入依赖包。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.gemu</groupId> <artifactId>dubbo-demo</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>DubboService</module> <module>DubboConsumer</module> </modules> <name>dubbo-demo Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.5.RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.4.9</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.3.3</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.7</version> </dependency> </dependencies> <build> <finalName>dubbo-demo</finalName> </build></project>其中有spring、zookeeper和dubbo的依赖包。
在DubboService中编写提供服务的代码。结构和内容如下:
ps: 请允许我用我蹩脚的英语装个B,截图软件我这个中文乱码。实属无奈。。。
appCtx.xml是spring的配置文件(内容是抄过来的,文章最后有地址)。内容如下:
<?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"> <!-- 具体的实现bean --> <bean id="demoService" class="com.gemu.DemoServiceImpl"></bean> <!-- 提供方应用信息,用于计算依赖关系 --> <dubbo:application name="demo_provider" /> <!-- 使用multicast广播注册中心暴露服务地址 --> <!--<dubbo:registry address="multicast://224.5.6.7:1234" />--> <!-- 使用zookeeper注册中心暴露服务地址 --> <dubbo:registry address="zookeeper://localhost:2181" /> <!-- 用dubbo协议在20880端口暴露服务 --> <dubbo:protocol name="dubbo" port="20880" /> <!-- 声明需要暴露的服务接口 --> <dubbo:service interface="com.gemu.intf.DemoService" ref="demoService" /></beans>
package com.gemu;import com.gemu.intf.DemoService;/** * Created by Gemu on 2/27/2016 0027. */public class DemoServiceImpl implements DemoService { public String build() { System.out.println("build success"); return "build success and return success"; }}
Provider.java 内容如下, 启动dubbo。
package com.gemu;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;/** * Created by Gemu on 2/27/2016 0027. */public class Provider { public static void main(String[] args) throws IOException { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appCtx.xml"); ctx.start(); System.out.println("我咋这么帅呢"); System.in.read(); }}
那么现在服务端已经准备好了,接下来就是消费端了。
DubboConsumer中的 appCtx.xml spring配置如下:
<?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_consumer" /> <!-- 使用zookeeper注册中心暴露服务地址 --> <!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> --> <dubbo:registry address="zookeeper://127.0.0.1:2181" /> <!-- 生成远程服务代理,可以像使用本地bean一样使用demoService --> <dubbo:reference id="demoService" interface="com.gemu.intf.DemoService" /></beans>PS:有报错的地方就 alt + enter 一下就好了,小灯泡还是很给力的。
Consumer.java 内容如下:
package com.gemu;import com.gemu.intf.DemoService;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by Gemu on 2/29/2016 0029. */public class Consumer { public static void main(String[] args) { ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("appCtx.xml"); DemoService demoService = (DemoService) ctx.getBean("demoService"); String retStr = demoService.build(); System.out.println(retStr); }}这样我们的准备工作就都做好了。接下来就是测试了。
首先你得启动zookeeper吧,不然还扯什么蛋。然后运行DubboService中的Provider.java(过程有时候慢,不清楚为啥,可能zookeeper的连接有些问题吧),控制台会出现如下内容:
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.我咋这么帅呢
恭喜你,启动成功了。接下来运行DubboConsumer中的Consumer.java,测试消费者是否可以调用此服务吧。如果Provider的控制台和Consumer的控制台分别输入如下内容,那么恭喜你成功了。
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.我咋这么帅呢build success
log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).log4j:WARN Please initialize the log4j system properly.build success and return successProcess finished with exit code 0
PS: Consumer调用服务的过程有点儿慢,不知道是不是我的配置有问题啊?所以仅供参考了,如果神知道啥原因的话就给我留个言哈。
参考:http://my.oschina.net/superman158/blog/466637 http://www.ziliao1.com/Article/Show/7B9AC403C03106FD10EDCE9C35D40123.html
0 0
- 使用maven集成Dubbo
- 使用maven集成Dubbo
- 使用maven集成Dubbo
- Springmvc 集成dubbo (非maven形式)
- maven dubbo spring eclipse 项目集成
- 使用spring-boot集成dubbo的日志
- 已有maven+spring+spring mvc项目,集成dubbo
- dubbo -7持续集成 svn与maven的安装
- dubbo-3 使用maven构建dubbo可执行jar包
- Maven集成dubbo报错Missing artifact com.alibaba:dubbo:jar:2.8.4
- 使用maven-assembly-plugin打包dubbo接口
- 使用maven创建dubbo的提供者
- 使用maven构建dubbo服务jar包
- maven 集成tomcat的使用
- maven同Eclipse集成使用
- maven dubbo
- 在Spring项目中集成使用dubbo实现分布式服务
- idea创建spring boot集成使用dubbo、zookepper
- JS运动框架
- bzoj 2474
- SAS 多个文件导入
- CNN(卷积神经网络)、RNN(循环神经网络)、DNN(深度神经网络)概念区分理解
- UVA 136 Ugly Numbers(队列)
- 使用maven集成Dubbo
- linux集群间配置互信
- Android Studio 加快编译速度
- Java设计模式
- 如何「偷」Android 的内存?
- AmaterasUML是个人觉得最好用的Eclipse UML插件
- Null Object Pattern
- 个人独立网站上线 http://www.16boke.com
- 【c/c++】C++代码一次读取文本文件全部内容到string对象