转自官网 。
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">
<parent>
<artifactId>dubbo</artifactId>
<groupId>com.learn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo</artifactId>
<packaging>pom</packaging>
<modules>
<module>dubbo-demo-interface</module>
<module>dubbo-demo-xml</module>
</modules>
<properties>
<dubbo.version>2.7.4.1</dubbo.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
</project>
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">
<parent>
<artifactId>dubbo-demo</artifactId>
<groupId>com.learn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-interface</artifactId>
</project>
HelloService
public interface HelloService {
String sayHello(String name);
}
暴露服务的服务提供方。
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">
<parent>
<artifactId>dubbo-demo-xml</artifactId>
<groupId>com.learn</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-xml-provider</artifactId>
<properties>
<slf4j-log4j12.version>1.7.25</slf4j-log4j12.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.learn</groupId>
<artifactId>dubbo-demo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
Application
public class Application {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-provider.xml");
context.start();
System.in.read();
}
}
HelloServiceImpl,服务方的实现
@Service("helloService")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
dubbo-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" xmlns:context="http://www.springframework.org/schema/context"
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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.learn.dubbo.demo"/>
<dubbo:application name="dubbo-provider"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:registry address="zookeeper://172.17.0.2:2181?backup=172.17.0.3:2181,172.17.0.4:2181&timeout=10000"/>
<dubbo:service interface="com.learn.dubbo.demo.HelloService" ref="helloService"/>
</beans>
dubbo.properties
dubbo.application.qos.port=22222
配置这个,是因为qos-server启动的时候,端口会占用。qos是dubbo的 在线运维命令 ,dubbo2.5.8新版本重构了telnet模块,提供了新的 telnet命令支持 。
log4j.properties
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy HH:mm:ss:SSS z}] %t %5p %c{2}: %m%n
Application,消费方,远程调用服务方的方法。
public class Application {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
context.start();
HelloService helloService = context.getBean("helloService", HelloService.class);
String result = helloService.sayHello("张三");
System.out.println(result);
}
}
dubbo-consumer.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 address="zookeeper://172.17.0.2:2181?backup=172.17.0.3:2181,172.17.0.4:2181&timeout=10000"/>
<dubbo:reference id="helloService" interface="com.learn.dubbo.demo.HelloService"/>
</beans>
dubbo.properties
dubbo.application.qos.port=33333
log4j.properties同上
先看看zookeeper节点
[zk: localhost:2181(CONNECTED) 5] ls / [zookeeper]
运行提供方的application,可以看到zookeeper有多个节点了
[zk: localhost:2181(CONNECTED) 2] ls /ddubbo/com.learn.dubbo.demo.HelloService [configurators, providers]
查看providers的信息:
dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService%3Fanyhost%3Dtrue%26application%3Ddubbo-provider%26bean.name%3Dcom.learn.dubbo.demo.HelloService%26deprecated%3Dfalse%26dubbo%3D2.0.2%26dynamic%3Dtrue%26generic%3Dfalse%26interface%3Dcom.learn.dubbo.demo.HelloService%26methods%3DsayHello%26pid%3D9692%26release%3D2.7.4.1%26side%3Dprovider%26timestamp%3D1577178472840
可以看到暴露了当前服务器的地址
dubbo%3A%2F%2F192.168.102.186%3A20881%2Fcom.learn.dubbo.demo.HelloService
运行消费方的application,控制台输出了信息
zookeeper的节点,多了consumers和routers
[configurators, consumers, providers, routers]
除了 XML 配置 ,还有 属性配置 、 API 配置 、 注解配置 。
schema 、 启动时检查 等示例,可以参考官网。