官网:http://dubbo.apache.org/zh-cn/docs/user/quick-start.html
官网demo: https://github.com/apache/incubator-dubbo
新建项目
父pom文件
<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.ghgcn</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>dubbo-provider</module>
<module>dubbo-api</module>
<module>dubbo-consumer</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.junit>4.12</version.junit>
<version.jdk>1.8</version.jdk>
<version.spring>4.3.16.RELEASE</version.spring>
<version.commons-lang3>3.5</version.commons-lang3>
<dubbo.version>2.6.5</dubbo.version>
<version.netty>4.1.31.Final</version.netty>
<version.curator>2.12.0</version.curator>
<version.logback>1.2.3</version.logback>
</properties>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>${version.jdk}</version>
<scope>system</scope>
<systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${version.spring}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${version.commons-lang3}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>${version.jdk}</source>
<target>${version.jdk}</target>
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
provider pom
<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>
<parent>
<groupId>com.ghgcn</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.ghgcn</groupId>
<artifactId>dubbo-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
<exclusions>
<exclusion>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${version.netty}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.5.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${version.curator}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${version.logback}</version>
</dependency>
</dependencies>
</project>
consumer pom
<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>
<parent>
<groupId>com.ghgcn</groupId>
<artifactId>dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>dubbo-consumer</artifactId>
<dependencies>
<dependency>
<groupId>com.ghgcn</groupId>
<artifactId>dubbo-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${version.netty}</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.10.5.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${version.curator}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${version.logback}</version>
</dependency>
</dependencies>
</project>
api就一个接口
package com.ghg.dubbo.api;
public interface GreetingService {
String sayHello(String name);
}
provider
实现类
package com.ghgcn.dubbo.service.impl;
import com.ghg.dubbo.api.GreetingService;
public class GreetingServiceImpl implements GreetingService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
service-provider.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="demo-provider" />
<!-- 注册中心地址 -->
<!-- <dubbo:registry address="multicast://224.5.6.7:1234" /> -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 声明协议与商品-->
<dubbo:protocol name="dubbo" port="20880" />
<!-- 声明服务的实现类 -->
<bean id="greetingService" class="com.ghgcn.dubbo.service.impl.GreetingServiceImpl" />
<!-- 声明服务 -->
<dubbo:service interface="com.ghg.dubbo.api.GreetingService"
ref="greetingService" />
</beans>
main方法
package com.ghgcn.dubbo;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) throws IOException {
// com.alibaba.dubbo.container.Main.main(args);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "META-INF/spring/service-provider.xml" });
context.start();
System.out.println("provider启动了");
System.in.read(); // press any key to exit
}
}
consumer
dubbo-client.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<dubbo:application name="dubbo-consumer" />
<!-- 注册中心 -->
<dubbo:registry protocol="zookeeper" address="zookeeper://127.0.0.1:2181" />
<!-- 声明要引用的服务 -->
<dubbo:reference id="greetingService" check="false" interface="com.ghg.dubbo.api.GreetingService" />
</beans>
main方法
package com.ghgcn.consumer;
import java.util.Date;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ghg.dubbo.api.GreetingService;
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[]{ "sping/dubbo-client.xml" });
context.start();
GreetingService greetingService = (GreetingService) context.getBean("greetingService"); // get remote service
while (true) {
try {
Thread.sleep(1000);
System.out.println(greetingService.sayHello("刘楠* " + new Date()));
} catch (Throwable throwable) {
throwable.printStackTrace();
}
}
}
}
运行provider与consumer的main方法
可以通过dubbo-admin来查看
遇到的问题
dubbo 启动报 服务注册失败Failed to register dubbo
Exception in thread "main" java.lang.IllegalStateException: Failed to register dubbo://192.168.187.1:20880/com.ghg.dubbo.api.GreetingService?anyhost=true&application=demo-provider&bean.name=com.ghg.dubbo.api.GreetingService&dubbo=2.0.2&generic=false&interface=com.ghg.dubbo.api.GreetingService&methods=sayHello&pid=9092&side=provider×tamp=1545384997858 to registry 127.0.0.1:2181, cause: Failed to register dubbo://192.168.187.1:20880/com.ghg.dubbo.api.GreetingService?anyhost=true&application=demo-provider&bean.name=com.ghg.dubbo.api.GreetingService&dubbo=2.0.2&generic=false&interface=com.ghg.dubbo.api.GreetingService&methods=sayHello&pid=9092&side=provider×tamp=1545384997858 to zookeeper zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?application=demo-provider&dubbo=2.0.2&interface=com.alibaba.dubbo.registry.RegistryService&pid=9092×tamp=1545384997854, cause: KeeperErrorCode = Unimplemented for /dubbo/com.ghg.dubbo.api.GreetingService/providers/dubbo%3A%2F%2F192.168.187.1%3A20880%2Fcom.ghg.dubbo.api.GreetingService%3Fanyhost%3Dtrue%26application%3Ddemo-provider%26bean.name%3Dcom.ghg.dubbo.api.GreetingService%26dubbo%3D2.0.2%26generic%3Dfalse%26interface%3Dcom.ghg.dubbo.api.GreetingService%26methods%3DsayHello%26pid%3D9092%26side%3Dprovider%26timestamp%3D1545384997858
at com.alibaba.dubbo.registry.support.FailbackRegistry.register(FailbackRegistry.java:150)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.register(RegistryProtocol.java:126)
at com.alibaba.dubbo.registry.integration.RegistryProtocol.export(RegistryProtocol.java:146)
at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper.export(ProtocolFilterWrapper.java:98)
at com.alibaba.dubbo.qos.protocol.QosProtocolWrapper.export(QosProtocolWrapper.java:60)
at com.alibaba.dubbo.rpc.protocol.ProtocolListenerWrapper.export(ProtocolListenerWrapper.java:55)
at com.alibaba.dubbo.rpc.Protocol$Adaptive.export(Protocol$Adaptive.java)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrlsFor1Protocol(ServiceConfig.java:513)
at com.alibaba.dubbo.config.ServiceConfig.doExportUrls(ServiceConfig.java:358)
at com.alibaba.dubbo.config.ServiceConfig.doExport(ServiceConfig.java:317)
解决方法:
这里的问题主要是服务端的zookeeper版本和客户端的zookeeper版本不同。
只要降低一下curator-framework的版本就可以了,
<version.curator>2.12.0</version.curator>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${version.curator}</version>
</dependency>
终于走出第一走了。