一、首先添加nacos注册发现的pom依赖

        <dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>nacos-discovery-spring-boot-starter</artifactId>
<version>0.2.3</version>
</dependency>

二、application.properties文件中添加nacos的配置

nacos.discovery.server-addr=你的nacos地址:你的nacos端口

三、在项目启动类中添加配置,启动时向nacos注册服务

    @NacosInjected
private NamingService namingService;
@Value("${spring.application.name}")
private String applicationName;
@PostConstruct
public void registerInstance() throws NacosException {
namingService.registerInstance(applicationName,"127.0.0.1",9193);
}

以上就是spring boot 的注册方式,比较简单,项目启动后注册成功

但是在第一次配置的时候发生了一些问题,项目启动报错:注册失败,然后追踪

namingService.registerInstance方法的源码,发现是调用httpclient注册的时候报错,源码中有一部分代码如下:
 public String callServer(String api, Map<String, String> params, String curServer, String method) throws NacosException {
List<String> headers = Arrays.asList("Client-Version", "Nacos-Java-Client:v0.2.1", "Accept-Encoding", "gzip,deflate,sdch", "Connection", "Keep-Alive", "RequestId", UuidUtil.generateUuid());
if (!curServer.contains(":")) {
curServer = curServer + ":" + 8848;
} String url = HttpClient.getPrefix() + curServer + api;
HttpResult result = HttpClient.request(url, headers, params, "UTF-8", method);
if (200 == result.code) {
return result.content;
} else if (304 == result.code) {
return "";
} else {
LogUtils.LOG.error("CALL-SERVER", "failed to req API:" + HttpClient.getPrefix() + curServer + api + ". code:" + result.code + " msg: " + result.content);
throw new NacosException(500, "failed to req API:" + HttpClient.getPrefix() + curServer + api + ". code:" + result.code + " msg: " + result.content);
}
}

发现,http接口的返回结果报错:cluster name can only have these characters: 0-9a-zA-Z-

查了很多资料发现是pom引入的版本问题,改为最新版本,注册成功

spring boot 向nacos注册方式,以及遇见的报错(boot!boot! 不是cloud!)的更多相关文章

  1. SpringBoot注册Windows服务和启动报错的原因

    SpringBoot注册Windows服务和启动报错的原因 Windows系统启动Java程序会弹出黑窗口.黑窗口有几点不好.首先它不美观:其次容易误点导致程序关闭:但最让我匪夷所思的是:将鼠标光标选 ...

  2. Hp电脑开机报错:no boot disk has been detected or the disk has failed

    hp主机开机报错no boot disk has been detected  or the disk has failed,重启之后没有作用,开机之后仍然是同样界面.考虑是硬盘问题,按ESC+F10 ...

  3. 【spring boot】使用注解@ConfigurationProperties读取配置文件时候 报错 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'rocketmqAutoConfiguration': Unsatisfied dependenc

    如题,配置文件如下: #注册中心配置 eureka: instance: instanceId: ${spring.application.name}:${random.int} hostname: ...

  4. 解决spring-boot配置文件使用加密方式保存敏感数据启动报错No decryption for FailsafeTextEncryptor. Did you configure the keystore correctly

    spring-boot配置文件使用加密方式保存敏感数据 application.yml spring: datasource: username: dbuser password: '{cipher} ...

  5. mutillidae之注册页面的Insert型报错注入

    http://127.0.0.1/mutillidae/index.php?page=register.php 1.注册一个用户试一试,发现页面只提示用户注册成功信息,并五其它可回显信息,果断尝试盲注 ...

  6. 关于spring boot 使用 mybatis plus INSERT的时候id报错

    mybatis plus 在INSERT的时候会默认自动设置插入id 我当时数据库采用的id自增. 在使用插入语句的时候并没有set  ID 但是它默认给了一大串 更改mybatis plus全局配置 ...

  7. STS创建spring boot项目,pom.xml文件第一行报错

    亲测能用url地址:https://blog.csdn.net/jrx1995/article/details/100008552

  8. Spring Boot 2.1.7 启动项目失败,报错: "Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured."

    一开始按照网上的很多解决办法是: 启动类头部声明@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class}),但是这样会排除 ...

  9. spring程序打包使用该插件,不然容易报错xsd找不到

    <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade ...

  10. AJAX方式发送远程请求报错:No 'Access-Control-Allow-Origin' header

    AJAX GET方式发送远程请求,chrome开发者工具console中报错:XMLHttpRequest cannot load http://www.shikezhi.com/ajax/getDa ...

随机推荐

  1. 从.net开发做到云原生运维(一)——从.net framework过渡到.net core

    1. 前言 序篇讲了自己的一些感悟和经历,从这章开始就开始讲一些.net技术栈的东西了. 2. .net framework和.net core对比 .NET Framework 概述 .NET Fr ...

  2. 强化学习:经典测试环境Cart-pole的原始文献

    参考文献格式: A. G. Barto, R. S. Sutton, and C. W. Anderson. Neuronlike adaptive elements that can solve d ...

  3. Linux系统下使用pytorch多进程读取图片数据时的注意事项——DataLoader的多进程使用注意事项

    原文: PEP 703 – Making the Global Interpreter Lock Optional in CPython 相关内容: The GIL Affects Python Li ...

  4. 神经网络初始化:xavier,kaiming、ortho正交初始化在CNN网络中的使用

    xavier.ortho是神经网络中常用的权重初始化方法,在全连接中这两种权重初始化的方法比较好理解,但是在CNN的卷积网络中的具体实现却不好理解了. 在CNN网络中xavier的初始化可以参看: [ ...

  5. 为python安装扩展模块时报错——error: invalid command 'bdist_wheel'

    具体过程: devil@hp:~/lab$ ./bazel-bin/python/pip_package/build_pip_package /tmp/dmlab_pkg2022年 10月 03日 星 ...

  6. 亚信科技基于 Apache SeaTunnel 的二次开发应用实践

    亚信科技在Apache SeaTunnel的实践分享 自我介绍 各位同学好,很荣幸通过Apache SeaTunnel社区和大家进行分享交流.我是来自亚信科技的潘志宏,主要负责公司内部数据中台产品的开 ...

  7. SpringBoot 整合线程池

    分为三步 启动类加 @EnableAsync 注解 在方法上加 @Async 注解 创建线程池配置类 1.启动类加 @EnableAsync 注解 @SpringBootApplication @En ...

  8. quartz监控日志(一)

    最近几个月,现网总是出现定时器不执行的情况,或者定时器卡死的情况,而又不方便排查,只能依靠quartz的debug日志以及错误日志来监控定时器的执行情况,并且随着我们系统中job越来越多,而使得job ...

  9. 开关中断与cpsid/cpsie指令

    在汇编代码中,CPSID   CPSIE  用于快速的开关中断. I:IRQ中断;    F:FIQ中断最常见的这两个命令的使用处是在关中断.开中断的实现中,我们经常用的local_irq_enabl ...

  10. 什么是AOP,以及在Springboot中自定义AOP

    AOP (Aspect Oriented Programming)一般译为面向切面编程 Aspect [ˈæspekt] n.方面;层面;(动词的)体那么AOP 面相切面编程具体是指什么,它和之前的O ...