发布时间:2023-03-09 19:30
项目上要为消费者写个生产者模拟器,用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 reference = new ReferenceConfig();
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)等价于
ITdCtsService userService = new ITdCtsServiceImpl();
// (4.3.1-2)等价于
ApplicationConfig application = getApplicationConfig();
// (4.3.1-3)等价于
RegistryConfig registry = getRegistryConfig();
// (4.3.1-4)等价于
ProtocolConfig protocol = getProtocolConfig();
// 4.3.1-5)等价于
MonitorConfig monitorConfig = getMonitorConfig();
// 4.3.1-6)等价于
// 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
ServiceConfig service = new ServiceConfig();
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;
}
}