本案例是dubbo
版本:2.7.4.1 org.apache.dubbo
建一个工程为dubbo-demo
,再创建三个module
dubbo-user-api
dubbo-user-provider
dubbo-user-consumer
dubbo-user-api
DemoService.java
接口
package com.tian.dubbo.service;
public interface DemoService {
String test(String string);
}
然后打成jar给dubbo-user-provider
和dubbo-user-consumer
使用
mvn clean install
dubbo-user-api.jar
zookeeper
本地启动一个zookeepeer
,作为dubbo
的注册中心,(dubbo
推荐zookeeper
为注册中心)
dubbo-user-provider
pom.xml
中添加
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<dubbo.version>2.5.3</dubbo.version>
<spring.version>4.3.6.RELEASE</spring.version>
<java.version>1.8</java.version>
<log4j.version>1.2.17</log4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<artifactId>dubbo-user-api</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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>
</dependencies>
创建一个DemoServiceImpl.java
public class DemoServiceImpl implements DemoService {
@Override
public String test(String msg) {
System.out.println("接收到参数:" + msg);
return "provider:" + msg;
}
}
创建启动类ProviderApplication.java
public class ProviderApplication {
private static String zookeeperHost = System.getProperty("zookeeper.address", "127.0.0.1");
public static void main(String[] args) throws Exception {
ServiceConfig<DemoService> service = new ServiceConfig<>();
service.setApplication(new ApplicationConfig("first-dubbo-provider"));
service.setRegistry(new RegistryConfig("zookeeper://" + zookeeperHost + ":2181"));
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.export();
System.out.println("Dubbo server started");
new CountDownLatch(1).await();
}
}
启动
输出:Dubbo server started
启动成功,等着消费方调用。下面来说说消费方
dubbo-user-consumer
pom.xml
添加
<properties>
<dubbo.version>2.5.3</dubbo.version>
<spring.version>4.3.6.RELEASE</spring.version>
<java.version>1.8</java.version>
<log4j.version>1.2.17</log4j.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<version>2.7.4.1</version>
</dependency>
<dependency>
<artifactId>dubbo-user-api</artifactId>
<groupId>com.tian.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
<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>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
启动类ConsumerApplication.java
启动并调用
输出:provider:dubbo
dubbo-user-provider
中输出:接收到参数:dubbo
xml
方式provider.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="provider"/>
<!-- 使用zookeeper注册中心暴露服务地址 -->
<dubbo:registry address="zookeeper://localhost:2181" protocol="zookeeper" check="false"/>
<!-- 用dubbo协议在随机端口暴露服务 -->
<dubbo:protocol id="dubbo" name="dubbo" port="-1" threads="200" />
<!-- 声明需要暴露的服务接口 -->
<dubbo:service interface="com.tian.dubbo.service.DemoService" ref="demoService"/>
<!-- 和本地bean一样实现服务 -->
<bean id="demoService" class="com.tian.dubbo.service.impl.DemoServiceImpl"/>
</beans>
启动类ProviderApplication.java
public class ProviderApplication {
public static void main(String[] args) throws IOException {
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("provider.xml");
System.err.println(context.getDisplayName() + ": here");
context.start();
System.err.println("服务已经启动...");
System.in.read();
}
}
log4j.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
启动输出:
ConsumerApplication.java
public class ConsumerApplication {
public static void main(String[] args) {
//测试常规服务
ClassPathXmlApplicationContext context =
new ClassPathXmlApplicationContext("consumer.xml");
context.start();
System.err.println("consumer start");
DemoService demoService = context.getBean(DemoService.class);
System.out.println(demoService.test("dubbo xml"));
}
}
consumer.xml
<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="demotest-consumer" owner="programmer" organization="dubbox"/>
<!--向 zookeeper 订阅 provider 的地址,由 zookeeper 定时推送-->
<dubbo:registry address="zookeeper://localhost:2181"/>
<!--使用 dubbo 协议调用定义好的 api.PermissionService 接口-->
<dubbo:reference id="demoService" interface="com.tian.dubbo.service.DemoService"/>
</beans>
log4j.xml
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="CONSOLE" />
</root>
</log4j:configuration>
启动输出:
证明调用成功,再看provider的输出
自此,一个xml
版本的dubbo demo
搞定。
DemoServiceImpl.java
中的@Service使用的是org.apache.dubbo
下面的
import com.tian.dubbo.service.DemoService;
import org.apache.dubbo.config.annotation.Service;
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String test(String msg) {
System.out.println("接收到参数:" + msg);
return "provider:" + msg;
}
}
启动类
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.io.IOException;
@Configuration
@EnableDubbo(scanBasePackages = "com.tian.dubbo.service.impl")//扫描添加了@Service的注解
@PropertySource("classpath:dubbo-provider.properties")//配置文件的扫描
public class ProviderApplication {
public static void main(String[] args) throws IOException {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(ProviderApplication.class);
context.start();
System.err.println("服务已经启动...");
System.in.read(); //需要等待
}
}
dubbo-provider.properties
dubbo.application.name=annotation-provider
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
启动
dubbo-consumer.properties
dubbo.application.name=annotation-consumer
dubbo.registry.address=zookeeper://localhost:2181
dubbo.consumer.timeout=3000
import com.tian.dubbo.service.DemoService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@EnableDubbo(scanBasePackages = "com.tian.dubbo")//启用Dubbo扫描并注入对象
@PropertySource("classpath:dubbo-consumer.properties")//扫描配置文件
public class ConsumerApplication {
//这里需要使用Dubbo来进行注入
@Reference
DemoService demoService;
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new
AnnotationConfigApplicationContext(ConsumerApplication.class);
ConsumerApplication customer = context.getBean(ConsumerApplication.class);
String say = customer.demoService.test("dubbo annotation");
System.out.println(say);
}
}
启动
调用成功。