请选择 进入手机版 | 继续访问电脑版

[java] 关于springboot中nacos动态路由的设置

[复制链接]
查看94 | 回复8 | 2021-9-13 03:05:47 | 显示全部楼层 |阅读模式
目次

nacos动态路由的设置

什么都不说了,springboot-nacos 不懂得的下面自行学习啦我直接贴下代码!

起首 。。。

我本身 有个服务器。在无聊之时写的代码,告急 是通过网关来调用接口以是 有了下面的代码。

1.作为一个动态路由维护管理的类

  1. @Service
  2. public class DynamicRouteServiceImpl implements ApplicationEventPublisherAware {
  3. /**
  4. *
  5. */
  6. @Autowired
  7. private RouteDefinitionWriter routeDefinitionWriter;
  8. private ApplicationEventPublisher publisher;
  9. /**
  10. * 增加路由
  11. * @param definition
  12. * @return
  13. */
  14. public String add(RouteDefinition definition) {
  15. routeDefinitionWriter.save(Mono.just(definition)).subscribe();
  16. this.publisher.publishEvent(new RefreshRoutesEvent(this));
  17. return "success";
  18. }
  19. /**
  20. * 更新路由
  21. * @param definition
  22. * @return
  23. */
  24. public String update(RouteDefinition definition) {
  25. try {
  26. this.routeDefinitionWriter.delete(Mono.just(definition.getId()));
  27. } catch (Exception e) {
  28. return "update fail,not find route routeId: "+definition.getId();
  29. }
  30. try {
  31. routeDefinitionWriter.save(Mono.just(definition)).subscribe();
  32. this.publisher.publishEvent(new RefreshRoutesEvent(this));
  33. return "success";
  34. } catch (Exception e) {
  35. return "update route fail";
  36. }
  37. }
  38. /**
  39. * 删除路由
  40. * @param id
  41. * @return
  42. */
  43. public String delete(String id) {
  44. try {
  45. this.routeDefinitionWriter.delete(Mono.just(id));
  46. return "delete success";
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. return "delete fail";
  50. }
  51. }
  52. @Override
  53. public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
  54. this.publisher = applicationEventPublisher;
  55. }
复制代码

2.基于Nacos动态设置 路由服务

  1. @Component
  2. public class DynamicRouteServiceImplByNacos implements CommandLineRunner {
  3. private static final Logger logger = LoggerFactory.getLogger(DynamicRouteServiceImplByNacos.class);
  4. @Autowired
  5. private DynamicRouteServiceImpl dynamicRouteService;
  6. @Value("${spring.cloud.nacos.discovery.server-addr}")
  7. private String address;
  8. @Value("${config.dataId}")
  9. private String dataId;
  10. @Value("${config.groupId}")
  11. private String groupId;
  12. @Value("${config.timeout}")
  13. private Long timeout;
  14. /* @Value("${config.ignore}")
  15. private String ignore;*/
  16. /**
  17. * 监听Nacos Server下发的动态路由配置
  18. */
  19. public void dynamicRouteByNacosListener() {
  20. try {
  21. ConfigService configService = NacosFactory.createConfigService(address);
  22. configService.addListener(dataId, groupId, new Listener() {
  23. @Override
  24. public void receiveConfigInfo(String configInfo) {
  25. try {
  26. logger.info("================Nacos 配置中心路由配置信息已修改================\n" + configInfo + "\n\n");
  27. List<RouteDefinition> list = JSON.parseArray(configInfo, RouteDefinition.class);
  28. list.forEach(definition -> {
  29. dynamicRouteService.update(definition);
  30. });
  31. } catch (Exception e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. @Override
  36. public Executor getExecutor() {
  37. return null;
  38. }
  39. });
  40. /*configService.addListener(ignore, groupId, new Listener() {
  41. @Override
  42. public void receiveConfigInfo(String configInfo) {
  43. try {
  44. logger.info("================Nacos 配置中心忽略URL配置已修改================\n" + configInfo + "\n\n");
  45. logger.info("\n" + configInfo + "\n\n");
  46. List<String> ignoreList = JSON.parseArray(configInfo, String.class);
  47. IgnoreRouteConfig.setIgnoreRouteArr(ignoreList);
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. }
  51. }
  52. @Override
  53. public Executor getExecutor() {
  54. return null;
  55. }
  56. });*/
  57. } catch (Exception e) {
  58. e.printStackTrace();
  59. }
  60. }
  61. @Override
  62. public void run(String... args) throws Exception {
  63. dynamicRouteByNacosListener();
  64. }
  65. }
复制代码

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出现的标题

报错信息

  1. java.lang.ClassNotFoundException:org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
复制代码

由于当前Nacos版本还不支持Springboot 2.4.+ 的版本,以是 必要 降一个版本 为2.3.+

详细 如下:

  1. 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]
  2. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:579) ~[spring-beans-5.3.6.jar:5.3.6]
  3. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.6.jar:5.3.6]
  4. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.6.jar:5.3.6]
  5. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.6.jar:5.3.6]
  6. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.6.jar:5.3.6]
  7. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:213) ~[spring-beans-5.3.6.jar:5.3.6]
  8. at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:270) ~[spring-context-5.3.6.jar:5.3.6]
  9. at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:762) ~[spring-context-5.3.6.jar:5.3.6]
  10. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:567) ~[spring-context-5.3.6.jar:5.3.6]
  11. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:782) ~[spring-boot-2.4.5.jar:2.4.5]
  12. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:774) ~[spring-boot-2.4.5.jar:2.4.5]
  13. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:439) ~[spring-boot-2.4.5.jar:2.4.5]
  14. at org.springframework.boot.SpringApplication.run(SpringApplication.java:339) ~[spring-boot-2.4.5.jar:2.4.5]
  15. at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:144) ~[spring-boot-2.4.5.jar:2.4.5]
  16. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
  17. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
  18. at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) ~[spring-cloud-context-2.2.5.RELEASE.jar:2.2.5.RELEASE]
  19. at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.6.jar:5.3.6]
  20. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.6.jar:5.3.6]
  21. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.6.jar:5.3.6]
  22. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) ~[spring-context-5.3.6.jar:5.3.6]
  23. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:82) ~[spring-boot-2.4.5.jar:2.4.5]
  24. at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:63) ~[spring-boot-2.4.5.jar:2.4.5]
  25. at java.base/java.util.ArrayList.forEach(ArrayList.java:1540) ~[na:na]
  26. at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:117) ~[spring-boot-2.4.5.jar:2.4.5]
  27. at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:111) ~[spring-boot-2.4.5.jar:2.4.5]
  28. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:62) ~[spring-boot-2.4.5.jar:2.4.5]
  29. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:375) ~[spring-boot-2.4.5.jar:2.4.5]
  30. at org.springframework.boot.SpringApplication.run(SpringApplication.java:333) ~[spring-boot-2.4.5.jar:2.4.5]
  31. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1340) ~[spring-boot-2.4.5.jar:2.4.5]
  32. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1329) ~[spring-boot-2.4.5.jar:2.4.5]
  33. at com.lenyuqin.product.ProductApplication.main(ProductApplication.java:14) ~[classes/:na]
  34. Caused by: java.lang.IllegalStateException: Failed to introspect Class [org.springframework.cloud.context.properties.ConfigurationPropertiesBeans] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@1f89ab83]
  35. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:481) ~[spring-core-5.3.6.jar:5.3.6]
  36. at org.springframework.util.ReflectionUtils.doWithLocalMethods(ReflectionUtils.java:321) ~[spring-core-5.3.6.jar:5.3.6]
  37. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.buildLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:232) ~[spring-beans-5.3.6.jar:5.3.6]
  38. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.findLifecycleMetadata(InitDestroyAnnotationBeanPostProcessor.java:210) ~[spring-beans-5.3.6.jar:5.3.6]
  39. at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(InitDestroyAnnotationBeanPostProcessor.java:149) ~[spring-beans-5.3.6.jar:5.3.6]
  40. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessMergedBeanDefinition(CommonAnnotationBeanPostProcessor.java:294) ~[spring-context-5.3.6.jar:5.3.6]
  41. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyMergedBeanDefinitionPostProcessors(AbstractAutowireCapableBeanFactory.java:1098) ~[spring-beans-5.3.6.jar:5.3.6]
  42. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:576) ~[spring-beans-5.3.6.jar:5.3.6]
  43. ... 31 common frames omitted
  44. Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
  45. at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
  46. at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3167) ~[na:na]
  47. at java.base/java.lang.Class.getDeclaredMethods(Class.java:2310) ~[na:na]
  48. at org.springframework.util.ReflectionUtils.getDeclaredMethods(ReflectionUtils.java:463) ~[spring-core-5.3.6.jar:5.3.6]
  49. ... 38 common frames omitted
  50. Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
  51. at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583) ~[na:na]
  52. at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
  53. at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
  54. ... 42 common frames omitted
复制代码

以上为个人履历 ,渴望 能给大家一个参考,也渴望 大家多多支持脚本之家。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

avatar 柳芽2017 | 2021-9-20 19:25:33 | 显示全部楼层
十分赞同admin楼主!
回复

使用道具 举报

avatar ai2017 | 2021-9-29 18:39:20 | 显示全部楼层
楼上的别说的那么悲观好吧!
回复

使用道具 举报

avatar 杰9 | 2021-9-30 10:02:54 | 显示全部楼层
不错哦,admin楼主这是要火的节奏啊!
回复

使用道具 举报

avatar 下沙消掉 | 2021-10-3 01:16:49 | 显示全部楼层
admin楼主的病已经好了百分之六十二了!
回复

使用道具 举报

avatar Mirandawsj | 2021-10-3 01:16:51 | 显示全部楼层
经典!
回复

使用道具 举报

avatar 工兵班长纬 | 2021-10-14 13:05:36 | 显示全部楼层
哥回复的不是帖子,是寂寞!
回复

使用道具 举报

avatar 白度了一生一x | 2021-10-15 20:34:54 | 显示全部楼层
帖子很有深度!
回复

使用道具 举报

世界末日我都挺过去了,看到admin楼主我才知道为什么上帝留我到现在!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则