#Dubbo 接口 、提供者、消费者
注册中心使用 zookeeper ,安装省略
dubbo 管理控制台 dubbo-admin 安装 jdk1.8 环境下 需要重新源码编译项目,dubbo-admin-jdk1.8下载地址
一、dubbo-helloworld-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>dubbo-helloworld-api</groupId>
<artifactId>dubbo-helloworld-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>api</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
</dependencies>
</project>
HelloService.java
package top.ljaer.dubbo.service;
public interface HelloService {
String sayHello(String name);
}
将 dubbo-helloworld-api 打包放入本地 maven 仓库
直接 maven-install 就可以自动打包到本地仓库
二、dubbo-helloworld-service 项目 (服务提供者)
项目目录
pom.xml
需要在 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>dubbo-helloworld-service</groupId>
<artifactId>dubbo-helloworld-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>server</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end -->
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->
<!-- 注册中心zookeeper begin -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end -->
<!-- log begin -->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->
<!-- other begin -->
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>top.ljaer</groupId>
<artifactId>dubbo-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
HelloServiceImpl.java
package com.dubbo.service.impl;
import top.ljaer.dubbo.service.HelloService;
public class HelloServiceImpl implements HelloService{
public String sayHello(String name) {
System.out.println("init : " + name);
return "hello "+name;
}
}
applicationProvider.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" />
<dubbo:protocol name="dubbo" port="20880" />
<!-- 和本地bean一样实现服务 -->
<bean id="helloService" class="com.dubbo.service.impl.HelloServiceImpl" />
<!-- 向注册中心注册暴漏服务地址,注册服务 -->
<dubbo:service interface="top.ljaer.dubbo.service.HelloService"
ref="helloService" executes="10" />
</beans>
ServiceMain.java
package main;
import java.io.IOException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ServiceMain {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationProvider.xml" });
context.start();
System.out.println("输入任意按键退出 ~ ");
System.in.read();
context.close();
}
}
执行本地测试方法 ServiceMain.main\
dubbo 服务启动起来
- [DUBBO] Subscribe: provider://192.168.12.127:20880/top.ljaer.dubbo.service.HelloService?anyhost=true&application=dubbo-demo&category=configurators&check=false&dubbo=2.5.3&executes=10&interface=top.ljaer.dubbo.service.HelloService&methods=sayHello&pid=9716&revision=0.0.1-SNAPSHOT&side=provider×tamp=1489738664243, dubbo version: 2.5.3, current host: 127.0.0.1
- [DUBBO] Notify urls for subscribe url provider://192.168.12.127:20880/top.ljaer.dubbo.service.HelloService?anyhost=true&application=dubbo-demo&category=configurators&check=false&dubbo=2.5.3&executes=10&interface=top.ljaer.dubbo.service.HelloService&methods=sayHello&pid=9716&revision=0.0.1-SNAPSHOT&side=provider×tamp=1489738664243, urls: [empty://192.168.12.127:20880/top.ljaer.dubbo.service.HelloService?anyhost=true&application=dubbo-demo&category=configurators&check=false&dubbo=2.5.3&executes=10&interface=top.ljaer.dubbo.service.HelloService&methods=sayHello&pid=9716&revision=0.0.1-SNAPSHOT&side=provider×tamp=1489738664243], dubbo version: 2.5.3, current host: 127.0.0.1
输入任意按键退出 ~
二、dubbo-helloworld-client 项目 (服务消费者)
###项目目录
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>dubbo-helloworld-client</groupId>
<artifactId>dubbo-helloworld-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>client</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- spring begin -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!-- spring end -->
<!-- dubbo begin -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.5.3</version>
</dependency>
<!-- dubbo end -->
<!-- 注册中心zookeeper begin -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.3.6</version>
</dependency>
<!-- 注册中心zookeeper end -->
<!-- log begin -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.15</version>
<exclusions>
<exclusion>
<groupId>com.sun.jdmk</groupId>
<artifactId>jmxtools</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.jmx</groupId>
<artifactId>jmxri</artifactId>
</exclusion>
<exclusion>
<artifactId>jms</artifactId>
<groupId>javax.jms</groupId>
</exclusion>
<exclusion>
<artifactId>mail</artifactId>
<groupId>javax.mail</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- log end -->
<!-- other begin -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.8</version>
</dependency>
<dependency>
<groupId>com.xbz</groupId>
<artifactId>dubbo-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- other end -->
</dependencies>
</project>
log4j.properties
# Set root category priority to INFO and its only appender to CONSOLE.
log4j.rootCategory=INFO, CONSOLE
#log4j.rootCategory=INFO, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=INFO
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
### applicationConsumer.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-helloworld"/>
<dubbo:registry address="zookeeper://127.0.0.1:2181" />
<!-- 向注册中心订阅服务 -->
<dubbo:reference id="helloService" interface="top.ljaer.dubbo.service.HelloService" />
</beans>
### ClientMain.java
package main;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import top.ljaer.dubbo.service.HelloService;
public class ClientMain {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "applicationConsumer.xml" });
context.start();
HelloService service = (HelloService) context.getBean("helloService");
System.out.println(service.sayHello("world"));
context.close();
}
}
执行消费者 ClientMain.main 方法调用服务提供者的服务
在消费者方法中打一个断点可以在 dubbo-admin 中消费者查看到消费者记录+1
- [DUBBO] Successed connect to server /192.168.12.127:20880 from NettyClient 192.168.12.127 using dubbo version 2.5.3, channel is NettyChannel [channel=[id: 0x4409e975, /192.168.12.127:57852 => /192.168.12.127:20880]], dubbo version: 2.5.3, current host: 192.168.12.127
- [DUBBO] Start NettyClient dell-3020-PC/192.168.12.127 connect to the server /192.168.12.127:20880, dubbo version: 2.5.3, current host: 192.168.12.127
- [DUBBO] Refer dubbo service top.ljaer.dubbo.service.HelloService from url zookeeper://127.0.0.1:2181/com.alibaba.dubbo.registry.RegistryService?anyhost=true&application=dubbo-helloworld&check=false&dubbo=2.5.3&executes=10&interface=top.ljaer.dubbo.service.HelloService&methods=sayHello&pid=3920&revision=0.0.1-SNAPSHOT&side=consumer×tamp=1489741290341, dubbo version: 2.5.3, current host: 192.168.12.127
hello world