项目说明

Dubbo-Demo
dubbo的消费者,负责调用服务

Dubbo-Hello-Api
标准api接口,避免服务提供者和消费者书写重复的代码

Dubbo-Hello-Service
dubbo的服务者,提供服务共其他应用调用

Dubbo-Hello-Api

myeclipse中,右键》new》other》maven project,如下

Dubbo学习(二)- Dubbo的hello world_spring


Dubbo学习(二)- Dubbo的hello world_spring_02


修改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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>

<!-- 添加默认的全局,不然会打包失败 -->
<defaultGoal>compile</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

创建com.ahut.hello.service包,添加如下接口

package com.ahut.hello.service;

/**
*
* @ClassName: HelloService
* @Description: Hello业务逻辑接口
* @author cheng
* @date
public interface HelloService

/**
*
* @Title: sayHello
* @Description:
* @param
void

发布项目到maven本地仓库

选中项目》右键,run as》run configuration》maven build

Dubbo学习(二)- Dubbo的hello world_xml_03

Dubbo-Hello-Service

创建Dubbo-Hello-Service项目,和Dubbo-Hello-Api一样,参考上面
修改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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

创建com.ahut.hello.serviceImpl包,添加如下实现

package com.ahut.hello.serviceImpl;

import com.ahut.hello.service.HelloService;

/**
*
* @ClassName: HelloServiceImpl
* @Description: Hello业务逻辑实现
* @author cheng
* @date
public class HelloServiceImpl implements HelloService

/**
* 实现
*/
@Override
public void sayHello(String name) {
System.out.println("Hello "

添加spring-dubbo.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-hello"

<!-- zookeeper注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"

<dubbo:protocol name="dubbo" port="20880"

<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.ahut.hello.serviceImpl.HelloServiceImpl"

<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="com.ahut.hello.service.HelloService"
ref="helloService" executes="10"

</beans>

模拟启动服务,前提要开启zookeeper

package com.ahut;

import java.io.IOException;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
*
* @ClassName: ServiceMain
* @Description:
* @author cheng
* @date
public class ServiceMain

public static void main(String[] args) throws IOException {

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring-dubbo.xml" });
context.start();

System.out.println("输入任意按键退出 ~ ");
System.in.read();
context.close();
}

}

Dubbo-Demo

创建Dubbo-Demo项目,和Dubbo-Hello-Api一样,参考上面
修改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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

添加spring-dubbo.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-demo"

<!-- zookeeper注册中心 -->
<dubbo:registry address="zookeeper://127.0.0.1:2181"

<!-- 获取bean -->
<dubbo:reference id="helloService"
interface="com.ahut.hello.service.HelloService"

</beans>

模拟调用服务

package com.ahut;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ahut.hello.service.HelloService;

/**
*
* @ClassName: DemoMain
* @Description:
* @author cheng
* @date
public class DemoMain

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "spring-dubbo.xml" });
context.start();
HelloService helloService = (HelloService) context.getBean("helloService");
helloService.sayHello("world");

context.close();
}

}

可能出现的问题

Dubbo-Hello-Api项目打包失败,出现如下错误

[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.ahut:Dubbo-Hello-Api:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-compiler-plugin is missing. @ line 10, column 12
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.096 s
[INFO] Finished at: 2018-04-08T14:32:14+08:00
[INFO] Final Memory: 6M/155M
[INFO] ------------------------------------------------------------------------
[ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoGoalSpecifiedException

解决办法:修改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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>

<!-- 添加默认的全局,不然会打包失败 -->
<defaultGoal>compile</defaultGoal>

<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

Dubbo-Hello-Service或者Dubbo-Demo模拟启动失败,如下

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
at com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.connect(CuratorZookeeperTransporter.java:26)
at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java)
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.<init>(ZookeeperRegistry.java:69)
at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistryFactory.createRegistry(ZookeeperRegistryFactory.java:37)
at com.alibaba.dubbo.registry.support.AbstractRegistryFactory.getRegistry(AbstractRegistryFactory.java:95)
at com.alibaba.dubbo.registry.RegistryFactory$Adaptive.getRegistry(RegistryFactory$Adaptive.java)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.getRegistry(RegistryProtocol.java:200)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:134)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:54)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:91)
at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:54)
at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:505)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:357)
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:316)
at com.alibaba.dubbo.config.ServiceConfig.export(ServiceConfig.java:215)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:121)
at com.alibaba.dubbo.config.spring.ServiceBean.onApplicationEvent(ServiceBean.java:50)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.ahut.ServiceMain.main(ServiceMain.java:18)
Caused by: java.lang.ClassNotFoundException: org.apache.curator.RetryPolicy
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 27

解决办法:修改pom.xml,引入缺少的jar

<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.ahut</groupId>
<artifactId>Dubbo-Hello-Service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>

<!-- 引入Api -->
<dependency>
<groupId>com.ahut</groupId>
<artifactId>Dubbo-Hello-Api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

<!-- dubbo -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
</dependency>

<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.11</version>
</dependency>

<!-- 缺少的jar -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>