Dubbo使用java api 配置开发禁止使用config center
Posted On 2021年3月13日
昨日,未了验证一个测试脚本,需要使用api configuration模式直接实现consumer功能。 发现Dubbo使用java api 配置开发无法禁止使用config center。
根据官方文档https://dubbo.apache.org/en/docs/v2.7/user/configuration/, 配置dubbo的方式有如下4种。 因为需要用代码快速的实现,直接采用API C哦南方iguration的方式。
- XML Configuration – Configure Dubbo with XML
- Properties Configuration – Configure Dubbo with properties
- API Configuration – Configure Dubbo with API
- Annotation Configuration – Configure Dubbo with annotation
如下代码为dubbo-provider 使用api confgiratuion实现的代码。 根据官方文档,未指定config center地址时,默认使用 注册中心地址 作为 config center(配置中心)。 因为第一次部署好zookeeper后,未初始化配置中心的dubbo的配置。所以provider(和consumer同样问题)启动加载会尝试读取配置。 此时需要将显示的设置注册中心,并设置为不检查。 configCenter.setCheck(false);
package com.hissummer.dubbotest;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ConfigCenterConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;
public class DubboService {
public static void main(String[] args) {
// 服务实现
HelloServiceImpl helloservice = new HelloServiceImpl();
// 当前应用配置
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-provider");
// 连接注册中心配置
RegistryConfig registry = new RegistryConfig();
registry.setAddress("zookeeper://127.0.0.1:2181");
//registry.setCheck(false);
//registry.setSimplified(true);
// 配置中心,若无指定的时候,默认使用注册中心的地址作为配置中心!!!
// ConfigCenterConfig configCenter = new ConfigCenterConfig();
// configCenter.setAddress("zookeeper://127.0.0.1:2181");
// configCenter.setCheck(false);
//registry.setRegister(false);
// registry.setUsername("aaa");
// registry.setPassword("bbb");
// 服务提供者协议配置
ProtocolConfig protocol = new ProtocolConfig();
protocol.setName("dubbo");
protocol.setPort(12345);
// 注意:ServiceConfig为重对象,内部封装了与注册中心的连接,以及开启服务端口
// 服务提供者暴露服务配置
ServiceConfig<HelloService> service = new ServiceConfig<HelloService>(); // 此实例很重,封装了与注册中心的连接,请自行缓存,否则可能造成内存和连接泄漏
service.setApplication(application);
//service.setConfigCenter(configCenter);
service.setRegistry(registry); // 多个注册中心可以用setRegistries()
service.setProtocol(protocol); // 多个协议可以用setProtocols()
service.setInterface(HelloService.class);
service.setRef(helloservice);
service.setVersion("1.0.0");
// 暴露及注册服务
service.export();
while(true)
{
try {
Thread.sleep(60000L);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}如下时dubbo consumer的代码
package com.hissummer.dubbotest;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;
public class TestScript {
public String test() {
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-consumer");
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setAddress("zookeeper://127.0.0.1:2181");
ReferenceConfig<HelloService> reference = new ReferenceConfig<HelloService>();
reference.setApplication(application);
reference.setProtocol("dubbo");
reference.setVersion("1.0.0");
reference.setRegistry(registryConfig);
reference.setInterface(HelloService.class);
//reference.setUrl("dubbo://localhost:12345/shuaicj.example.dubbo.common.HelloService:1.0.0");
HelloService greetingsService = reference.get();
Hello hiMessage = greetingsService.hello("www.hissummer.com");
return hiMessage.toString();
}
public static void main(String[] args)
{
System.out.println(new TestScript().test());
}
}
启动dubbo-provider后,然后执行dubbo-consumer, 打印出
Hello{id=0, message='www.hissummer.com'}如果需要点对点直连,跳过注册中心
reference.setUrl("dubbo://localhost:12345/com.hissummer.dubbotest.HelloService:1.0.0");此篇文章已被阅读1718 次