Atitit dubbo使用总结 attilax总结 艾龙 总结
1.1. Dubbo提供的注册中心有如下几种类型可供选择: 2
2. 下载dubbo 版本2.6.0 ,但是官网下的是src。没有jar包。。也不提供jar下载。只好通过maven下载 2
3.1. 新建maven配置文件 down_dubbo.txt 格式参照pom文件格式 3
3.3. 找到jar包 C:\Users\Administrator\.m2\repository 4
4.1. Define service interface 6
4.2. Implement service provider 6
6.1. Overview _ DUBBO.mhtml 10
6.3. java-dubbo-使用Multicast注册中心提供服务 - CSDN博客.mhtml 10
6.4. Dubbo入门---搭建一个最简单的Demo框架 - noaman_wgs的博客 - CSDN博客.mhtml 10
1. 概念
1.1. Dubbo提供的注册中心有如下几种类型可供选择:
1.1.1. Multicast注册中心
1.1.2. Zookeeper注册中心
1.1.3. Redis注册中心
1.1.4. Simple注册中心
1.2. Multicast注册中心
不需要启动任何中心节点,只要广播地址一样,就可以互相发现 |
组播受网络结构限制,只适合小规模应用或开发阶段使用。 |
组播地址段: 224.0.0.0 - 239.255.255.255 |
2. 下载dubbo 版本2.6.0 ,但是官网下的是src。没有jar包。。也不提供jar下载。只好通过maven下载
3. 通过maven下载dubbo jar包
最新的貌似2.6.1但是通过maven下载报错,说找不到这个版本的,还好2.6.0的可以找到
3.1. 新建maven配置文件 down_dubbo.txt 格式参照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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>myPrj</artifactId>
<version>1</version>
<name>nammm</name>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
</project>
3.2. 执行下载D:\prgrm\apache-maven-3.5.0\bin\mvn.cmd -f C:\Users\Administrator\Desktop\dubbo\down_dubbo.txt test
3.3. 找到jar包 C:\Users\Administrator\.m2\repository
3.4. 下载后的jar大概13m 77个文件
D:\workspace\dubboprj\libs
classworlds-1.1.jar
commons-cli-1.0.jar
commons-lang3-3.1.jar
commons-logging-1.2.jar
commons-logging-api-1.1.jar
doxia-sink-api-1.0-alpha-7.jar
dubbo-2.6.0.jar
dubbo2.6.0down_jar.txt
google-collections-1.0.jar
javassist-3.20.0-GA.jar
jsr305-2.0.1.jar
junit-3.8.1.jar
junit-3.8.2.jar
log4j-1.2.12.jar
maven-artifact-2.0.6.jar
maven-artifact-2.0.9.jar
maven-artifact-manager-2.0.6.jar
maven-artifact-manager-2.0.9.jar
maven-common-artifact-filters-1.3.jar
maven-compiler-plugin-3.1.jar
maven-core-2.0.6.jar
maven-core-2.0.9.jar
maven-error-diagnostics-2.0.6.jar
maven-error-diagnostics-2.0.9.jar
maven-filtering-1.1.jar
maven-model-2.0.6.jar
maven-model-2.0.9.jar
maven-monitor-2.0.6.jar
maven-monitor-2.0.9.jar
maven-plugin-annotations-3.1.jar
maven-plugin-api-2.0.6.jar
maven-plugin-api-2.0.9.jar
maven-plugin-descriptor-2.0.6.jar
maven-plugin-descriptor-2.0.9.jar
maven-plugin-parameter-documenter-2.0.6.jar
maven-plugin-parameter-documenter-2.0.9.jar
maven-plugin-registry-2.0.6.jar
maven-plugin-registry-2.0.9.jar
maven-profile-2.0.6.jar
maven-profile-2.0.9.jar
maven-project-2.0.6.jar
maven-project-2.0.9.jar
maven-reporting-api-2.0.6.jar
maven-reporting-api-2.0.9.jar
maven-repository-metadata-2.0.6.jar
maven-repository-metadata-2.0.9.jar
maven-resources-plugin-2.6.jar
maven-settings-2.0.6.jar
maven-settings-2.0.9.jar
maven-shared-incremental-1.1.jar
maven-shared-utils-0.1.jar
maven-surefire-common-2.12.4.jar
maven-surefire-plugin-2.12.4.jar
maven-toolchain-1.0.jar
maven-toolchain-2.0.9.jar
netty-3.2.5.Final.jar
plexus-build-api-0.0.4.jar
plexus-classworlds-2.2.2.jar
plexus-compiler-api-2.2.jar
plexus-compiler-javac-2.2.jar
plexus-compiler-manager-2.2.jar
plexus-component-annotations-1.5.5.jar
plexus-container-default-1.0-alpha-9-stable-1.jar
plexus-container-default-1.5.5.jar
plexus-interactivity-api-1.0-alpha-4.jar
plexus-interpolation-1.13.jar
plexus-utils-1.5.1.jar
plexus-utils-2.0.5.jar
plexus-utils-3.0.8.jar
spring-aop-4.3.10.RELEASE.jar
spring-beans-4.3.10.RELEASE.jar
spring-context-4.3.10.RELEASE.jar
spring-core-4.3.10.RELEASE.jar
spring-expression-4.3.10.RELEASE.jar
spring-web-4.3.10.RELEASE.jar
surefire-api-2.12.4.jar
surefire-booter-2.12.4.jar
xbean-reflect-3.4.jar
4. 开始代码
D:\workspace\dubboprj\src\com\alibaba\dubbo\demo\DemoServiceImpl.java
4.1. Define service interface
Since both service provider and service consumer rely on the same interface, it is strongly recommended to put the interface definition below in one separated module which could be shared by both provider module and consumer module.
package com.alibaba.dubbo.demo;
public interface DemoService {
String sayHello(String name);}
4.2. Implement service provider
package com.alibaba.dubbo.demo.provider;import com.alibaba.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}}
4.3. 启动服务
package com.alibaba.dubbo.demo;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ProtocolConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ProviderConfig;
import com.alibaba.dubbo.config.ServiceConfig;
/*
import com.xxx.XxxService;
import com.xxx.XxxServiceImpl;
*/
public class Provider {
public static void main(String[] args) {
// 服务实现
DemoService xxxService1 = new DemoServiceImpl();
// 当前应用配置 <!-- 消费方应用名,用于计算依赖关系,不是匹配条件,不要与提供方一样 -->
ApplicationConfig application = new ApplicationConfig();
application.setName("xxx");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
// registry.setAddress("10.20.130.230:9090");
registry.setAddress("multicast://224.5.6.7:1234");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
protocol.setThreads(200);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
ServiceConfig<DemoService> service = new ServiceConfig<DemoService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(DemoService.class);
service.setRef(xxxService1);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
//mustr receyl to hold the port ..beir exit auto..and cant conn
while (true) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("---ok");
}
}
}
4.4. 消费服务
package com.alibaba.dubbo.demo;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.ConsumerConfig;
import com.alibaba.dubbo.config.ReferenceConfig;//
//import com.xxx.XxxService;
public class Custom {
public static void main(String[] args)
{
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("yyy");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("multicast://224.5.6.7:1234");
registry.setUsername("aaa");
registry.setPassword("bbb");
// 注意:ReferenceConfig为重对象,内部封装了与注册中心的连接,以及与服务提供方的连接
// 引用远程服务
ReferenceConfig<DemoService> reference = new ReferenceConfig<DemoService>(); // 此实例很重,封装了与注册中心的连接以及与提供者的连接,请自行缓存,否则可能造成内存和连接泄漏
reference.setApplication(application);
reference.setRegistry(registry); // 多个注册中心可以用setRegistries()
reference.setInterface(DemoService.class);
reference.setVersion("1.0.0");
// 和本地bean一样使用xxxService
DemoService xxxService = reference.get(); // 注意:此代理对象内部封装了所有通讯细节,对象较重,请缓存复用
System.out.println(xxxService.sayHello("--test"));
}
5. }问题解决
5.1. 官网范例代码错误的编译错误问题,一一解决。
分离代码比较新,但是2.6.0的jar有点久,还好下载2.6.1的src插入部分config ,common部分pakg。。编译ok
还有fastjson类库需要集成将来。。不知为什么maven没有吧这个依赖down下来
范例代码启动后,需要时间不短循环保持在内存服务提供持久化,增加这部分无线循环代码。
6. 参考资料
6.1. Overview _ DUBBO.mhtml
6.2.
6.3. java-dubbo-使用Multicast注册中心提供服务 - CSDN博客.mhtml
6.4. Dubbo入门---搭建一个最简单的Demo框架 - noaman_wgs的博客 - CSDN博客.mhtml
dubbo通信协议之对比 - 自行车上的程序员 - 博客园.mhtml