本来想简单过一下dubbo不同配置下的使用示例-http://dubbo.apache.org/books/dubbo-user-book/configuration/api.html,但是到自己实践的时候,还是发生了一系列问题,网上不少资料参考价值还是不高。
来来来,这里给出的是dubbo直接采用API形式的配置(这里使用了zookeeper,所以注意本地起zk),比较重,官方也说了:
新建一个maven空白工程,pom.xml内容如下,注意引用javaassist和netty:
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>dubbo_zxf_api</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.1</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.20.0-GA</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>3.6.10.Final</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.22</version>
</dependency>
</dependencies>
</project>
整个工程目录结构如下图所示:
注意如果你本地maven仓库不能找到新版本的dubbo,比如2.6.X,可以先从mvn官方库:http://mvnrepository.com/artifact/com.alibaba/dubbo/2.6.1
从这里下载jar和pom,然后添加到本地maven库里,添加的命令:
mvn install:install-file -Dfile=/Users/mfhj-dz-001-302/Downloads/dubbo-2.6.1.jar -DgroupId=com.alibaba -DartifactId=dubbo -Dversion=2.6.1 -Dpackaging=jar
。
具体代码,下面贴生产者和消费者代码:
/**
* Created by zxf on 18/5/28.
*/
import com.alibaba.dubbo.common.utils.LogUtil;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ServiceConfig;
import facade.TestService;
import facade.TestServiceImpl;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
public class DubboProvider {
private static AtomicBoolean running = new AtomicBoolean(false);
public static void main(String[] args) {
while(true){
if(!running.get()){
// 服务实现
TestService testService = new TestServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-api-test");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("127.0.0.1:2181");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(100);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
ServiceConfig<TestService> service = new ServiceConfig<TestService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(TestService.class);
service.setRef(testService);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
running.set(true);
}
LogUtil.printList(new ArrayList<Object>());
}
}
}
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import facade.TestService;
/**
* Created by zxf on 18/5/29.
*/
public class DubboConsumer {
public static void main(String[] args) {
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-api-test");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setProtocol("zookeeper");
registry.setAddress("127.0.0.1:2181");
// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<TestService> reference = new ReferenceConfig<TestService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(TestService.class);
reference.setVersion("1.0.0");
// 和本地bean一样使用xxxService
TestService testService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
String ret = testService.hello("haha");
reference.destroy();
System.out.println(ret);
}
}
有问题欢迎大家一起讨论~