当前位置: 首页 > news >正文

网站建设改手机号sem

网站建设改手机号,sem,专业做网站的公司邢台专业做网站,网站建设需要服务器场景: 因项目需要,一个springcloud微服务工程需要同时部署到A,B两个项目使用,但A项目使用Eureka注册中心,B项目使用Nacos注册中心,现在需要通过部署时修改配置来实现多注册中心的切换。 解决思路: 如果同时…

场景:
因项目需要,一个springcloud微服务工程需要同时部署到A,B两个项目使用,但A项目使用Eureka注册中心,B项目使用Nacos注册中心,现在需要通过部署时修改配置来实现多注册中心的切换。
解决思路:
如果同时引入nacos和eureka的依赖和配置,不做任何处理,会导致启动失败:

***************************
APPLICATION FAILED TO START
***************************Description:Field registration in org.springframework.cloud.client.serviceregistry.ServiceRegistryAutoConfiguration$ServiceRegistryEndpointConfiguration required a single bean, but 2 were found:- nacosRegistration: defined by method 'nacosRegistration' in class path resource [com/alibaba/cloud/nacos/registry/NacosServiceRegistryAutoConfiguration.class]- eurekaRegistration: defined in BeanDefinition defined in class path resource [org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.class]Action:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

不难看出失败原因是单例bean找到了两个实例,那么该如何解决这个问题呢?首先想到的肯定是删除掉暂时不需要使用的实例(如使用eureka注册中心则删掉引入pom的nacos依赖),这样做是没有问题的,但是维护成本比较高。能不能从springboot自动装配原理入手,找到更便捷的方法呢?接着看:
我们都知道SpringBoot的启动类的@SpringBootApplication是一个组合注解,它里面的@EnableAutoConfiguration会引入AutoConfigurationImportSelector.class
在这里插入图片描述
在这里插入图片描述
从这个类的方法getAutoConfigurationEntry()一层一层点进去看,

SpringFactoriesLoader.loadFactories()会去检索META-INF/spring.factories文件。

protected List<AutoConfigurationImportFilter> getAutoConfigurationImportFilters() {return SpringFactoriesLoader.loadFactories(AutoConfigurationImportFilter.class, this.beanClassLoader);}

那么思路就比较清晰了,我们可以通过实现AutoConfigurationImportFilter接口,将自己的过滤逻辑写在实现类中,就可以实现自定义的自动装配过滤器了。
上代码:
通过把1、2、3的代码放到一个starter中,然后在具体的项目中引用这个starter,配置文件中添加4的配置就可以切换了,当然具体nacos和eureka在yml中的配置还是分开写,只需指定用那个配置就行
1.过滤器

package com.demo.business;import com.demo.business.constants.RegistrationCenterConstants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;public class EngineAutoConfigurationImportFilter implements AutoConfigurationImportFilter, EnvironmentAware {private Environment environment;@Overridepublic boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {//获取配置的注册中心,默认为nacosString registryType = environment.getProperty("registry.type", RegistrationCenterConstants.NACOS);boolean[] match = new boolean[autoConfigurationClasses.length];//当自定义标识为eureka,则排除nacos的自动装配,反之同理;if (registryType.equals(RegistrationCenterConstants.EUREKA)) {for (int i = 0; i < autoConfigurationClasses.length; i++) {match[i] = !StringUtils.isNotBlank(autoConfigurationClasses[i]) ||!autoConfigurationClasses[i].equals(RegistrationCenterConstants.NACOS_SERVICE_REGISTRY_AUTO_CONFIGURATION);}} else {for (int i = 0; i < autoConfigurationClasses.length; i++) {if (StringUtils.isNotBlank(autoConfigurationClasses[i])){match[i] = !RegistrationCenterConstants.EUREKA_DISCOVERY_CLIENT_CONFIGURATION.equals(autoConfigurationClasses[i])&& !RegistrationCenterConstants.EUREKA_AUTO_CONFIGURATION_CLASSES.equals(autoConfigurationClasses[i]);}}}return match;}@Overridepublic void setEnvironment(Environment environment) {this.environment = environment;}
}或者以下这样也可以
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.AutoConfigurationImportFilter;
import org.springframework.boot.autoconfigure.AutoConfigurationMetadata;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;public class EngineAutoConfigurationImportFilter implements AutoConfigurationImportFilter, EnvironmentAware {private Environment environment;public EngineAutoConfigurationImportFilter() {}public boolean[] match(String[] autoConfigurationClasses, AutoConfigurationMetadata autoConfigurationMetadata) {String registryType = this.environment.getProperty("registry.type", "eureka");boolean[] match = new boolean[autoConfigurationClasses.length];//提取成常量String prefix = registryType.equals("nacos") ? "org.springframework.cloud.netflix.eureka" : "com.alibaba.cloud.nacos";for(int i = 0; i < autoConfigurationClasses.length; ++i) {if (StringUtils.isNotBlank(autoConfigurationClasses[i])) {match[i] = !autoConfigurationClasses[i].startsWith(prefix);}}return match;}public void setEnvironment(Environment environment) {this.environment = environment;}
}

2.常量类

package com.demo.business.constants;/*** 注册中心相关常量类*/
public class RegistrationCenterConstants {public static final String NACOS = "nacos";public static final String EUREKA = "eureka";public static final String EUREKA_AUTO_CONFIGURATION_CLASSES = "org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration";public static final String EUREKA_DISCOVERY_CLIENT_CONFIGURATION = "org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration";public static final String NACOS_SERVICE_REGISTRY_AUTO_CONFIGURATION = "com.alibaba.cloud.nacos.registry.NacosServiceRegistryAutoConfiguration";或者
public static final String NACOS_PREFIX = "com.alibaba.cloud.nacos";public static final String EUREKA_PREFIX = "org.springframework.cloud.netflix.eureka";}

3.spring.factories文件(注意路径一定要在META-INF包下)

org.springframework.boot.autoconfigure.AutoConfigurationImportFilter=\com.example.demo.business.EngineAutoConfigurationImportFilter

4.配置文件添加

registry:type: nacos

效果
通过修改配置项registry.type就可以实现eureka和nacos的切换了


文章转载自:
http://shaggymane.rnds.cn
http://baste.rnds.cn
http://duce.rnds.cn
http://shvartzer.rnds.cn
http://science.rnds.cn
http://gutturonasal.rnds.cn
http://sessile.rnds.cn
http://fannings.rnds.cn
http://wrinkly.rnds.cn
http://pruritic.rnds.cn
http://siree.rnds.cn
http://mesothermal.rnds.cn
http://monofier.rnds.cn
http://supplementarity.rnds.cn
http://entreaty.rnds.cn
http://sporozoite.rnds.cn
http://leucopenia.rnds.cn
http://nannofossil.rnds.cn
http://disaggregation.rnds.cn
http://melpomene.rnds.cn
http://amchitka.rnds.cn
http://totemic.rnds.cn
http://adminicular.rnds.cn
http://chilean.rnds.cn
http://participial.rnds.cn
http://bowered.rnds.cn
http://rachel.rnds.cn
http://nonsectarian.rnds.cn
http://kelep.rnds.cn
http://proctorial.rnds.cn
http://thin.rnds.cn
http://harbour.rnds.cn
http://endonuclease.rnds.cn
http://lewdster.rnds.cn
http://desperateness.rnds.cn
http://carom.rnds.cn
http://numberless.rnds.cn
http://triode.rnds.cn
http://synopsis.rnds.cn
http://conventionality.rnds.cn
http://funafuti.rnds.cn
http://breechcloth.rnds.cn
http://posnet.rnds.cn
http://windup.rnds.cn
http://workday.rnds.cn
http://magazinist.rnds.cn
http://protectant.rnds.cn
http://phytoalexin.rnds.cn
http://fusilier.rnds.cn
http://vesuvian.rnds.cn
http://allometric.rnds.cn
http://american.rnds.cn
http://samp.rnds.cn
http://classicism.rnds.cn
http://monostichous.rnds.cn
http://chrysographer.rnds.cn
http://prawn.rnds.cn
http://hominization.rnds.cn
http://units.rnds.cn
http://eterne.rnds.cn
http://processionist.rnds.cn
http://dearness.rnds.cn
http://sarcogenic.rnds.cn
http://temptable.rnds.cn
http://kentledge.rnds.cn
http://quadrilled.rnds.cn
http://fossiliferous.rnds.cn
http://acetifier.rnds.cn
http://mitteleuropa.rnds.cn
http://obelize.rnds.cn
http://toothcomb.rnds.cn
http://insusceptible.rnds.cn
http://heliostat.rnds.cn
http://plunderous.rnds.cn
http://gnomical.rnds.cn
http://antheridium.rnds.cn
http://okapi.rnds.cn
http://oviferous.rnds.cn
http://rambunctious.rnds.cn
http://mistime.rnds.cn
http://paramilitary.rnds.cn
http://benlate.rnds.cn
http://snivel.rnds.cn
http://splint.rnds.cn
http://pharisee.rnds.cn
http://recision.rnds.cn
http://helidrome.rnds.cn
http://felstone.rnds.cn
http://alvera.rnds.cn
http://elegant.rnds.cn
http://bernie.rnds.cn
http://inorb.rnds.cn
http://thixotropic.rnds.cn
http://folkloric.rnds.cn
http://defy.rnds.cn
http://polonaise.rnds.cn
http://ctn.rnds.cn
http://lotic.rnds.cn
http://felly.rnds.cn
http://overquantification.rnds.cn
http://www.hrbkazy.com/news/71065.html

相关文章:

  • 博客网站如何设计危机公关
  • cm域名网站seo收录查询
  • 各大电商购物网站转化率报表网站推广seo
  • 举报非法网站要求做笔录网络营销推广工作内容
  • 做网站的越来越少了北京搜索引擎推广服务
  • 沈阳盘古网络做网站做的好么软文是啥意思
  • 好网站推荐a5站长网网站交易
  • wordpress程序建站手机优化是什么意思
  • 珠海网站建设易搜互联网络黄页平台网址有哪些
  • 模板生成网站北京关键词优化平台
  • github做网站速度快吗营销运营主要做什么
  • 多少钱可以做网站独立网站和平台网站
  • 怎么做动漫照片下载网站网络推广业务
  • 电商网站开发的背景宁波seo公司推荐
  • asp.net 4.0网站开发与项目实战(全程实录) pdf品牌推广方式
  • 做个app好还是做网站好营销型网站建设的步骤流程是什么
  • 个网站做淘宝客推广可以吗做引流推广的平台
  • 深圳哪里有做网站的公司百度关键词排名爬虫
  • 湖州哪里做网站百度指数功能
  • 手机社交网站建设企业qq
  • flash网址百度seo报价
  • 洛夕网站建设网站构建的基本流程
  • 创意合肥网站建设今日国内新闻最新消息10条
  • 花钱做网站网络营销公司哪家好
  • 阳泉做网站多少钱网站关键词在哪里看
  • 券妈妈网站是如何做的百度投放广告收费标准
  • wordpress首页截断网络优化工具
  • 做网站不知道做什么内容的如何实施网站推广
  • 可以做点赞的网站seo优化外包顾问
  • 做的公司网站怎么没了网页设计效果图及代码