Dubbo之API配置
程序员文章站
2022-07-03 15:14:48
...
Dubbo之API配置
Dubbo之API配置
说明
项目上要为消费者写个生产者模拟器,用xml配置好了生产者,调试了很久,才想到消费者是api直接配置的,是不是生产者也必须api直接调用,写完测试代码才调通接口,所以记录下代码以便参考。
消费者代码示例
public class TongdunTest2 {
public static void main(String[] args) {
try {
ITdCtsService service = getTdCtsService();
TongdunReq req = new TongdunReq();
req.setAccountMobile("111");
service.callService(req);
service.getLocalData("111111");
} catch (Exception e) {
e.printStackTrace();
}
}
public static ITdCtsService getTdCtsService() {
ITdCtsService tdCtsService = null;
ApplicationConfig application = getApplication();
RegistryConfig registry = getRegistry();
ReferenceConfig<ITdCtsService> reference = new ReferenceConfig<ITdCtsService>();
reference.setApplication(application);
reference.setRegistry(registry);
reference.setInterface(ITdCtsService.class);
reference.setVersion("*");
reference.setProtocol("dubbo");
reference.setGroup("dubbo");
tdCtsService = reference.get();
return tdCtsService;
}
public static ApplicationConfig getApplication() {
ApplicationConfig application = new ApplicationConfig();
application.setName("cash");
return application;
}
public static RegistryConfig getRegistry() {
RegistryConfig registry = null;
if (registry == null) {
registry = new RegistryConfig();
registry.setAddress("127.0.0.1:2181");
registry.setProtocol("zookeeper");
registry.setGroup("dubbo");
registry.setCheck(false);
}
return registry;
}
}
生产者代码示例
class TdCtsRunnable implements Runnable {
@Override
public void run() {
try {
TestProviderApi2.providerTdCtsService();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class TestProviderApi2 {
public static void main(String[] arg) {
try {
Runnable tdCtsRunnable = new TdCtsRunnable(); // 创建一个Runnable实现类的对象
// target创建新的线程
Thread thread3 = new Thread(tdCtsRunnable);
thread3.start();
System.out.println("启动线程结束");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void providerTdCtsService() throws InterruptedException {
// (4.3.1-1)等价于 <bean id="userService" class="com.test.UserServiceImpl"
// />
ITdCtsService userService = new ITdCtsServiceImpl();
// (4.3.1-2)等价于 <dubbo:application name="dubboProvider" />
ApplicationConfig application = getApplicationConfig();
// (4.3.1-3)等价于 <dubbo:registry address="zookeeper://127.0.0.1:2181" />
RegistryConfig registry = getRegistryConfig();
// (4.3.1-4)等价于 <dubbo:protocol name="dubbo" port="20880" />
ProtocolConfig protocol = getProtocolConfig();
// 4.3.1-5)等价于 <dubbo:monitor protocol="registry" />
MonitorConfig monitorConfig = getMonitorConfig();
// 4.3.1-6)等价于 <dubbo:service interface="com.test.UserServiceBo"
// ref="userService"
// group="dubbo" version="1.0.0" timeout="3000"/>
// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
ServiceConfig<ITdCtsService> service = new ServiceConfig<ITdCtsService>();
service.setApplication(application);
service.setMonitor(monitorConfig);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(ITdCtsService.class);
service.setRef(userService);
service.setVersion("*");
service.setGroup("dubbo");
service.setTimeout(3000);
service.export();
// 4.3.1-8) 挂起当前线程
Thread.currentThread().join();
}
public static RegistryConfig getRegistryConfig() {
RegistryConfig registry = new RegistryConfig();
registry.setAddress("127.0.0.1:2181");
registry.setProtocol("zookeeper");
return registry;
}
public static ProtocolConfig getProtocolConfig() {
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(20880);
return protocol;
}
public static ApplicationConfig getApplicationConfig() {
ApplicationConfig application = new ApplicationConfig();
application.setName("cash");
return application;
}
public static MonitorConfig getMonitorConfig() {
MonitorConfig monitorConfig = new MonitorConfig();
monitorConfig.setProtocol("dubbo");
return monitorConfig;
}
}
上一篇: 【未解决问题】封装slf4j在spring中无法输出正确的调用类
下一篇: Dubbo简介