泛化调用,是非常了灵活的一个服务方/调用方实现方式,不需要引入协议包,可以直接构建服务端或消费端。
Dubbo的泛化调用 有详细的使用介绍。
示例
协议
package demo.contract;
import demo.contract.request.HiRequest;
import demo.contract.response.HiResponse;
public interface DemoService {
String sayHello(String name);
HiResponse sayHi(HiRequest request);
}
package demo.contract.request;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
@Data
@EqualsAndHashCode(callSuper = false)
public class HiRequest implements Serializable {
private String msg;
}
package demo.contract.response;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
@Data
@EqualsAndHashCode(callSuper = false)
public class HiResponse implements Serializable {
private String result;
}
服务端实现
package demo.provider;
import demo.contract.DemoService;
import demo.contract.request.HiRequest;
import demo.contract.response.HiResponse;
import org.springframework.stereotype.Service;
@Service
public class DemoServiceImpl implements DemoService {
@Override
public String sayHello(String name) {
return "Hello, " + name;
}
@Override
public HiResponse sayHi(HiRequest request) {
HiResponse response = new HiResponse();
response.setResult("Hi, " + request.getMsg());
return response;
}
}
dubbo-provider.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
<dubbo:application name="hello-world-app"></dubbo:application>
<dubbo:registry address="zookeeper://127.0.0.1:2181"/>
<dubbo:protocol name="dubbo" port="20881"/>
<dubbo:service interface="demo.contract.DemoService"
class="demo.provider.DemoServiceImpl"
/>
</beans>
基于泛化调用实现的消费端
示例1:
package demo.consumer;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
public class ConsumerMainV1 {
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig();
application.setName("api-generic-consumer");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
application.setRegistry(registry);
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
// 弱类型接口名
reference.setInterface("demo.contract.DemoService");
// 声明为泛化接口
reference.setGeneric(true);
reference.setApplication(application);
// 用com.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService genericService = reference.get();
Object name = genericService.$invoke("sayHello", new String[]{String.class.getName()}, new Object[]{ "世界" });
System.out.println(name.getClass().getName());
System.out.println(name);
}
}
运行结果:
java.lang.String
Hello, 世界
示例2:
package demo.consumer;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
import java.util.HashMap;
import java.util.Map;
public class ConsumerMainV2 {
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig();
application.setName("api-generic-consumer");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
application.setRegistry(registry);
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
// 弱类型接口名
reference.setInterface("demo.contract.DemoService");
// 声明为泛化接口
reference.setGeneric(true);
reference.setApplication(application);
// 用com.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService genericService = reference.get();
Map<String, Object> param = new HashMap<>();
param.put("msg", "World!");
Object name = genericService.$invoke(
"sayHi",
new String[]{"demo.contract.request.HiRequest"},
new Object[]{ param }
);
System.out.println(name.getClass().getName());
System.out.println(name);
}
}
运行结果:
java.util.HashMap
{result=Hi, World!, class=demo.contract.response.HiResponse}
示例3:
package demo.consumer;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.rpc.service.GenericService;
import java.util.HashMap;
import java.util.Map;
public class ConsumerMainV3 {
public static void main(String[] args) {
ApplicationConfig application = new ApplicationConfig();
application.setName("api-generic-consumer");
ReferenceConfig<GenericService> reference = new ReferenceConfig<>();
// 弱类型接口名
reference.setInterface("demo.contract.DemoService");
// 服务直连,不经过 zookeeper
reference.setUrl("dubbo://127.0.0.1:20881");
// 声明为泛化接口
reference.setGeneric(true);
reference.setApplication(application);
// 用com.apache.dubbo.rpc.service.GenericService可以替代所有接口引用
GenericService genericService = reference.get();
Map<String, Object> param = new HashMap<>();
param.put("msg", "World!");
Object name = genericService.$invoke(
"sayHi",
new String[]{"demo.contract.request.HiRequest"},
new Object[]{ param }
);
System.out.println(name.getClass().getName());
System.out.println(name);
}
}
运行结果:
java.util.HashMap
{result=Hi, World!, class=demo.contract.response.HiResponse}