父项目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/dubbohttp://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/dubbohttp://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泛型调用演示