按照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.1version.nacos.springboot>
<dependency><groupId>com.alibaba.bootgroupId><artifactId>nacos-config-spring-boot-starterartifactId><version>${version.nacos.springboot}version>
dependency>
application.properties
如下
nacos.config.server-addr&#61;10.101.5.147:8847
按照下图配置&#xff0c;我的疑问是&#64;NacosPropertySource
写死dataId
&#xff0c;如果更改环境呢&#xff1f;另外获取配置的中的值要使用com.alibaba.nacos.api.config.annotation.NacosValue
&#xff0c;而不是org.springframework.beans.factory.annotation.Value
如果按照下图注释掉NacosPropertySource
,则需要在启动的时候增加Active profiles
为bdo-test
切换过去了。但这样并不完美。
因为还是有问题&#xff1a;
问题一&#xff1a;&#64;NacosValue中autoRefreshed默认值为false
&#xff0c;那么是否每一个配置变量都夭加上autoRefreshed &#61; true
呢&#xff1f;而&#64;NacosPropertySource(dataId &#61; "bdo-dev", autoRefreshed &#61; true)
是全部默认的
问题二&#xff1a;部署到测试环境怎么切换这边配置的环境呢&#xff1f;
2 spring cloud
上线的springboot遇到一些疑问&#xff0c;springcloud使用nacos就相对容易很多&#xff0c;可是springboot与springcloud之前差距真的很大吗&#xff1f;
pom.xml中
<version.nacos>2.0.1.RELEASEversion.nacos>
<dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-nacos-configartifactId><version>${version.nacos}version>
dependency><dependency><groupId>com.alibaba.cloudgroupId><artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId><version>${version.nacos}version>
dependency>
在使用到配置的程序中需要使用org.springframework.cloud.context.config.annotation.RefreshScope
即可&#xff0c;org.springframework.beans.factory.annotation.Value
则不用改变。
把nacos作为注册中心&#xff0c;增加&#64;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;&#64;Slf4j
&#64;SpringBootApplication
&#64;ComponentScan(basePackages &#61; { "com.test.*" })
&#64;MapperScan("com.test.**.dao")
&#64;EnableScheduling
&#64;EnableFeignClients(basePackages &#61; { "com.test.*" })
&#64;EnableCaching
&#64;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&#61;bdo&group&#61;DEFAULT_GROUP&tenant&#61;012e6aa3-a6d6-4a6c-9cfb-93f5e4d76667
看看是否能获取到配置&#xff0c;参数根据自己的调整.
在服务管理中服务列表
中发现有些问题&#xff0c;因为服务注册到public
下面了&#xff0c;正确的是应该是在DEV
下面&#xff0c;怎么解决这个问题呢&#xff1f;那是因为spring.cloud.nacos.discovery.namespace
没有配置导致的
如果把nacos当成注册中心又当成配置中心&#xff0c;则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.ServerHttpAgent
中httpGet
方法中设置断点&#xff0c;注意如果server-addr
是域名&#xff0c;则需要hs.nacos.com:80
,添加上80端口.
3 springmvc
按照下面的配置固然可以加载进来&#xff0c;但是缺点就是nacos自身的配置也写在xml里面&#xff0c;如果想properties和xml分离&#xff0c;就有些困难了。
nacos中Properties中如果有中文&#xff0c;直接发包会报js错误&#xff0c;如果先发布不带中文&#xff0c;然后编辑后再写入中文&#xff0c;提示语法错误&#xff0c;但是可以发布
<dependency><groupId>com.alibaba.nacosgroupId><artifactId>nacos-spring-contextartifactId><version>0.3.1version>dependency>
<beans xmlns&#61;"http://www.springframework.org/schema/beans" xmlns:nacos&#61;"http://nacos.io/schema/nacos"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation&#61;"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:global-properties server-addr&#61;"hs.nacos.com:80" namespace&#61;"f730605f-e19a-4207-82d4-3b76e8d1e54f"/><nacos:property-source data-id&#61;"peb-web" group-id&#61;"DEFAULT_GROUP" auto-refreshed&#61;"true" />beans>
4 问题清单
4.1 数字问题
连接mysql访问拒绝&#xff0c;一开始以为是配置的问题&#xff0c;发现好像没什么错。原始yml将000000
当成了数字&#xff0c;故这类字符&#xff0c;应该加上引号
02:00:33.873 ERROR com.alibaba.druid.pool.DruidDataSource - create connection SQLException, url: jdbc:mysql://10.101.100.30:3306/peb?characterEncoding&#61;UTF-8, errorCode 1045, state 28000
java.sql.SQLException: Access denied for user &#39;root&#39;&#64;&#39;10.101.10.99&#39; (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)
出现下面的问题&#xff0c;在yml中增加spring.main.allow-bean-definition-overriding&#61;true
The bean &#39;dataSource&#39;, 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&#61;true
如果要关掉nacos配置
spring:cloud:nacos:config:enabled: falsediscovery:enabled: false