使用IntelliJ IDEA创建简单的Dubbo实例
这个博客是在https://blog.csdn.net/Crazer_cy/article/details/80397649篇文章上的基础上,自己学习用的。
- Zookeeper为dubbo的注册中心,dubbo服务的生产者和消费者都需要在Zookeeper进行注册;
- 下载zookeeper压缩包并解压;
- 进入conf目录将 zoo_sample.cfg 改名为 zoo.cfg;
- 进入bin目录双击zkServer.cmd,若启动成功,则windows单机版zookeeper搭建成功
- 使用duboo-admin
使用IntelliJ IDEA搭建Dubbo:创建一个maven空项目,作为项目的父工程
在pom.xml添加以下公共的依赖:
- <?xml version="1.0" encoding="UTF-8"?>
- <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/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.dubbo</groupId>
- <artifactId>dubbo_demo</artifactId>
- <packaging>pom</packaging>
- <version>1.0-SNAPSHOT</version>
- <properties>
- <motan.version>0.3.0</motan.version>
- <dubbo.version>2.5.3</dubbo.version>
- <dubbox.version>2.8.4</dubbox.version>
- <spring.version>4.3.6.RELEASE</spring.version>
- <java.version>1.8</java.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dubbo</artifactId>
- <version>2.5.3</version>
- <exclusions>
- <exclusion>
- <groupId>org.springframework</groupId>
- <artifactId>spring</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
- <groupId>com.github.sgroschupf</groupId>
- <artifactId>zkclient</artifactId>
- <version>0.1</version>
- </dependency>
- <!-- spring相关 -->
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-beans</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-aop</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-tx</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-orm</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-context-support</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jms</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjrt</artifactId>
- <version>1.6.11</version>
- </dependency>
- <dependency>
- <groupId>org.aspectj</groupId>
- <artifactId>aspectjweaver</artifactId>
- <version>1.6.11</version>
- </dependency>
- </dependencies>
- <modules>
- <module>dubbo_api</module>
- <module>dubbo_consumer</module>
- <module>dubbo_provider</module>
- </modules>
- </project>
在刚才创建的dubbo_demo下创建一个新的Module:dubbo_api(服务接口)
dubbo_api的pom.xml文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>dubbo_demo</artifactId>
- <groupId>com.dubbo</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>dubbo_api</artifactId>
- <packaging>jar</packaging>
- </project>
重复3、4步骤,创建dubbo_provider(服务生产者),对应的pom.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>dubbo_demo</artifactId>
- <groupId>com.dubbo</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>dubbo_provider</artifactId>
- <dependencies>
- <dependency>
- <groupId>com.dubbo</groupId>
- <artifactId>dubbo_api</artifactId>
- <version>1.0-SNAPSHOT</version>
- <scope>compile</scope>
- </dependency>
- </dependencies>
- </project>
再重复3、4步骤,创建dubbo_consumer(服务消费者),对应pom.xml文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <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/xsd/maven-4.0.0.xsd">
- <parent>
- <artifactId>dubbo_demo</artifactId>
- <groupId>com.dubbo</groupId>
- <version>1.0-SNAPSHOT</version>
- </parent>
- <modelVersion>4.0.0</modelVersion>
- <artifactId>dubbo_consumer</artifactId>
- <dependencies>
- <dependency>
- <groupId>com.dubbo</groupId>
- <artifactId>dubbo_api</artifactId>
- <version>1.0-SNAPSHOT</version>
- <scope>compile</scope>
- </dependency>
- </dependencies>
- </project>
最后项目结构如下:
框架搭建起来之后,开始写代码和配置文件,首先是dubbo_api:需要定义服务接口:
- package com.api.service;
- /**
- * 定义服务接口
- */
- public interface DemoService {
- String sayHello(String name);
- }
然后再dubbo_provider中实现上述接口(由于在pom.xml文件中,将dubbo_api作为依赖,故能引入上述接口):
- package com.provider.service;
- import com.api.service.DemoService;
- public class DemoServiceImpl implements DemoService {
- public String sayHello(String name) {
- return "Hello "+name;
- }
- }
dubbo_provider项目中的resource文件包含dubbo-provider.xml和springmvc.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.xsd
- http://code.alibabatech.com/schema/dubbo
- http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
- <!-- 提供方应用信息,用于计算依赖关系 -->
- <dubbo:application name="dubbo_provider" />
- <!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="zookeeper://127.0.0.1:2181" />
- <!-- 用dubbo协议在20880端口暴露服务 -->
- <dubbo:protocol name="dubbo" port="20880" />
- <!-- 声明需要暴露的服务接口 -->
- <dubbo:service interface="com.api.service.DemoService" ref="demoService" />
- <!-- 接口实现类-->
- <bean id="demoService" class="com.provider.service.DemoServiceImpl"/>
- </beans>
springmvc.xml如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd"
- default-autowire="byName">
- <aop:aspectj-autoproxy />
- <context:component-scan base-package="com" />
- <import resource="classpath:dubbo-provider.xml" />
- </beans>
再在该工程下写一个测试类,后面测试用到:
- package com.provider.test;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.io.IOException;
- public class ProviderTest {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");
- context.start();
- System.out.println("Dubbo provider start...");
- try {
- System.in.read(); // 按任意键退出
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
接下来开始写消费者——dubbo_consumer的代码和配置文件:resource中的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="dubbo_consumer" />
- <!-- 使用multicast广播注册中心暴露发现服务地址 -->
- <dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
- <!-- 生成远程服务代理,可以和本地bean一样使用demoService -->
- <dubbo:reference id="demoService" interface="com.api.service.DemoService" />
- </beans>
springmvc.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/util
- http://www.springframework.org/schema/util/spring-util-4.0.xsd"
- default-autowire="byName">
- <aop:aspectj-autoproxy />
- <context:component-scan base-package="com" />
- <import resource="classpath:/dubbo-consumer.xml" />
- </beans>
然后写消费者的测试
- package com.consumer;
- import com.api.service.DemoService;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import java.io.IOException;
- public class ConsumerTest {
- public static void main(String[] args) {
- ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });
- context.start();
- DemoService demoService = (DemoService) context.getBean("demoService");
- System.out.println(demoService.sayHello("哈哈哈"));
- try {
- System.in.read();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
到现在整个代码就开发完成了,按照先后顺序,首先启动Zookeeper:zkServer.cmd 然后启动服务生产者测试类,最后启动服务消费者的测试类,当控制台输出以下结果,则代表成功。
服务生产者测试类控制台输出:
服务消费者测试了控制台输出:
使用dubbo-admin管理平台查看运行情况。
使用IntelliJ IDEA创建简单的Dubbo实例的更多相关文章
- 用Intellij Idea创建简单的Servlet
Servlet作为Java服务端程序,使用起来还是挺方便的,下面是具体配置过程,我用的是Intellij Idea. 1. 做好必要准备,Intellij Idea(或者Eclipse for J2E ...
- 使用Intellij Idea创建简单Maven项目(转)
我是学Java Web的,基本靠自学,在网上收集了各种视频资料,逐一的看,代码逐一的敲.学习了这么久之前一直未成想过要把自己的学习路程记录下来,在网上也看到过很多人把自己的学习历程以及遇到的问题写在了 ...
- 使用IntelliJ IDEA创建简单的Spring Boot项目
方法一: File - New -Project 创建结束后进行测试运行,修改代码如下: package com.springboot.testone; import org.springframew ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(二)
1. 创建一个Controller类 package com.example.demo; import org.springframework.web.bind.annotation.RequestM ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(一)
.新建项目File-->New-->Project-->Spring Initializr 点击Finish,一个Spring Boot web应用就创建好了.
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(五)
使用application.ym进行多环境配置 1.配置激活选项 spring: profiles: active: dev 2.在配置文件添加若干个英文状态下的短横线即可区分 spring: pro ...
- 创建简单的Telnet实例
step1.先加入库SuperSocket.Common.dll, SuperSocket.SocketBase.dll, SuperSocket.SocketEngine.dll,log4net.d ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(四)
多环境配置 1. 在springBoot多环境配置文件名需要满足application-{profile}.properties的格式,其中{profile}对应你的环境标识,例如: (1)appli ...
- Spring Boot 使用IntelliJ IDEA创建一个web开发实例(三)
属性配置 1.配置application.properties文件 配置web访问端口和context path server.port = 8081 server.servlet.context-p ...
随机推荐
- python操作redis集群
strictRedis对象方法用于连接redis 指定主机地址,port与服务器连接,默认db是0,redis默认数据库有16个,在配置文件中指定database 16 上代码 .对redis的单实例 ...
- 二、JAVA基本数据类型:内置数据类型,引用类型
变量的值存储在内存中,内存管理系统通过变量的类型分配存储空间,且该空间只能存储该类型数据,通过定义不同的变量,在内存中储存不同类型的数据. JAVA的两大数据类型 1. 内置数据类型 2.引用数据类型 ...
- Vue - v-for 的延伸用法
1.v-for 合并标签template 一起使用 2.vue.set 1.v-for 合并标签template 一起使用 之前在设计table的时候,如果使用v-for ,会直接放在tr里面,一次产 ...
- RHEL 6 和 RHEL 7 的一些有关运行级别,服务管理,服务启动等方面的区别介绍
systemd是7中的新命令组,集成了service和chkconfig的功能.system命令可参考:https://www.cnblogs.com/ray-bk/p/10415173.html 运 ...
- java面向对象编程(二)-构造方法(函数)
1.类的构造方法介绍 什么是构造方法呢?在回答这个问题之前,我们来看一个需求:我们在创建人类的对象时,是先把一个对象创建好后,再给他的年龄和姓名属性赋值,如果现在我要求,在创建人类的对象时,就直接指定 ...
- vs2015和Oracle在一起时的Shit问题
VS2015在连接Oracle时,必须要安装到一个不含有空格的目录中去,否则连接不上Oracle,至于为什么,不知道,鬼知道,日的. 如果你不幸以前安装过VS2015,安装到它的默认的什么“progr ...
- 自动化测试-6.selenium的css定位
前言 大部分人在使用selenium定位元素时,用的是xpath定位,因为xpath基本能解决定位的需求.css定位往往被忽略掉了,其实css定位也有它的价值,css定位更快,语法更简洁.这一篇css ...
- 使用Sublime Text 3进行Markdown 编辑+实时预览
这种做法可能会对你的磁盘IO造成一小部分性能负担,但负面影响足以忽略. 另外,由于这种频率的读写会被磁盘缓存接管,不必担心磁盘寿命的影响. 对于刚安装好的Sublime Text,我们需要安装一个软件 ...
- Ubuntu16.04上添加用户以及修改用户所属的组
我的问题是这样的,我的本地的电脑上有一个用户以及一个用户组,我还想添加其他的用户,并且这个用户属于这个已有的用户组 <鸟哥的linux私房菜>针对的是centos系统,还是有一些不一样 实 ...
- 解决Myeclipse通过svn导入项目后,项目直接报错问题
在使用Myeclipse2015通过SNV导入项目后,项目直接报错,如下图: 点开后报错详细信息如下: Multiple markers at this line - The type java.la ...