有关dubbo的基础、架构等介绍请参考之前博客:Dubbo背景及架构简介
XML版(官方推荐)示例请参考:
1. 创建暴露服务模块(dubbo-demo-api)
本模块下没有实际的业务逻辑,主要是定义提供者和消费者公用服务接口
/**
* 需要暴露出去的服务
*
*/
public interface HelloService {
/**
* say hello
*
* @param name
* @return
*/
String sayHello(String name);
}
2. 创建服务提供者和消费者父模块管理和引入公用依赖
依赖版本管理在父工程(dubbo-demo),采用dubbo-2.7.5
和xml或者注解版本不同的是不需要依赖spring
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-demo</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>dubbo-api-demo</artifactId>
<packaging>pom</packaging>
<name>dubbo-api-demo</name>
<dependencies>
<!-- 引入Dubbo依赖,原生api不需要依赖spring -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 使用zookeeper作为注册中心 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<!-- 使用curator作为zookeeper客户端 -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<!-- 引入需要暴露的服务 -->
<dependency>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-demo-api</artifactId>
<version>0.0.1</version>
</dependency>
</dependencies>
<modules>
<module>dubbo-api-demo-consumer</module>
<module>dubbo-api-demo-provider</module>
</modules>
</project>
3. 创建服务提供者
1).创建dubbo-api-demo-provider模块
2).编写服务实现类和启动类(两种启动方式)
1).创建dubbo-api-demo-provider模块
<?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>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-api-demo</artifactId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api-demo-provider</artifactId>
</project>
2).编写服务实现类(HelloServiceImpl)和启动类(BootstrapProviderApplication及ExportProviderApplication)
package com.qqxhb.api.provider.impl;
import org.apache.dubbo.rpc.RpcContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.demo.api.HelloService;
public class HelloServiceImpl implements HelloService {
private static final Logger logger = LoggerFactory.getLogger(HelloServiceImpl.class);
public String sayHello(String name) {
logger.info("========Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
}
}
package com.qqxhb.api.provider;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.api.provider.impl.HelloServiceImpl;
import com.qqxhb.demo.api.HelloService;
/**
*
* 通过DubboBootstrap方式启动服务
*
*/
public class BootstrapProviderApplication {
private static final Logger logger = LoggerFactory.getLogger(ExportProviderApplication.class);
public static void main(String[] args) throws Exception {
ServiceConfig<HelloServiceImpl> service = new ServiceConfig<>();
service.setInterface(HelloService.class);
service.setRef(new HelloServiceImpl());
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("dubbo-api-demo-provider"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")).service(service).start();
logger.info("dubbo service started......");
bootstrap.await();
}
}
package com.qqxhb.api.provider;
import java.util.concurrent.CountDownLatch;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.api.provider.impl.HelloServiceImpl;
import com.qqxhb.demo.api.HelloService;
/**
*
* 通过服务导出的方式启动服务
*
*/
public class ExportProviderApplication {
private static final Logger logger = LoggerFactory.getLogger(ExportProviderApplication.class);
public static void main(String[] args) throws Exception {
ServiceConfig<HelloServiceImpl> service = new ServiceConfig<>();
service.setInterface(HelloService.class);
service.setRef(new HelloServiceImpl());
service.setApplication(new ApplicationConfig("dubbo-api-demo-provider"));
service.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
service.export();
logger.info("dubbo service started......");
new CountDownLatch(1).await();
}
}
4. 创建服务消费者
1).创建dubbo-api-demo-consumer模块
2).消费者启动类(同样有两种方式)
1).创建dubbo-api-demo-consumer模块
<?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>
<groupId>com.qqxhb</groupId>
<artifactId>dubbo-api-demo</artifactId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-api-demo-consumer</artifactId>
</project>
2).消费者启动类(BootstrapConsumerApplication 、ExportConsumerApplication)
package com.qqxhb.api.consumer;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.config.utils.ReferenceConfigCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.demo.api.HelloService;
/**
*
* 通过DubboBootstrap方式启动服务
*
*/
public class BootstrapConsumerApplication {
private static final Logger logger = LoggerFactory.getLogger(BootstrapConsumerApplication.class);
public static void main(String[] args) throws Exception {
ReferenceConfig<HelloService> reference = new ReferenceConfig<>();
reference.setInterface(HelloService.class);
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("dubbo-api-demo-consumer"))
.registry(new RegistryConfig("zookeeper://127.0.0.1:2181")).reference(reference).start();
HelloService helloService = ReferenceConfigCache.getCache().get(reference);
String message = helloService.sayHello("Dubbo Bootstrap");
logger.info("============{}", message);
}
}
package com.qqxhb.api.consumer;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.qqxhb.demo.api.HelloService;
/**
*
* 通过服务导出的方式调用服务
*
*/
public class ExportConsumerApplication {
private static final Logger logger = LoggerFactory.getLogger(BootstrapConsumerApplication.class);
public static void main(String[] args) throws Exception {
ReferenceConfig<HelloService> reference = new ReferenceConfig<>();
reference.setApplication(new ApplicationConfig("dubbo-demo-api-consumer"));
reference.setRegistry(new RegistryConfig("zookeeper://127.0.0.1:2181"));
reference.setInterface(HelloService.class);
HelloService service = reference.get();
String message = service.sayHello("Dubbo Export");
logger.info("============{}", message);
}
}
5. 启动测试
1).依赖zookeeper作为注册中心,因此需要优先启动zookeeper
zookeeper相关知识请参考之前博客:Zookeeper入门及单机及集群环境搭建
2). 启动任意一个 ProviderApplication 、启动任意一个ConsumerApplication
ProviderApplication 端窗口打印日志======== result: Hello Dubbo Bootstrap, response from provider: 192.168.25.1:20880
ConsumerApplication 端窗口打印日志========Hello Dubbo Bootstrap, request from consumer: /192.168.25.1:7079