服务端代码:
public class DubboMulticastServer {
public static void main(String[] args) throws IOException {
//应用配置
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-server");
//配置中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("multicast");
registryConfig.setAddress("224.5.6.7:1234");
//端口
ProtocolConfig protocolConfig = new ProtocolConfig();
protocolConfig.setName("dubbo");
protocolConfig.setPort(-1);
ServiceConfig serviceConfig = new ServiceConfig();
serviceConfig.setApplication(applicationConfig);
serviceConfig.setRegistry(registryConfig);
serviceConfig.setProtocol(protocolConfig);
serviceConfig.setInterface(UserService.class);
//serviceConfig.setInterface("com.test.client.UserService");
serviceConfig.setRef(new UserServiceImpl());
serviceConfig.export();
System.out.println("multicast:服务已暴露");
System.in.read(new byte[1024]);
}
}
消费端代码:
public class DubboMulticastClient {
public static void main(String[] args) throws IOException {
ApplicationConfig applicationConfig = new ApplicationConfig();
applicationConfig.setName("user-client");
//指定注册中心
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("multicast");
registryConfig.setAddress("224.5.6.7:1234");
ReferenceConfig referenceConfig = new ReferenceConfig();
referenceConfig.setApplication(applicationConfig);
referenceConfig.setRegistry(registryConfig);
referenceConfig.setInterface(UserService.class);
UserService userService = (UserService) referenceConfig.get();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
while (true) {
if (bufferedReader.readLine().equals("quit")) {
break;
}
System.out.println(userService.getUser(1));
}
}
}
服务端启动日志信息
信息: [DUBBO] Register dubbo service com.test.client.UserService url dubbo://192.168.182.1:20880/com.test.client.UserService?anyhost=true&application=user-server&bind.ip=192.168.182.1&bind.port=20880&deprecated=false&dubbo=2.0.2&dynamic=true&generic=false&interface=com.test.client.UserService&methods=getUser&pid=11580&release=2.7.4.1&side=provider×tamp=1586686322809 to registry registry://224.5.6.7:1234/org.apache.dubbo.registry.RegistryService?application=user-server&dubbo=2.0.2&pid=11580®istry=multicast&release=2.7.4.1×tamp=1586686322768, dubbo version: 2.7.4.1, current host: 192.168.182.1
四月 12, 2