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次进行验证码验证功能,东西可能比较多,这个是我这两天在网上结合各种资源整合出来的,基本功能都已经实现, ...
随机推荐
- iOS 数组问题
在iOS开发过程中,使用json抓取网络数据进行解析时,用tableview承载,发现数据已经抓取到,但是在cell里面使用却会导致程序崩溃 原因可能是初始化方法问题,将 _infoArray=[[N ...
- Openvswitch手册(3): sFlow, netFlow
这一节,我们重点看sFlow 采样流sFlow(Sampled Flow)是一种基于报文采样的网络流量监控技术,主要用于对网络流量进行统计分析. sFlow系统包含一个嵌入在设备中的sFlow Age ...
- [转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison
http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html? ...
- 用apache和tomcat搭建集群,实现负载均衡
型的企业应用每天都需要承受巨大的访问量,在着巨大访问量的背后有数台服务器支撑着,如果一台服务器崩溃了,那么其他服务器可以使企业应用继续运行,用户对服务器的运作是透明化的,如何实现这种透明化呢?由如下问 ...
- C++ 知识回顾总结 -- 指针
指针是一种特殊类型的变量,用于存储值的地址,因此,指针名表示的是地址.*运算符被成为间接值或解除引用运算符,将其应用于指针,可以得到该地址存储的值. 对指针的理解可以参照以下的例子: #include ...
- MySQL索引优化看这篇文章就够了!
阅读本文大概需要 5 分钟. 来源:cnblogs.com/songwenjie/p/9410009.html 本文主要讨论MySQL索引的部分知识.将会从MySQL索引基础.索引优化实战和数据库索引 ...
- Python面向对象5:类的常用魔术方法
魔术方法就是不需要人为调用的方法,基本是在特定的时刻自动触发- 魔术方法的统一的特征,方法名被前后各两个下滑线包裹 - 操作类 - `__init__`: 构造函数 - `__new__`: 对象实例 ...
- 吴恩达机器学习笔记27-样本和直观理解2(Examples and Intuitions II)
二元逻辑运算符(BINARY LOGICAL OPERATORS)当输入特征为布尔值(0 或1)时,我们可以用一个单一的激活层可以作为二元逻辑运算符,为了表示不同的运算符,我们只需要选择不同的权重即可 ...
- C 单向链表就地逆转
1.问题描述 给定一个单链表L,设计函数Reverse将L就地逆转.即不需要申请新的节点,将第一个节点转换为最后一个结点,第二个节点转换为倒数第二个结点,以此类推. 2.思路分析 循环处理整个链表.将 ...
- 简单聊一聊那些svg的沿路径运动
之前遇见动画就很想用css实现,显然有些效果是我们力所不能及,实现起来麻烦,效果不好,让人捉急.其实归结起来,不同的动画有自己的优势,根据实际情况进行取舍.本文就告诉大家如何用SVG写出个简单动画.就 ...