按照Nacos Spring Cloud 快速开始、Nacos Spring Boot 快速开始,将我之前项目依赖的config-server和Euraka替换过来,过程并不是那么顺利
提示找不到配置

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.druidConfig': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'spring.datasource.url' in value "${spring.datasource.url}"

1 spring boot
版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本,这个官方有说明

<version.nacos.springboot>0.2.1</version.nacos.springboot>
<dependency><groupId>com.alibaba.boot</groupId><artifactId>nacos-config-spring-boot-starter</artifactId><version>${version.nacos.springboot}</version>
</dependency>

application.properties如下

nacos.config.server-addr=10.101.5.147:8847

按照下图配置,我的疑问是@NacosPropertySource写死dataId,如果更改环境呢?另外获取配置的中的值要使用com.alibaba.nacos.api.config.annotation.NacosValue,而不是org.springframework.beans.factory.annotation.Value

如果按照下图注释掉NacosPropertySource,则需要在启动的时候增加Active profilesbdo-test切换过去了。但这样并不完美。



因为还是有问题:
问题一:@NacosValue中autoRefreshed默认值为false,那么是否每一个配置变量都夭加上autoRefreshed = true呢?而@NacosPropertySource(dataId = "bdo-dev", autoRefreshed = true)是全部默认的
问题二:部署到测试环境怎么切换这边配置的环境呢?
2 spring cloud
上线的springboot遇到一些疑问,springcloud使用nacos就相对容易很多,可是springboot与springcloud之前差距真的很大吗?
pom.xml中

<version.nacos>2.0.1.RELEASE</version.nacos>
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>${version.nacos}</version>
</dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>${version.nacos}</version>
</dependency>

在使用到配置的程序中需要使用org.springframework.cloud.context.config.annotation.RefreshScope即可,org.springframework.beans.factory.annotation.Value则不用改变。

把nacos作为注册中心,增加@EnableDiscoveryClient

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;@Slf4j
@SpringBootApplication
@ComponentScan(basePackages = { "com.test.*" })
@MapperScan("com.test.**.dao")
@EnableScheduling
@EnableFeignClients(basePackages = { "com.test.*" })
@EnableCaching
@EnableDiscoveryClient
public class BdoParentApplication {public static void main(String[] args) {try {System.setProperty("es.set.netty.runtime.available.processors", "false");SpringApplication.run(BdoParentApplication.class, args);} finally {log.info("server start finish");}}
}

bootstrap.yml如下,这里通过命令空间id来判断是属于哪个环境

spring:application:name: bdocloud:nacos:discovery:server-addr: 10.101.5.147:8847namespace: 012e6aa3-a6d6-4a6c-9cfb-93f5e4d76667config:server-addr: 10.101.5.147:8847file-extension: ymlnamespace: 012e6aa3-a6d6-4a6c-9cfb-93f5e4d76667


可以在浏览器中通过http://10.101.5.147:8847/nacos/v1/cs/configs?dataId=bdo&group=DEFAULT_GROUP&tenant=012e6aa3-a6d6-4a6c-9cfb-93f5e4d76667看看是否能获取到配置,参数根据自己的调整.
在服务管理中服务列表中发现有些问题,因为服务注册到public下面了,正确的是应该是在DEV下面,怎么解决这个问题呢?那是因为spring.cloud.nacos.discovery.namespace没有配置导致的

如果把nacos当成注册中心又当成配置中心,则bootstrap.yml中设置这样就可以了。

nacos:server-addr: 10.101.5.147:8847namespace: 012e6aa3-a6d6-4a6c-9cfb-93f5e4d76667
spring:application:name: bdocloud:nacos:discovery:server-addr: ${nacos.server-addr}namespace: ${nacos.namespace}config:server-addr: ${nacos.server-addr}file-extension: ymlnamespace: ${nacos.namespace}

可以在com.alibaba.nacos.client.config.http.ServerHttpAgenthttpGet方法中设置断点,注意如果server-addr是域名,则需要hs.nacos.com:80,添加上80端口.
3 springmvc
按照下面的配置固然可以加载进来,但是缺点就是nacos自身的配置也写在xml里面,如果想properties和xml分离,就有些困难了。
nacos中Properties中如果有中文,直接发包会报js错误,如果先发布不带中文,然后编辑后再写入中文,提示语法错误,但是可以发布

<dependency><groupId>com.alibaba.nacos</groupId><artifactId>nacos-spring-context</artifactId><version>0.3.1</version></dependency>
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:nacos="http://nacos.io/schema/nacos"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://nacos.io/schema/nacos http://nacos.io/schema/nacos.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><!--开启注解--><nacos:annotation-driven></nacos:annotation-driven><!--指定nacos配置地址--><nacos:global-properties server-addr="hs.nacos.com:80" namespace="f730605f-e19a-4207-82d4-3b76e8d1e54f"/><!--指定dataId,group-id, 是否是自动刷新--><nacos:property-source data-id="peb-web" group-id="DEFAULT_GROUP" auto-refreshed="true" /></beans>


4 问题清单
4.1 数字问题
连接mysql访问拒绝,一开始以为是配置的问题,发现好像没什么错。原始yml将000000当成了数字,故这类字符,应该加上引号

02:00:33.873 ERROR com.alibaba.druid.pool.DruidDataSource - create connection SQLException, url: jdbc:mysql://10.101.100.30:3306/peb?characterEncoding=UTF-8, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user 'root'@'10.101.10.99' (using password: YES)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:870)at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1659)


出现下面的问题,在yml中增加spring.main.allow-bean-definition-overriding=true

The bean 'dataSource', defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class], could not be registered. A bean with that name has already been defined in class path resource [com/dzmsoft/ucs/user/server/config/DruidConfig.class] and overriding is disabled.Action:Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

如果要关掉nacos配置

spring:cloud:nacos:config:enabled: falsediscovery:enabled: false

nacos替代config-server和Euraka相关推荐

  1. spring cloud: 使用consul来替换config server

    上一篇提到了,eureka 2.x官方停止更新后,可以用consul来替代,如果采用consul的话,其实config server也没必要继续使用了,consul自带kv存储,完全可以取代confi ...

  2. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  3. 为Spring Cloud Config Server配置远程git仓库

    简介 虽然在开发过程,在本地创建git仓库操作起来非常方便,但是在实际项目应用中,多个项目组需要通过一个中心服务器来共享配置,所以Spring Cloud配置中心支持远程git仓库,以使分散的项目组更 ...

  4. 配置SpringCloud Config Client连上Config Server

    SpringCloud Config Client实际上就是连接到Config Server的普通应用,前面一篇文章 SpringCloud Config Server搭建 已经介绍了如何搭建一个Co ...

  5. SpringCloud Config Server搭建

    文章目录 1 创建Git仓库 2 创建应用配置文件 3 将应用配置文件commit到Git仓库中 4 创建Config Server应用 5 配置运行Config Server 6 访问Config ...

  6. 微服务SpringCloud—Config Server对称加密

    配置内容的加解密 在Git仓库中明文存储配置属性的.很多场景下,对于某些敏感的配置内容(例如数据库账号.密码等),应当加密存储. Config对称加解密 1.安装JCE 默认情况下我们的JRE自带了J ...

  7. 测试一个config server 服务器挂机后,集群是否能读写数据

    测试架构: 测试目的: 测试一个config server 服务器挂机后,集群是否能读写数据. 测试原因: 上周因为内存吃紧,准备添加内存.在查看服务器时,把一台服务器关机检查.     关机后,WE ...

  8. spring cloud: 使用consul来替换config server,config key/value 具体的配置详解

    1.先添加依赖 之前config server的依赖去掉,换成consul-config的依赖即可. <dependency><groupId>org.springframew ...

  9. java测试spring cloud_java – 从Spring引导单元测试中排除Spring Cloud Config Server

    鉴于我有以下3豆: @Component public class ServiceConfig { // This value is only available from the Spring Cl ...

  10. Spring Cloud Config Server简介

    1.概述 在本教程中,我们将回顾Spring Cloud Config Server的基础知识. 我们将设置一个Config Server ,然后构建一个客户端应用程序 ,该客户端应用程序在启动时会消 ...

最新文章

  1. 【MyBatis学习01】宏观上把握MyBatis框架
  2. Ueditor 自动换行,添加P标签问题
  3. php根据循环条件切换图片,thinkphp 循环显示图片问题!!!~~~~
  4. 安卓屏幕分辨率及UI尺寸详解
  5. boc android app,BOC
  6. C++17尝鲜:类模板中的模板参数自动推导
  7. 秦汉考场科目三路线图_海淀驾校科目三校内考场总结
  8. MTK平台LCM驱动加载流程
  9. Python pip安装报错及解决办法:is not a supported wheel on this platform
  10. dynamips中网卡参数如何和网络连接中的具体网卡匹配
  11. 很好用的返回顶部代码
  12. 狸窝全能视频转换器功能介绍
  13. 跳一跳html小游戏代码,100 行代码实现『跳一跳』辅助
  14. CTF-Tools 一款CTF古典密码加解密工具
  15. 使用VirtualBox创建Mac虚拟机
  16. 李宏毅机器学习 02回归
  17. bert-textcnn实现多标签文本分类(基于keras+keras-bert构建)
  18. JavaScript 实现页面内时间实时倒计时 计时器内附完整文件欢迎调用(可用于抢购倒计时,记录恋爱纪念日总时长等)输出对应的天数小时分钟秒数
  19. Cesium中添加entitie模型,实现贴地。
  20. 【科创人】瑞云创始人汪忠田:售后服务SaaS市场拓荒者

热门文章

  1. 组播负载分担、静态组播路由和MBGP技术原理
  2. 手把手搭建K3cloud插件开发环境
  3. 网易云音乐代码如何写入html,如何将网易云音乐加入到自己的网站!
  4. app的性能测试小工具Emmagee使用教程
  5. GD32F103单片机内部FLASH按半字读写驱动
  6. CS224n自然语言处理(四)——单词表示及预训练,transformer和BERT
  7. 简单说说jsonp原理
  8. TextCNN代码解读及实战
  9. OpenPose人体姿态估计详细配置(win10)
  10. android接收红外传感器发送的脉冲信号,Arduino红外传感器-IR Receiver Module红外接收模块...