关于springboot中nacos动态路由的配置
程序员文章站
2022-06-16 17:02:10
目录2.基于nacos动态配置路由服务springboot配置nacos出现的问题nacos动态路由的配置什么都不说了,springboot-nacos 不懂得的下面自行学习啦我直接贴下代码!首先。。...
nacos动态路由的配置
什么都不说了,springboot-nacos 不懂得的下面自行学习啦我直接贴下代码!
首先。。。
我自己有个服务器。在无聊之时写的代码,主要是通过网关来调用接口所以有了下面的代码。
1.作为一个动态路由维护管理的类
@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动态配置路由服务
@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;*/ /** * 监听nacos server下发的动态路由配置 */ 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网关配置
5.最后:我建的是
父子工程两个服务发到服务器后 注意注意注意!!!:一定要开启防火墙,登上阿里云自己的服务增加端口,然后再linux中也要增加端口 linux防火墙相关命令
- 删除 firewall-cmd --zone= public --remove-port=80/tcp --permanent
- 开放 firewall-cmd --zone=public --add-port=1935/tcp --permanent
- 查看状态 systemctl status firewalld
- 启动 systemctl start firewalld
- 查看已开启端口 firewall-cmd --list-ports
- 重启 firewall-cmd --reload
yml配置文件中 有一个这个端口 这个也要在阿里云上面还有服务器开启
这个就是接口访问时的网关端口号
springboot配置nacos出现的问题
报错信息
java.lang.classnotfoundexception:org.springframework.boot.context.properties.configurationbeanfactorymetadata
由于当前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
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。