關(guān)于springboot中nacos動態(tài)路由的配置
nacos動態(tài)路由的配置
什么都不說了,springboot-nacos 不懂得的下面自行學(xué)習(xí)啦我直接貼下代碼!
首先。。。
我自己有個服務(wù)器。在無聊之時寫的代碼,主要是通過網(wǎng)關(guān)來調(diào)用接口所以有了下面的代碼。
1.作為一個動態(tài)路由維護(hù)管理的類
@Service public class DynamicRouteServiceImpl implements ApplicationEventPublisherAware { /** * */ @Autowired private RouteDefinitionWriter routeDefinitionWriter; private ApplicationEventPublisher publisher; /** * 增加路由 * @param definition * @return */ public String add(RouteDefinition definition) { routeDefinitionWriter.save(Mono.just(definition)).subscribe(); this.publisher.publishEvent(new RefreshRoutesEvent(this)); return "success"; } /** * 更新路由 * @param definition * @return */ public String update(RouteDefinition definition) { try { this.routeDefinitionWriter.delete(Mono.just(definition.getId())); } catch (Exception e) { return "update fail,not find route routeId: "+definition.getId(); } try { routeDefinitionWriter.save(Mono.just(definition)).subscribe(); this.publisher.publishEvent(new RefreshRoutesEvent(this)); return "success"; } catch (Exception e) { return "update route fail"; } } /** * 刪除路由 * @param id * @return */ public String delete(String id) { try { this.routeDefinitionWriter.delete(Mono.just(id)); return "delete success"; } catch (Exception e) { e.printStackTrace(); return "delete fail"; } } @Override public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { this.publisher = applicationEventPublisher; }
2.基于Nacos動態(tài)配置路由服務(wù)
@Component public class DynamicRouteServiceImplByNacos implements CommandLineRunner { private static final Logger logger = LoggerFactory.getLogger(DynamicRouteServiceImplByNacos.class); @Autowired private DynamicRouteServiceImpl dynamicRouteService; @Value("${spring.cloud.nacos.discovery.server-addr}") private String address; @Value("${config.dataId}") private String dataId; @Value("${config.groupId}") private String groupId; @Value("${config.timeout}") private Long timeout; /* @Value("${config.ignore}") private String ignore;*/ /** * 監(jiān)聽Nacos Server下發(fā)的動態(tài)路由配置 */ public void dynamicRouteByNacosListener() { try { ConfigService configService = NacosFactory.createConfigService(address); configService.addListener(dataId, groupId, new Listener() { @Override public void receiveConfigInfo(String configInfo) { try { logger.info("================Nacos 配置中心路由配置信息已修改================\n" + configInfo + "\n\n"); List<RouteDefinition> list = JSON.parseArray(configInfo, RouteDefinition.class); list.forEach(definition -> { dynamicRouteService.update(definition); }); } catch (Exception e) { e.printStackTrace(); } } @Override public Executor getExecutor() { return null; } }); /*configService.addListener(ignore, groupId, new Listener() { @Override public void receiveConfigInfo(String configInfo) { try { logger.info("================Nacos 配置中心忽略URL配置已修改================\n" + configInfo + "\n\n"); logger.info("\n" + configInfo + "\n\n"); List<String> ignoreList = JSON.parseArray(configInfo, String.class); IgnoreRouteConfig.setIgnoreRouteArr(ignoreList); } catch (Exception e) { e.printStackTrace(); } } @Override public Executor getExecutor() { return null; } });*/ } catch (Exception e) { e.printStackTrace(); } } @Override public void run(String... args) throws Exception { dynamicRouteByNacosListener(); } }
3.yml配置
4. nacos網(wǎng)關(guān)配置
5.最后:我建的是
父子工程兩個服務(wù)發(fā)到服務(wù)器后 注意注意注意!!!:一定要開啟防火墻,登上阿里云自己的服務(wù)增加端口,然后再linux中也要增加端口 linux防火墻相關(guān)命令
- 刪除 firewall-cmd --zone= public --remove-port=80/tcp --permanent
- 開放 firewall-cmd --zone=public --add-port=1935/tcp --permanent
- 查看狀態(tài) systemctl status firewalld
- 啟動 systemctl start firewalld
- 查看已開啟端口 firewall-cmd --list-ports
- 重啟 firewall-cmd --reload
yml配置文件中 有一個這個端口 這個也要在阿里云上面還有服務(wù)器開啟
這個就是接口訪問時的網(wǎng)關(guān)端口號
Springboot配置Nacos出現(xiàn)的問題
報錯信息
java.lang.ClassNotFoundException:org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
由于當(dāng)前Nacos版本還不支持Springboot 2.4.+ 的版本,所以需要降一個版本 為2.3.+
具體如下
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configurationPropertiesBeans' defined in class path resource [org/springframework/cloud/autoconfigure/ConfigurationPropertiesRebinderAutoConfiguration.class]: Post-processing of merged bean definition failed; nested exception is java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1f89ab83] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:270) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:762) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:567) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE] at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63) ~[spring-boot-2.4.5.jar:2.4.5] at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) ~[na:na] at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:375) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) ~[spring-boot-2.4.5.jar:2.4.5] at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) ~[spring-boot-2.4.5.jar:2.4.5] at com.lenyuqin.product.ProductApplication.main(ProductApplication.java:14) ~[classes/:na] Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1f89ab83] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.3.6.jar:5.3.6] at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.6.jar:5.3.6] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:232) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:210) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(InitDestroyAnnotationBeanPostProcessor.java:149) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:294) ~[spring-context-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-5.3.6.jar:5.3.6] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.3.6.jar:5.3.6] ... 31 common frames omitted Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na] at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na] at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310) ~[na:na] at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.3.6.jar:5.3.6] ... 38 common frames omitted Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na] at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na] at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na] ... 42 common frames omitted
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式
這篇文章主要介紹了SpringBoot切面攔截@PathVariable參數(shù)及拋出異常的全局處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java反射之Method的invoke方法實(shí)現(xiàn)教程詳解
這篇文章主要給大家介紹了關(guān)于java反射之Method的invoke方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01IntelliJ IDEA 安裝 Grep Console插件 自定義控制臺輸出多顏色格式功能
由于Intellij idea不支持顯示ascii顏色,grep-console插件能很好的解決這個問題,下面就以開發(fā)JavaEE項目中,結(jié)合Log4j配置多顏色日志輸出功能,感興趣的朋友一起看看吧2020-05-05SpringBoot里使用Servlet進(jìn)行請求的實(shí)現(xiàn)示例
這篇文章主要介紹了SpringBoot里使用Servlet進(jìn)行請求的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01