小小的码农想请教一下了解微服务的大佬们,能不能发现我现在的问题,想请教一下,我究竟是哪一步出现了问题。我也去nacos官网看了看,并没发相应的解决方法,害。困扰了我3天,大四的码农该何去何从
mac环境
1.eclipse
2.maven 3.2.5
3.java 1.8.0
1.spring boot 2.2.7.RELEASE
2.spring-cloud-starter-alibaba-nacos-config 2.2.7.RELEASE
3.nacos 1.2.1
我通过两种方式想获取配置中心指定dataid中的配置信息,发现并没有获取出来。
方式一
通过写了前端控制器,利用@value标签获取,发现获取到的为null值
方式二
我在启动类里通过ConfigurableApplicationContextgetEnvironment().getProperty(“user.name”)获取配置信息,发现获取到的居然是本地电脑的用户名,并不是配置中心的信息
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.ziqiugroupId>
<artifactId>springcloud-nacos-serviceartifactId>
<version>1.0version>
<name>springcloud-nacos-servicename>
<description>nacos服务工程description>
<modules>
<module>springcloud-nacos-a-configmodule>
modules>
<packaging>pompackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
<platform-bom.version>Cairo-SR3platform-bom.version>
<spring-cloud-dependencies.version>Finchley.RELEASEspring-cloud-dependencies.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.2.7.RELEASEversion>
dependency>
dependencies>
dependencyManagement>
project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.siyiface.hellogroupId>
<artifactId>springcloud-nacos-configartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>springcloud-nacos-configname>
<description>nacos-providerdescription>
<parent>
<groupId>com.ziqiugroupId>
<artifactId>springcloud-nacos-serviceartifactId>
<version>1.0version>
parent>
<properties>
<java.version>1.8java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
dependencies>
project>
1.因为我使用的是yaml格式,我也在配置文件中声明了
server:
port: 8080
spring:
application:
name: nacos-config
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
file-extension: yaml
package com.siyiface.ceshi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringcloudNacosConfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringcloudNacosConfigApplication.class, args);
String userName = applicationContext.getEnvironment().getProperty("user.name");
String userAge = applicationContext.getEnvironment().getProperty("useLocalCache");
System.err.println("user name :"+userName+"; age: "+userAge);
}
}
package com.siyiface.ceshi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.nacos.api.config.annotation.NacosValue;
@RestController
@RequestMapping("/config")
@RefreshScope
public class controler {
@Value("${server.port}")
private String port;
@Value("${user.name:}")
private String name;
@NacosValue(value="${useLocalCache:}",autoRefreshed=true)
private String useLocalCachename;
@RequestMapping("/get")
public String get() {
return port +" "+ name +" "+ useLocalCachename;
// return useLocalCache;
}
}
首先感谢在提问区遇到一位大哥,连续两天帮助我解决问题,不求回报的精神,手把手的讲解使我受益两朵。在解决问题的当天,我决定把遇到的问题分享给大家。希望大家不要遇到和我一样的坑,绕过这个坑。
还记得前面我写的pom依赖吗,我之前用的nacos版本是1.2-1和spring boot版本是2-2.7
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.7.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.2.7.RELEASEversion>
dependency>
dependencies>
dependencyManagement>
老大哥和我说我的版本不对称,导致了spring获取不到nacos配置服务的信息。我还特地的去看了一下nacos的配置文件,发现正如老大哥所说,显示的请求返回为null。
我使用了nacos 1.4.2版本的,和配置依赖版本为2.2.0.RELEASE和如下的spring boot版本,成功解决问题。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
<version>2.2.0.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.0.7.RELEASEversion>
dependency>
dependencies>
dependencyManagement>
首先,遇到问题,一定要学会看日志。从日志中寻找问题,并多去github对应的社区逛逛,看看有没有人遇到相同的问题,其次在弄java项目要时刻注意版本问题(特别是官网特意强调的)
偷偷告诉你们…大哥从github上拿下来的代码改下ip就能用,然而我死活都报错!!!!。
nacos 2.X.X版本一定要注意!!!!!!!!本地的数据库要开启,还有nacos配置文件中的数据库账号密码一定要正确!!!!!