Springboot War包部署下nacos无法注册问题


如果需要解决方法,请直接看 9. 如何在WAR包部署下成功注册nacos部分

1. @EnableDiscoveryClient的使用

在项目中需要使用nacos进行服务治理时,需要在启动类上添加该注解来实现服务的自动注册

/**
 * Annotation to enable a DiscoveryClient implementation.
 * @author Spencer Gibb
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(EnableDiscoveryClientImportSelector.class)
public @interface EnableDiscoveryClient {
   

	/**
	 * If true, the ServiceRegistry will automatically register the local server.
	 * @return - {@code true} if you want to automatically register.
	 */
	boolean autoRegister() default true;

}

autoRegister默认是注册的,引入EnableDiscoveryClientImportSelector.class

2. EnableDiscoveryClientImportSelector类的作用

首先将源码贴出来,该类继承SpringFactoryImportSelector(用来选择与泛型相关的配置,加载其相应实现)

/**
 * @author Spencer Gibb
 */
@Order(Ordered.LOWEST_PRECEDENCE - 100)
public class EnableDiscoveryClientImportSelector
		extends SpringFactoryImportSelector<EnableDiscoveryClient> {
   

	@Override
	public String[] selectImports(AnnotationMetadata metadata) {
   
		String[] imports = super.selectImports(metadata);

		AnnotationAttributes attributes = AnnotationAttributes.fromMap(
				metadata.getAnnotationAttributes(getAnnotationClass().getName(), true));

		boolean autoRegister = attributes.getBoolean("autoRegister");

		if (autoRegister) {
   
			List<String> importsList = new ArrayList<>(Arrays.asList(imports));
			importsList.add(
					"org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration");
			imports = importsList.toArray(new String[0]);
		}
		else {
   
			Environment env = getEnvironment();
			if (ConfigurableEnvironment.class.isInstance(env)) {
   
				ConfigurableEnvironment configEnv = (ConfigurableEnvironment) env;
				LinkedHashMap<String, Object> map = new LinkedHashMap<>();
				map.put("spring.cloud.service-registry.auto-registration.enabled", false);
				MapPropertySource propertySource = new MapPropertySource(
						"springCloudDiscoveryClient", map);
				configEnv.getPropertySources().addLast(propertySource);
			}
		}
		return imports;
	}

	@Override
	protected boolean isEnabled() {
   
		return getEnvironment().getProperty("spring.cloud.discovery.enabled",
				Boolean.class, Boolean.TRUE);
	}

	@Override
	protected boolean hasDefaultFactory() {
   
		return true;
	}

}

1.在selectImports(AnnotationMetadata)方法中获取@EnableDiscoveryClient中的autoRegister参数,如过为True则将
org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationConfiguration加入加载列表中。

2.当autoRegister=false时,设spring.cloud.service-registry.auto-registration.enabled=false,这样跟注册相关的类将不会自动装配,因为自动注册相关的类都有一个条件装配@ConditionalOnProperty(value =“spring.cloud.service-registry.auto-registration.enabled”, matchIfMissing = true)。

3.AutoServiceRegistrationConfiguration

@Configuration
@EnableConfigurationProperties(AutoServiceRegistrationProperties.class)
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
public class AutoServiceRegistrationConfiguration {
   

}

@ConditionOnProperty为条件装配,只有spring.cloud.service-registry.auto-registration.enabled=true时才装配。
该类更像是一个信号量,用来若其加载则会加载其后的那些注册类

4.NacosDiscoveryAutoConfiguration

/**
 * @author xiaojing
 * @author <a href="mailto:mercyblitz@gmail.com">Mercy</a>
 */
@Configuration
@EnableConfigurationProperties
@ConditionalOnNacosDiscoveryEnabled
@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true)
@AutoConfigureAfter({
    AutoServiceRegistrationConfiguration.class,
		AutoServiceRegistrationAutoConfiguration.class }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值