请问nacosRegistration没有getNacosNamingService方法了,2021.0.1.0版本怎么拿到NacosNamingService呢?
配置类中设计到的几个bean:NacosServiceRegistry 、NacosRegistration 、NacosAutoServiceRegistration 、AutoServiceRegistrationConfiguration
NacosServiceRegistry : 实现服务的注册逻辑
NacosRegistration : 通过 NacosRegistrationCustomizer 实现类定制化服务
NacosAutoServiceRegistration : 服务启动后自动注册的入口
AutoServiceRegistrationConfiguration : 用于注册 AutoServiceRegistrationProperties``
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
# 默认开启服务自动注册
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled",
matchIfMissing = true)
@AutoConfigureAfter({ AutoServiceRegistrationConfiguration.class,
AutoServiceRegistrationAutoConfiguration.class,
NacosDiscoveryAutoConfiguration.class })
public class NacosServiceRegistryAutoConfiguration {
@Bean
public NacosServiceRegistry nacosServiceRegistry(
NacosDiscoveryProperties nacosDiscoveryProperties) {
return new NacosServiceRegistry(nacosDiscoveryProperties);
}
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosRegistration nacosRegistration(
ObjectProvider<List<NacosRegistrationCustomizer>> registrationCustomizers,
NacosDiscoveryProperties nacosDiscoveryProperties,
ApplicationContext context) {
return new NacosRegistration(registrationCustomizers.getIfAvailable(),
nacosDiscoveryProperties, context);
}
@Bean
@ConditionalOnBean(AutoServiceRegistrationProperties.class)
public NacosAutoServiceRegistration nacosAutoServiceRegistration(
NacosServiceRegistry registry,
AutoServiceRegistrationProperties autoServiceRegistrationProperties,
NacosRegistration registration) {
return new NacosAutoServiceRegistration(registry,
autoServiceRegistrationProperties, registration);
}
}
——参考链接。
Nacos 2021.0.1.0 版本中,您可以通过以下方式获取 NacosNamingService:
com.alibaba.nacos
nacos-client
2021.0.1.0
CopyCopy
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.client.DefaultNamingClient;
import com.alibaba.nacos.api.naming.client.NamingClient;
import com.alibaba.nacos.config.annotation.NacosInjected;
import com.alibaba.nacos.config.client.ConfigClient;
import com.alibaba.nacos.config.client.ConfigClientFactory;
import com.alibaba.nacos.config.server.config.annotation.ConfigInjected;
import com.alibaba.nacos.config.server.config.annotation.RefreshScope;
@RefreshScope
public class NacosClientHolder {
@NacosInjected
private NamingService namingService;
@NacosInjected
private ConfigClient configClient;
private static NacosClientHolder instance;
private NacosClientHolder() {
// 初始化 Nacos 客户端
NamingClient namingClient = new DefaultNamingClient("127.0.0.1:8848");
namingClient.setNamespaceId("public");
namingClient.setTimeout(3000);
namingService = namingClient;
// 初始化 ConfigClient
ConfigClientFactory configClientFactory = new ConfigClientFactory();
configClientFactory.setConfigServerAddr("127.0.0.1:8848");
configClientFactory.setTimeout(3000);
configClient = configClientFactory.getConfigClient();
}
public static NacosClientHolder getInstance() {
if (instance == null) {
synchronized (NacosClientHolder.class) {
if (instance == null) {
instance = new NacosClientHolder();
}
}
}
return instance;
}
public NamingService getNamingService() {
return namingService;
}
public ConfigClient getConfigClient() {
return configClient;
}
}
CopyCopy
NacosClientHolder nacosClientHolder = NacosClientHolder.getInstance();
NamingService namingService = nacosClientHolder.getNamingService();
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
阿里云拥有国内全面的云原生产品技术以及大规模的云原生应用实践,通过全面容器化、核心技术互联网化、应用 Serverless 化三大范式,助力制造业企业高效上云,实现系统稳定、应用敏捷智能。拥抱云原生,让创新无处不在。