dubbo+zookeeper+springBoot框架整合与dubbo泛型调用演示
dubbo + zookeeper + spring Boot框架整合与dubbo泛型调用演示
By:客 授客 QQ:1033553122 欢迎加入全国软件测试交流 QQ 群:7156436
测试环境 1
实践过程 2
Java运行配置 2
zookeeper运行与配置 2
配置 2
运行 3
测试 3
Maven运行与配置 5
配置 5
Repository设置(可选) 6
利用maven打dubbo-admin-0.0.1-SNAPSHOT.jar包 8
dubbo-admin控制台运行与配置 9
配置(可选) 9
运行 9
编码 11
Maven mirrors配置 11
Idea maven运行环境配置 12
新建项目 14
新建父项目 14
新建模块 19
dubbo-demo-api模块编码 23
dubbo-demo-provider模块编码与配置 23
dubbo-dubbo-consumer模块编码 28
参考链接 32
Dubbo特性-泛型调用 32
依赖spring配置文件的泛型调用 32
不依赖Spring配置文件的泛型调用 33
泛型调用-传递更复杂的参数类型 34
参考链接 39
测试环境
服务端
CentOS 7操作系统(CentOS-7-x86_64-DVD-1503-01.iso)
下载地址:http://ftp.riken.jp/Linux/centos/7/isos/x86_64/
zookeeper-3.4.13.tar.gz
下载地址:
http://zookeeper.apache.org/releases.html
http://mirror.bit.edu.cn/apache/zookeeper/
https://pan.baidu.com/s/1Ug4hQ6PVQJmIhghyX4UIkQ
Java(jdk-8u65-linux-x64.tar.gz)
下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html
incubator-dubbo-ops-master.zip
下载地址:
https://github.com/apache/incubator-dubbo-ops
https://pan.baidu.com/s/10Qu9IQa9RCmUyeSQLFpSkQ
客户端
Win7
apache-maven-3.5.4-bin.zip
https://maven.apache.org/download.cgi
https://pan.baidu.com/s/1OUNC0kZNduXJJLbpw76GZA
ideaIU-2018.2.1.exe
实践过程
Java运行配置
略
zookeeper运行与配置
配置
注:以下为单例模式运行下的配置
# tar xvzf zookeeper-3.4.13.tar.gz
# mkdir -p /var/lib/zookeeper
# cd zookeeper-3.4.13/
// 手工创建zoo.cfg配置
# cd conf/
# vim zoo.cfg //内容如下
tickTime=2000
dataDir=/var/lib/zookeeper
clientPort=2181
说明:
dataDir
tickTime 供Zookeeper使用的基本时间单位(单位毫秒),用于执行心跳,同时最小会话超时时间将会是tickTime的两倍。
dataDir 必须指向一个已存在目录,用于存在内存数据库快照,除非特别指明,否则为更新至数据库的事务日志(the location to store the in-memory database snapshots and, unless specified otherwise, the transaction log of updates to the database.)
clientPort 用于监听客户端连接的端口。
运行
# cd ../bin
# pwd
/usr/local/zookeeper/zookeeper-3.4.13/bin
# ls
README.txt zkCleanup.sh zkCli.cmd zkCli.sh zkEnv.cmd zkEnv.sh zkServer.cmd zkServer.sh zkTxnLogToolkit.cmd zkTxnLogToolkit.sh
# zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
注:停止运行 zkServer.sh stop
测试
连接到ZooKeeper
# zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2017-09-15 20:48:06,986 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT
……(略)
Welcome to ZooKeeper!
2017-09-15 20:48:07,153 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1029] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
……(略)
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
[zk: 127.0.0.1:2181(CONNECTED) 1] ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 2] create /zk_test my_data
Created /zk_test
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /
[zookeeper, zk_test]
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk_test
my_data
cZxid = 0x2
ctime = Fri Sep 15 20:52:32 CST 2017
mZxid = 0x2
mtime = Fri Sep 15 20:52:32 CST 2017
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 7
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 5] set /zk_test junk
cZxid = 0x2
ctime = Fri Sep 15 20:52:32 CST 2017
mZxid = 0x3
mtime = Fri Sep 15 20:54:39 CST 2017
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 6] get /zk_test
junk
cZxid = 0x2
ctime = Fri Sep 15 20:52:32 CST 2017
mZxid = 0x3
mtime = Fri Sep 15 20:54:39 CST 2017
pZxid = 0x2
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 4
numChildren = 0
[zk: 127.0.0.1:2181(CONNECTED) 7] delete /zk_test
参考链接:
http://zookeeper.apache.org/doc/current/zookeeperStarted.html
http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_systemReq
http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_configuration
Maven运行与配置
确保安装了java jdk并正确设置了JAVA_HOME
配置
下载bin.zip压缩包,解压到目标路径(例中 D:\Program Files\apache-maven-3.5.4\
),设置MAVEN_HOME环境变量,如下
Cmd输入mvn -v测试
Repository设置(可选)
修改%MAVEN_HOME%\conf\setting.xml文件
找到以下内容,修改<localRepository>元素内容
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:/Program Files/apache-maven-3.5.4/repo</localRepository>
说明:
localRepository元素节点用于配置本地仓库,默认是被注释掉的(默认值为C:\Users\用户名.m2),例中未去掉注释,直接新增了一个节点值,并设置元素值为指定目录
3. 本地仓库起到了一个缓存的作用,当我们从maven中获取jar包的时候,maven首先会在本地仓库中查找,如果本地仓库有则返回;如果没有则从远程仓库中获取包,并在本地库中保存。
此外,我们在maven项目中运行mvn install,项目将会自动打包并安装到本地仓库中
配置测试,cmd命令行输入以下命令
mvn help:system
等待命令成功执行,可见如下输出,并在配置的本地仓库下生成一携带n多文件的org目录
参考链接
https://maven.apache.org/install.html
利用maven打dubbo-admin-0.0.1-SNAPSHOT.jar包
解压incubator-dubbo-master.zip至目标路径(例中为 F:\DownLoads\incubator-dubbo-ops-master)
cmd进入目标目录并执行maven编译命令
mvn install -Dmaven.test.skip=true
如下图,执行完毕后会看到如下输出
说明:如上图,maven编译打包成功后,会自动复制jar包及其它一些必备文件到本地仓库。
如上说明,我们可以直接进入到目标目录下对应工程的target目录下,获取我们需要的jar包:dubbo-admin-0.0.1-SNAPSHOT.jar (例中为:
F:\DownLoads\incubator-dubbo-ops-master\dubbo-admin\target\dubbo-admin-0.0.1-SNAPSHOT.jar)
附下载地址:
https://pan.baidu.com/s/1dm1ftVonqb_8gK4DNwn_gw
dubbo-admin控制台运行与配置
配置(可选)
修改jar包application.properties(路径 dubbo-admin-0.0.1-SNAPSHOT.jar\BOOT-INF\classes\application.properties),更改zookeeper地址,例中默认如下
dubbo.registry.address=zookeeper://127.0.0.1:2181
运行
上传dubbo-admin-0.0.1-SNAPSHOT.jar至linux服务器,执行以下命令
# java -jar dubbo-admin-0.0.1-SNAPSHOT.jar
如下,执行成功后会显示图示命令
防火墙开放端口
# firewall-cmd --permanent --zone=public --add-port=7001/tcp
# firewall-cmd --reload
验证,浏览器打开服务器地址(例中为 192.168.31.192:7001),如下
输入账号\密码:root\root
参考链接
http://dubbo.apache.org/en-us/docs/admin/install/admin-console.html
编码
Maven mirrors配置
如下设置
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>alimaven</id>
<mirrorOf>central</mirrorOf>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
</mirror>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>central</id>
<name>Maven Repository Switchboard</name>
<url>http://repo1.maven.org/maven2/</url>
<mirrorOf>central</mirrorOf>
</mirror>
<mirror>
<id>repo2</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://repo2.maven.org/maven2/</url>
</mirror>
<mirror>
<id>ibiblio</id>
<mirrorOf>central</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://mirrors.ibiblio.org/pub/mirrors/maven2/</url>
</mirror>
<mirror>
<id>jboss-public-repository-group</id>
<mirrorOf>central</mirrorOf>
<name>JBoss Public Repository Group</name>
<url>http://repository.jboss.org/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>google-maven-central</id>
<name>Google Maven Central</name>
<url>https://maven-central.storage.googleapis.com
</url>
<mirrorOf>central</mirrorOf>
</mirror>
<!-- 中央仓库在中国的镜像 -->
<mirror>
<id>maven.net.cn</id>
<name>oneof the central mirrors in china</name>
<url>http://maven.net.cn/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
Idea maven运行环境配置
可通过如下两种方式打开Settings界面
如下,设置Maven home directory,User Setting file, Local repository
设置Maven JVM参数
-Xms128m -Xmx512m -Duser.language=zh -Dfile.encoding=UTF-8
如上图,这一步也可以不执行,通过其它方式代替(注:方案未经过验证)
替代方案1:新建 系统环境变量,
变量名:MAVEN_OPTS
变量值:-Xms128m -Xmx512m -Duser.language=zh -Dfile.encoding=UTF-8
替代方案2:修改maven启动文件%maven_home%\bin\mvn.cmd(Linux mvn
添加MAVEN_OPTS变量
新建项目
新建父项目
File -> New -> Project -> Maven, 选择Project SDK,不勾选 Create from archetype,点击Next
如下,填写GroupId,ArtifactId,version,点击Next
如下,填写Project name, Project location,点击Finish
初始代码结构如下
修改上述pom.xml
<?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>org.apache.dubbo</groupId>
<artifactId>dubboDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<!--设置打包类型为pom,目的是为了实现多模块项目-->
<packaging>pom</packaging>
</project>
新建模块
新建以下三个模块
dubbo-demo-api: 公共服务api
dubbo-demo-provider: 服务提供者
dubbo-demo-consumer: 消费者
右键父项目 -> New -> Module,选择JDK,Next
如下,填写ArtifactId, Next
如下,填写 Module name,其它默认,Next
参考以上,再新建两个模块,模块名称及artifactId分别为dubbo-demo-provider,dubbo-demo-consumer
建好模块后,代码结构如下
父项目pom.xml如下
<?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>org.apache.dubbo</groupId>
<artifactId>dubboDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>dubbo-demo-api</module>
<module>dubbo-demo-provider</module>
<module>dubbo-demo-consumer</module>
</modules>
<!--设置打包类型为pom,目的是为了实现多模块项目-->
<packaging>pom</packaging>
</project>
dubbo-demo-api模块编码
如下,在java源码根目录下新建层级Package,然后右键demo包-> New -> Java Class,新建DemoService.java源文件,代码如下
package org.apache.dubbo.demo;
public interface DemoService {
String sayHello(String name);
}
dubbo-demo-provider模块编码与配置
新建DemoServiceImpl.java源文件,代码内容如下
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
}
新建Provider.java源文件
package org.apache.dubbo.demo.provider;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Provider {
/**
* To get ipv6 address to work, add
* System.setProperty("java.net.preferIPv6Addresses", "true");
* before running your application.
*/
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"META-INF/spring/provider.xml"});
context.start();
System.in.read(); // press any key to exit
}
}
新建provider.xml文件及存放目录,文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 提供方应用名称,用于跟踪依赖关系(provider's application name, used for tracing dependency relationship -->
<dubbo:application name="demo-provider"/>
<!-- 使用multicast广播注册中心暴露服务地址(use multicast registry center to export service -->
<dubbo:registry address="zookeeper://192.168.31.192:2181"/>
<!-- 用dubbo协议在20880端口暴露服务(use dubbo protocol to export service on port 20880 -->
<dubbo:protocol name="dubbo" port="20880"/>
<!-- 和本地bean一样实现服务(service implementation, as same as regular local bean -->
<bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>
<!-- 声明需要暴露的服务接口(declare the service interface to be exported -->
<dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService"/>
</beans>
注意:这里class和interface均需要携带包名,否则可能会提示类似以下错误
java.lang.ClassNotFoundException: DemoServiceImpl
如下,实践过程发现,provider.xml会提示以下错误:
URI is not registered (Settings | Languages & Frameworks | Schemas and DTDs)
解决方法:
File -> Settings -> Languages & Frameworks -> Schemas and DTDS,点击右侧+号,添加URL http://dubbo.apache/schema/dubbo
添加后效果如下
修改模块的pom.xml文件,增加依赖(以下加粗倾斜部分)
<?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>
<artifactId>dubboDemo</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-provider</artifactId>
<dependencies>
<dependency>
<artifactId>dubbo-demo-api</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
配置日志系统,新建log4j.propertities,内容如下
###set log levels###
log4j.rootLogger=info, stdout
###output to the console###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[%d{dd/MM/yy hh:mm:ss:sss z}] %t %5p %c{2}: %m%n
至此,项目代码结构如下
运行Provider
浏览器访问,查看服务
dubbo-dubbo-consumer模块编码
新建Consumer.java源文件及对应包
package org.apache.dubbo.demo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.dubbo.demo.DemoService;
import com.alibaba.dubbo.rpc.service.GenericService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/consumer.xml"});
context.start();
// Obtaining a remote service proxy
DemoService demoService = (DemoService)context.getBean("demoService");
// Executing remote methods
String hello = demoService.sayHello("world");
// Display the call result
System.out.println(hello);
}
}
修改模块的pom.xml文件,增加依赖(以下加粗倾斜部分)
<?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>
<artifactId>dubboDemo</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>dubbo-demo-consumer</artifactId>
<dependencies>
<dependency>
<artifactId>dubbo-demo-api</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
新增consumer.xml配置文件及存放目录,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
<!-- 消费者应用名称,用于跟踪依赖关系(不是匹配条件,不要和提供方应用名称一样 consumer's application name, used for tracing dependency relationship (not a matching criterion),
don't set it same as provider -->
<dubbo:application name="demo-consumer"/>
<!-- 使用multicast广播注册中心发现服务地址use multicast registry center to discover service -->
<!--<dubbo:registry address="multicast://224.5.6.7:1234"/>-->
<dubbo:registry address="zookeeper://192.168.31.192:2181"/>
<!-- 生成远程服务代理,这样便可以和本地bean一样使用demoService(generate proxy for the remote service, then demoService can be used in the same way as the
local regular interface -->
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService"/>
</beans>
新增log4j.propertities文件,内容同上
项目模块代码结构如下:
运行Consumer
浏览器访问http://192.168.31.192:7001/governance/consumers,查看消费者,结果看不到消费者
替换以下代码
String hello = demoService.sayHello("world");
// Display the call result
System.out.println(hello);
为下方代码
while (true) {
String hello = demoService.sayHello("world");
System.out.println(hello);
}
再次查看,结果如下
参考链接
http://dubbo.apache.org/en-us/docs/user/quick-start.html
Dubbo特性-泛型调用
依赖spring配置文件的泛型调用
修改consumer.xml配置文件,找到以下内容行,新增generic="true",表示该接口支持泛型调用
<dubbo:reference id="demoService" check="false" interface="org.apache.dubbo.demo.DemoService" generic="true"/>
修改Consumer.java如下
package org.apache.dubbo.demo.consumer;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.alibaba.dubbo.rpc.service.GenericService;
public class Consumer {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"META-INF/spring/consumer.xml"});
context.start();
// Spring 泛化调用
GenericService demoService = (GenericService) context.getBean("demoService");
Object result = demoService.$invoke("sayHello", new String[] {"java.lang.String"} , new Object[] {"world"});
System.out.println(result);
}
}
说明:
$invoke方法有三个参数,第一个参数是调用的远程接口的具体方法名称(例中为sayHello),第二个参数是所调用方法的入参类型,第三个是参数值。
不依赖Spring配置文件的泛型调用
修改Consumer.java文件
package org.apache.dubbo.demo.consumer;
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.alibaba.dubbo.rpc.service.GenericService;
public class Consumer {
public static void main(String[] args) {
//设置消费者应用名称
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-consumer");
//设置连接注册中心地址(zookeeper访问地址)
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://192.168.31.192:2181");
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface("org.apache.dubbo.demo.DemoService");
reference.setGeneric(true); // 声明为泛化接口
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
GenericService genericService = cache.get(reference);
Object result = genericService.$invoke("sayHello", new String[] {"java.lang.String"}, new Object[]{"world"});
System.out.println(result);
}
}
泛型调用-传递更复杂的参数类型
在dubbo-demo-api模块下新增dto Package,并在该包下新增QueryUserInfoReq.java源文件,内容如下
package org.apache.dubbo.demo.dto;
import java.io.Serializable;
import java.util.List;
public class QueryUserInfoReq implements Serializable
{
private static final long serialVersionUID = 1L;
private String name;
private String age;
private List<String> hobbyList;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return this.age;
}
public void setAge(String age) {
this.age = age;
}
public List<String> getHobbyList() {
return this.hobbyList;
}
public void setHobbyList(List<String> hobbyList) {
this.hobbyList = hobbyList;
}
public String toString()
{
return "QueryUserInfoReq{name='" + this.name + '\'' + ", age='" + this.age + '\'' + ", hobbyList=" + this.hobbyList + '}';
}
}
修改dubbo-demo-api模块下的DemoService.java文件,增加QueryUserInfo方法
package org.apache.dubbo.demo;
import org.apache.dubbo.demo.dto.QueryUserInfoReq;
public interface DemoService {
String sayHello(String name);
String QueryUserInfo(QueryUserInfoReq userInfo);
}
修改dubbo-demo-provider模块下DemoServiceImpl.java文件,增加QueryUserInfo方法实现
package org.apache.dubbo.demo.provider;
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.demo.dto.QueryUserInfoReq;
public class DemoServiceImpl implements DemoService {
public String sayHello(String name) {
return "Hello " + name;
}
public String QueryUserInfo(QueryUserInfoReq userInfo){
return userInfo.toString();
}
}
这里为了增加对dubbo认识和理解,单独为消费者新建了一个不带架构模板的maven项目,项目代码结构如下
Consumber.java如下
import com.alibaba.dubbo.config.ApplicationConfig;
import com.alibaba.dubbo.config.ReferenceConfig;
import com.alibaba.dubbo.config.RegistryConfig;
import com.alibaba.dubbo.config.utils.ReferenceConfigCache;
import com.alibaba.dubbo.rpc.service.GenericService;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
public class Consumer {
public static void main(String[] args) {
//设置消费者应用名称
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-consumer");
//设置连接注册中心地址
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://192.168.31.192:2181");
ReferenceConfig<GenericService> reference = new ReferenceConfig<GenericService>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface("org.apache.dubbo.demo.DemoService");
reference.setTimeout(5000); // 设置超时间为5秒
reference.setRetries(1); // 设置重试次数
reference.setGeneric(true); // 声明为泛化接口
ReferenceConfigCache cache = ReferenceConfigCache.getCache();
GenericService genericService = cache.get(reference);
Map<String,Object> map = new HashMap<String,Object>();
map.put("name","shouke");
List<String> l = new ArrayList<String>();
l.add("pingpong");
l.add("basketball");
map.put("hobbyList",l);
map.put("age","99");
Object result = genericService.$invoke("QueryUserInfo", new String[] {"org.apache.dubbo.demo.dto.QueryUserInfoReq"},new Object[]{map});
System.out.println(result);
}
}
注意:参数类型需要把其所在包也写上,否则会提示找不到类
java.lang.ClassNotFoundException: QueryUserInfoReq
pom.xml内容如下
<?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>org.apache.dubbo</groupId>
<artifactId>dubbo-consumer</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<dubbo.version>2.6.2</dubbo.version>
<zkclient.version>0.10</zkclient.version>
<curator-client.version>2.8.0</curator-client.version>
<curator-framework.version>2.8.0</curator-framework.version>
</properties>
<dependencies>
<!-- dubbo 依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo.version}</version>
</dependency>
<!-- zookeeper 客户端依赖 -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>${zkclient.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-client</artifactId>
<version>${curator-client.version}</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>${curator-framework.version}</version>
</dependency>
</dependencies>
</project>
运行Consumer
参考链接
https://www.cnblogs.com/flyingeagle/p/8908317.html
点击以下链接查看详情(压缩文件包含工程代码):
dubbo +
zookeeper +
spring Boot框架整合与dubbo泛型调用演示
dubbo+zookeeper+springBoot框架整合与dubbo泛型调用演示的更多相关文章
- dubbo+zookeeper+springboot简单示例
目录 dubbo+zookeeper+springboot简单示例 zookeeper安装使用 api子模块 生产者producer 消费者consumer @(目录) dubbo+zookeeper ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(二)
上一篇文章是关于搭建SpringBoot+dubbo+zookeeper+maven框架的,但是里面的功能还不够完善,今天就日志管理方面做一些改善. 下了demo的网友可能会发现项目在启动时会有警告: ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(一)
这几天项目还没来,所以就自己试着参考网上的一些资料,搭建了一个SpringBoot+dubbo+zookeeper+maven框架,网上参考的很多资料照着他们一步一步搭建,最后很多都运行不通,很是郁闷 ...
- SpringBoot 框架整合
代码地址如下:http://www.demodashi.com/demo/12522.html 一.主要思路 使用spring-boot-starter-jdbc集成Mybatis框架 通过sprin ...
- Dubbo+Zookeeper+SpringMVC+Maven整合实现微服务项目
互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ...
- dubbo+zookeeper+springboot构建服务
本次和大家分享的是dubbo框架应用的初略配置和zookeeper注册中心的使用:说到注册中心现在我使用过的只有两种:zookeeper和Eureka,zk我结合dubbo来使用,而Eureka结合s ...
- 160719、Spring + Dubbo + zookeeper (linux) 框架搭建
转载一篇博客,写得不错(至少我参考一下搭建成功了) 转载地址:http://my.oschina.net/wangt10/blog/522799 dubbo简介 节点角色说明: Provider: 暴 ...
- 【Dubbo&&Zookeeper】6、 给dubbo接口添加白名单——dubbo Filter的使用
在开发中,有时候需要限制访问的权限,白名单就是一种方法.对于Java Web应用,Spring的拦截器可以拦截Web接口的调用:而对于dubbo接口,Spring的拦截器就不管用了. dubbo提供了 ...
- 搭建SpringBoot+dubbo+zookeeper+maven框架(四)
今天我们完成框架的thymeleaf模板显示页面功能,页面的用户登陆,密码的AES加密解密,输错3次进行验证码验证功能,东西可能比较多,这个是我这两天在网上结合各种资源整合出来的,基本功能都已经实现, ...
随机推荐
- 微软SSAS 错误 Internal error: Invalid enumeration value. Please call customer support! 不是此元素的有效值。
错误 118 Internal error: Invalid enumeration value. Please call customer support! 不是此元素的有效值. 错误 119 分析 ...
- UC手机浏览器js加入收藏夹
概述 对于某些网站来说,让用户一键把网页加入收藏夹的设计是非常棒的,它能提醒用户把网页加入收藏夹,从而增加用户的回访率,使网站获得更多的流量. 在PC端,只有ie和ff支持用js把网页加入收藏夹的操作 ...
- Apache Lucene全局搜索引擎入门教程
Lucene简介 Lucent:Apache软件基金会Jakarta项目组的一个子项目,Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟 ...
- Jexus使用的相关记录
前言 本文是零零散散的记录,部分内容是我在平时工作中用到的,部分是从群里"偷"来的,所以难免会有一些错误. 主要还是希望能帮到部分使用Jexus的朋友. 安装 curl https ...
- mysql 开发基础系列16 视图
一. 什么是视图视图是一种虚拟存在的表,行和列数据来自,定义视图的查询中使用的表,并且是在使用视图时动态生成的.优势有: 简单: 使用视图的用户完全不需要关心后面对应的表的结构,关联条件,筛选条件. ...
- selenium python 一些操作和定位收集
(—)滚动条操作 python中selenium操作下拉滚动条方法汇总 selenium_webdriver(python)控制浏览器滚动条 selenium+Python(select定位) Sel ...
- Chapter 4 Invitations——10
"Mr. Cullen?" the teacher called, seeking the answer to a question that I hadn't heard. “C ...
- ABP框架是怎么一回事呢?
ABP(ASP.NET Boilerplate['bɔɪlɚplet]:样板文件),就是一套基于.Net开源框架,官方地址为:https://aspnetboilerplate.com/ , 在这个地 ...
- .Net Core实现记录接口执行时间的中间件
项目中有时接口访问时间过长,但是通过浏览器F12查看时,接口访问时间很正常,所以就很奇怪,于是写一个中间件,记录所有接口访问时间的中间件. 一.中间件 中间件是应用程序处理管道中的组件,用来处理请求和 ...
- Nacos系列:Nacos的三种部署模式
三种部署模式 Nacos支持三种部署模式 1.单机模式:可用于测试和单机使用,生产环境切忌使用单机模式(满足不了高可用) 2.集群模式:可用于生产环境,确保高可用 3.多集群模式:可用于多数据中心场景 ...