下面介绍下如何从零开始,使用idea+maven+spring(不使用zookeeper)搭建一个dubbo项目,并正常运行起来。
一.新建一个idea工程,选择maven就行。
选择maven工程,不需要选择“create from archetype”。点击“next”,输入groupiD和ArtifactId,
点击“next”,完成工程创建。
这个是空的父工程,用于包含两个module子工程,它里面不需要写java代码。我们可以在该父工程的pom文件中引入一些所需的公共依赖,这样其子工程就都可以使用这些公共依赖,而不用每个子工程都分别引入相同的依赖。
其pom内容如下:
<?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>org.luo.dubbo.learn</groupId>
<artifactId>mydubbo02</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring.version>4.3.5.RELEASE</spring.version>
<dubbo.version>2.6.2</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 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-tx</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-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<!-- 排除传递spring依赖 -->
<artifactId>spring</artifactId>
<groupId>org.springframework</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
</dependency>
</dependencies>
</project>
二.创建module,名为dubbo-provider。
右键项目名->new->module,选择maven工程,点击next:
输入module的ArtifactId,完成子module的创建。
1.在dubbo-provider的Java目录下,创建三个类,分别如下:
(1)HelloService接口(服务提供者)
public interface HelloService {
String hello(String msg);
}
(2)helloServiceImpl实现类
public class HelloServiceImpl implements HelloService {
@Override
public String hello(String msg) {
System.out.println("客户端传过来的信息:" + msg);
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(calendar.getTime());
return "服务端返回的信息:" + msg + ",当前时间:" + currentTime;
}
}
(3)Provider类:用于启动Provider提供的服务。
public class Provider {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
context.start();
System.in.read();
}
}
(4)在resources目录下,创建springContext.xml文件,用于配置dubbo的服务提供方(provider)。
<?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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供应用方信息,用于计算依赖关系-->
<dubbo:application name="hello-world-app"/>
<!--使用multicast广播注册中心暴露服务地址-->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!--用dubbo协议在20880端口暴露服务-->
<dubbo:protocol name="dubbo" port="20880"/>
<!--在spring中注册bean-->
<bean id="helloService" class="org.service.impl.HelloServiceImpl"/>
<!--声明需要暴露的服务接口-->
<dubbo:service interface="org.service.HelloService" ref="helloService"/>
</beans>
dubbo-provider模块创建完成。
三.创建一个module,名为dubbo-consumer,即服务消费方。
与上述一样,创建一些类与xml文件:
(1)远程服务的本地接口HelloService
public interface HelloService {
String hello(String msg);
}
(2)创建Consumer类,用于启动消费者模块。
public class Consumer {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("springContext.xml");
context.start();
HelloService helloService = context.getBean("helloService", HelloService.class);
String hello = helloService.hello("我在学习dubbo");
System.out.println(hello);
}
}
(3)在resources目录下,创建springContext.xml文件,用于配置dubbo的消费者(consumer)。
<?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-4.3.xsd
http://code.alibabatech.com/schema/dubbo
http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<!--提供应用方信息,用于计算依赖关系-->
<dubbo:application name="consumer-of-hello-world-app"/>
<!--使用multicast广播注册中心暴露服务地址-->
<dubbo:registry address="multicast://224.5.6.7:1234"/>
<!--声明需要暴露的服务接口-->
<dubbo:reference id="helloService" interface="org.service.HelloService"/>
</beans>
整个项目搭建完成。
四。项目运行。
1.先启动provider:运行Provider类的main方法。
2.启动Consumer:运行Consumer类的main方法。
此时,Provider打印如下信息:
Consumer会打印如下信息:
服务端返回的信息:我在学习dubbo,当前时间:2018-08-27 02:04:06
项目运行成功。
五.可能会出现的异常:
有些人在启动Provider或者Consumer是会出现如下异常:
Cannot start process, the working directory 'D:\idea_workspace\mydubbo01\idea_workspace\mydubbo01' does not exist。
解决方法:点击Edit-Configurations->Application下的项目->右侧面板中的Configuration,修改working directory即可。