当前位置: 首页 > news >正文

详解springcloud nacos使用

1.nacos server安装

  1. 下载 Nacos Server 2.5.1 https://nacos.io/download/nacos-server/?spm=5238cd80.2ef5001f.0.0.3f613b7clM2t6D

部署文档:https://nacos.io/docs/latest/manual/admin/deployment/deployment-standalone/?spm=5238cd80.6a33be36.0.0.25b41e5deZvu7d

2.安装步骤

  1. 使用mysql数据库,刷入nacos sql脚本:
/*

 * Copyright 1999-2018 Alibaba Group Holding Ltd.
   *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
   *
 * http://www.apache.org/licenses/LICENSE-2.0
    *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
   */

/******************************************/
/*   表名称 = config_info                  */
/******************************************/
CREATE TABLE `config_info` (
                               `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
                               `data_id` varchar(255) NOT NULL COMMENT 'data_id',
                               `group_id` varchar(128) DEFAULT NULL COMMENT 'group_id',
                               `content` longtext NOT NULL COMMENT 'content',
                               `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
                               `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                               `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
                               `src_user` text COMMENT 'source user',
                               `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
                               `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
                               `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
                               `c_desc` varchar(256) DEFAULT NULL COMMENT 'configuration description',
                               `c_use` varchar(64) DEFAULT NULL COMMENT 'configuration usage',
                               `effect` varchar(64) DEFAULT NULL COMMENT '配置生效的描述',
                               `type` varchar(64) DEFAULT NULL COMMENT '配置的类型',
                               `c_schema` text COMMENT '配置的模式',
                               `encrypted_data_key` varchar(1024) NOT NULL DEFAULT '' COMMENT '密钥',
                               PRIMARY KEY (`id`),
                               UNIQUE KEY `uk_configinfo_datagrouptenant` (`data_id`,`group_id`,`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_info';

/******************************************/
/*   表名称 = config_info  since 2.5.0                */
/******************************************/
CREATE TABLE `config_info_gray` (
                                    `id` bigint unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
                                    `data_id` varchar(255) NOT NULL COMMENT 'data_id',
                                    `group_id` varchar(128) NOT NULL COMMENT 'group_id',
                                    `content` longtext NOT NULL COMMENT 'content',
                                    `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
                                    `src_user` text COMMENT 'src_user',
                                    `src_ip` varchar(100) DEFAULT NULL COMMENT 'src_ip',
                                    `gmt_create` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'gmt_create',
                                    `gmt_modified` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) COMMENT 'gmt_modified',
                                    `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
                                    `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
                                    `gray_name` varchar(128) NOT NULL COMMENT 'gray_name',
                                    `gray_rule` text NOT NULL COMMENT 'gray_rule',
                                    `encrypted_data_key` varchar(256) NOT NULL DEFAULT '' COMMENT 'encrypted_data_key',
                                    PRIMARY KEY (`id`),
                                    UNIQUE KEY `uk_configinfogray_datagrouptenantgray` (`data_id`,`group_id`,`tenant_id`,`gray_name`),
                                    KEY `idx_dataid_gmt_modified` (`data_id`,`gmt_modified`),
                                    KEY `idx_gmt_modified` (`gmt_modified`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='config_info_gray';

/******************************************/
/*   表名称 = config_tags_relation         */
/******************************************/
CREATE TABLE `config_tags_relation` (
                                        `id` bigint(20) NOT NULL COMMENT 'id',
                                        `tag_name` varchar(128) NOT NULL COMMENT 'tag_name',
                                        `tag_type` varchar(64) DEFAULT NULL COMMENT 'tag_type',
                                        `data_id` varchar(255) NOT NULL COMMENT 'data_id',
                                        `group_id` varchar(128) NOT NULL COMMENT 'group_id',
                                        `tenant_id` varchar(128) DEFAULT '' COMMENT 'tenant_id',
                                        `nid` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增长标识',
                                        PRIMARY KEY (`nid`),
                                        UNIQUE KEY `uk_configtagrelation_configidtag` (`id`,`tag_name`,`tag_type`),
                                        KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='config_tag_relation';

/******************************************/
/*   表名称 = group_capacity               */
/******************************************/
CREATE TABLE `group_capacity` (
                                  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                  `group_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Group ID,空字符表示整个集群',
                                  `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
                                  `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
                                  `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
                                  `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数,,0表示使用默认值',
                                  `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
                                  `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
                                  `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                  `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
                                  PRIMARY KEY (`id`),
                                  UNIQUE KEY `uk_group_id` (`group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='集群、各Group容量信息表';

/******************************************/
/*   表名称 = his_config_info              */
/******************************************/
CREATE TABLE `his_config_info` (
                                   `id` bigint(20) unsigned NOT NULL COMMENT 'id',
                                   `nid` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'nid, 自增标识',
                                   `data_id` varchar(255) NOT NULL COMMENT 'data_id',
                                   `group_id` varchar(128) NOT NULL COMMENT 'group_id',
                                   `app_name` varchar(128) DEFAULT NULL COMMENT 'app_name',
                                   `content` longtext NOT NULL COMMENT 'content',
                                   `md5` varchar(32) DEFAULT NULL COMMENT 'md5',
                                   `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                   `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
                                   `src_user` text COMMENT 'source user',
                                   `src_ip` varchar(50) DEFAULT NULL COMMENT 'source ip',
                                   `op_type` char(10) DEFAULT NULL COMMENT 'operation type',
                                   `tenant_id` varchar(128) DEFAULT '' COMMENT '租户字段',
                                   `encrypted_data_key` varchar(1024) NOT NULL DEFAULT '' COMMENT '密钥',
                                   `publish_type` varchar(50)  DEFAULT 'formal' COMMENT 'publish type gray or formal',
                                   `gray_name` varchar(50)  DEFAULT NULL COMMENT 'gray name',
                                   `ext_info`  longtext DEFAULT NULL COMMENT 'ext info',
                                   PRIMARY KEY (`nid`),
                                   KEY `idx_gmt_create` (`gmt_create`),
                                   KEY `idx_gmt_modified` (`gmt_modified`),
                                   KEY `idx_did` (`data_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='多租户改造';


/******************************************/
/*   表名称 = tenant_capacity              */
/******************************************/
CREATE TABLE `tenant_capacity` (
                                   `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
                                   `tenant_id` varchar(128) NOT NULL DEFAULT '' COMMENT 'Tenant ID',
                                   `quota` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '配额,0表示使用默认值',
                                   `usage` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '使用量',
                                   `max_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个配置大小上限,单位为字节,0表示使用默认值',
                                   `max_aggr_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '聚合子配置最大个数',
                                   `max_aggr_size` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值',
                                   `max_history_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最大变更历史数量',
                                   `gmt_create` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                   `gmt_modified` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '修改时间',
                                   PRIMARY KEY (`id`),
                                   UNIQUE KEY `uk_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='租户容量信息表';


CREATE TABLE `tenant_info` (
                               `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'id',
                               `kp` varchar(128) NOT NULL COMMENT 'kp',
                               `tenant_id` varchar(128) default '' COMMENT 'tenant_id',
                               `tenant_name` varchar(128) default '' COMMENT 'tenant_name',
                               `tenant_desc` varchar(256) DEFAULT NULL COMMENT 'tenant_desc',
                               `create_source` varchar(32) DEFAULT NULL COMMENT 'create_source',
                               `gmt_create` bigint(20) NOT NULL COMMENT '创建时间',
                               `gmt_modified` bigint(20) NOT NULL COMMENT '修改时间',
                               PRIMARY KEY (`id`),
                               UNIQUE KEY `uk_tenant_info_kptenantid` (`kp`,`tenant_id`),
                               KEY `idx_tenant_id` (`tenant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='tenant_info';

CREATE TABLE `users` (
                         `username` varchar(50) NOT NULL PRIMARY KEY COMMENT 'username',
                         `password` varchar(500) NOT NULL COMMENT 'password',
                         `enabled` boolean NOT NULL COMMENT 'enabled'
);

CREATE TABLE `roles` (
                         `username` varchar(50) NOT NULL COMMENT 'username',
                         `role` varchar(50) NOT NULL COMMENT 'role',
                         UNIQUE INDEX `idx_user_role` (`username` ASC, `role` ASC) USING BTREE
);

CREATE TABLE `permissions` (
                               `role` varchar(50) NOT NULL COMMENT 'role',
                               `resource` varchar(128) NOT NULL COMMENT 'resource',
                               `action` varchar(8) NOT NULL COMMENT 'action',
                               UNIQUE INDEX `uk_role_permission` (`role`,`resource`,`action`) USING BTREE
);
  1. 修改${nacos.home}/conf/application.properties文件,增加支持MySQL数据源配置,添加MySQL数据源的url、用户名和密码。
 spring.sql.init.platform=mysql
  
  ### Count of DB:
  db.num=1
  
  ### Connect URL of DB:
  db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
  db.user.0=root
  db.password.0=123456
  1. 设置登录账户密码:
#开启鉴权 
nacos.core.auth.enabled=true

#设置服务端验证 key
nacos.core.auth.server.identity.key=test
nacos.core.auth.server.identity.value=test

#设置默认 token
# The default token (Base64 String):
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

4.启动 nacos server ,windows下执行:

.\startup.cmd -m standalone 或者修改startup.cmd中mode=cluster为standalone 双击启动

  1. 访问地址:
    http://127.0.0.1:8848/nacos
    在这里插入图片描述
    默认用户密码为 nacos nacos
    修改后账户密码 nacos 123456

2.SpringCloud Nacos配置注册中心报错 Could not resolve placeholder xxx in value 原因分析:

springcloud阿里巴巴框架,读取nacos配置中心变量值:
在这里插入图片描述

出现异常错误:
2025-04-10T16:03:43.682+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[wemedia-oss-nacos] & group[DEFAULT_GROUP]
2025-04-10T16:03:43.689+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[wemedia-oss-nacos.properties] & group[DEFAULT_GROUP]
2025-04-10T16:03:43.694+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.c.NacosPropertySourceBuilder : Ignore the empty nacos configuration and get it based on dataId[wemedia-oss-nacos-dev.properties] & group[DEFAULT_GROUP]
2025-04-10T16:03:43.695+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name=‘bootstrapProperties-wemedia-oss-nacos-dev.properties,DEFAULT_GROUP’}, BootstrapPropertySource {name=‘bootstrapProperties-wemedia-oss-nacos.properties,DEFAULT_GROUP’}, BootstrapPropertySource {name=‘bootstrapProperties-wemedia-oss-nacos,DEFAULT_GROUP’}]
2025-04-10T16:03:43.698+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] c.a.nacos.client.logging.NacosLogging : Load Logback Configuration of Nacos fail, message: Could not initialize Logback Nacos logging from classpath:nacos-logback.xml
2025-04-10T16:03:43.698+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.wemedia.NacosApplication : The following 1 profile is active: “dev”
2025-04-10T16:03:44.034+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=a4be7677-4070-3c92-9fd3-af9028deafd6
2025-04-10T16:03:44.145+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port 8877 (http)
2025-04-10T16:03:44.151+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2025-04-10T16:03:44.151+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/10.1.16]
2025-04-10T16:03:44.178+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2025-04-10T16:03:44.178+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 471 ms
2025-04-10T16:03:45.386+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2025-04-10T16:03:46.486+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2025-04-10T16:03:46.495+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 1 endpoint(s) beneath base path ‘/actuator’
2025-04-10T16:03:46.528+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port 8877 (http) with context path ‘’
2025-04-10T16:03:46.530+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : initializer namespace from ans.namespace attribute : null
2025-04-10T16:03:46.530+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : initializer namespace from ALIBABA_ALIWARE_NAMESPACE attribute :null
2025-04-10T16:03:46.530+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : initializer namespace from namespace attribute :null
2025-04-10T16:03:46.537+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.impl.NacosClientAuthServiceImpl success.
2025-04-10T16:03:46.537+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.p.a.s.c.ClientAuthPluginManager : [ClientAuthPluginManager] Load ClientAuthService com.alibaba.nacos.client.auth.ram.RamClientAuthServiceImpl success.
2025-04-10T16:03:46.701+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [RpcClientFactory] create a new rpc client of f8596e2c-e91c-4966-a72a-ab099222d79e
2025-04-10T16:03:46.702+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] RpcClient init, ServerListFactory = com.alibaba.nacos.client.naming.core.ServerListManager
2025-04-10T16:03:46.702+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Registry connection listener to current client:com.alibaba.nacos.client.naming.remote.gprc.redo.NamingGrpcRedoService
2025-04-10T16:03:46.702+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Register server push request handler:com.alibaba.nacos.client.naming.remote.gprc.NamingPushRequestHandler
2025-04-10T16:03:46.702+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Try to connect to server on start up, server: {serverIp = ‘127.0.0.1’, server main port = 8848}
2025-04-10T16:03:46.702+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.c.remote.client.grpc.GrpcClient : grpc client connection server:127.0.0.1 ip,serverPort:9848,grpcTslConfig:{“sslProvider”:“OPENSSL”,“enableTls”:false,“mutualAuthEnable”:false,“trustAll”:false}
2025-04-10T16:03:46.814+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Success to connect to server [127.0.0.1:8848] on start up, connectionId = 1744272226706_127.0.0.1_4812
2025-04-10T16:03:46.814+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient C o n n e c t R e s e t R e q u e s t H a n d l e r 2025 − 04 − 10 T 16 : 03 : 46.814 + 08 : 00 I N F O 35784 − − − [ w e m e d i a − o s s − n a c o s ] [ m a i n ] c o m . a l i b a b a . n a c o s . c o m m o n . r e m o t e . c l i e n t : [ f 8596 e 2 c − e 91 c − 4966 − a 72 a − a b 099222 d 79 e ] R e g i s t e r s e r v e r p u s h r e q u e s t h a n d l e r : c o m . a l i b a b a . n a c o s . c o m m o n . r e m o t e . c l i e n t . R p c C l i e n t ConnectResetRequestHandler 2025-04-10T16:03:46.814+08:00 INFO 35784 --- [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Register server push request handler:com.alibaba.nacos.common.remote.client.RpcClient ConnectResetRequestHandler20250410T16:03:46.814+08:00INFO35784[wemediaossnacos][main]com.alibaba.nacos.common.remote.client:[f8596e2ce91c4966a72aab099222d79e]Registerserverpushrequesthandler:com.alibaba.nacos.common.remote.client.RpcClient$Lambda/0x000001edb93fcc88
2025-04-10T16:03:46.814+08:00 INFO 35784 — [wemedia-oss-nacos] [t.remote.worker] com.alibaba.nacos.common.remote.client : [f8596e2c-e91c-4966-a72a-ab099222d79e] Notify connected event to listeners.
2025-04-10T16:03:46.814+08:00 INFO 35784 — [wemedia-oss-nacos] [t.remote.worker] com.alibaba.nacos.client.naming : Grpc connection connect
2025-04-10T16:03:46.814+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [REGISTER-SERVICE] public registering service wemedia-oss-nacos with instance Instance{instanceId=‘null’, ip=‘192.168.239.1’, port=8877, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName=‘DEFAULT’, serviceName=‘null’, metadata={IPv6=null, preserved.register.source=SPRING_CLOUD}}
2025-04-10T16:03:46.823+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.registry.NacosServiceRegistry : nacos registry, DEFAULT_GROUP wemedia-oss-nacos 192.168.239.1:8877 register finished
2025-04-10T16:03:47.840+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.s.cloud.commons.util.InetUtils : Cannot determine local hostname
2025-04-10T16:03:47.842+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scopedTarget.nacosConfigClientController’: Injection of autowired dependencies failed
2025-04-10T16:03:47.844+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.registry.NacosServiceRegistry : De-registering from Nacos Server now…
2025-04-10T16:03:47.844+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [DEREGISTER-SERVICE] public deregistering service wemedia-oss-nacos with instance: Instance{instanceId=‘null’, ip=‘192.168.239.1’, port=8877, weight=1.0, healthy=true, enabled=true, ephemeral=true, clusterName=‘DEFAULT’, serviceName=‘null’, metadata={}}
2025-04-10T16:03:47.859+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.c.n.registry.NacosServiceRegistry : De-registration finished.
2025-04-10T16:03:47.859+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.cache.ServiceInfoHolder do shutdown begin
2025-04-10T16:03:47.859+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.backups.FailoverReactor do shutdown begin
2025-04-10T16:03:48.182+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.backups.FailoverReactor do shutdown stop
2025-04-10T16:03:48.182+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.cache.ServiceInfoHolder do shutdown stop
2025-04-10T16:03:48.182+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate do shutdown begin
2025-04-10T16:03:48.182+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.core.ServiceInfoUpdateService do shutdown begin
2025-04-10T16:03:48.182+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.core.ServiceInfoUpdateService do shutdown stop
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.core.ServerListManager do shutdown begin
2025-04-10T16:03:48.183+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [NamingHttpClientManager] Start destroying NacosRestTemplate
2025-04-10T16:03:48.183+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [NamingHttpClientManager] Destruction of the end
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.core.ServerListManager do shutdown stop
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy do shutdown begin
2025-04-10T16:03:48.183+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [NamingHttpClientManager] Start destroying NacosRestTemplate
2025-04-10T16:03:48.183+08:00 WARN 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : [NamingHttpClientManager] Destruction of the end
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.remote.http.NamingHttpClientProxy do shutdown stop
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : Shutdown rpc client, set status to shutdown
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : Shutdown client event executor java.util.concurrent.ScheduledThreadPoolExecutor@431babe6[Running, pool size = 2, active threads = 2, queued tasks = 0, completed tasks = 0]
2025-04-10T16:03:48.183+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.common.remote.client : Close current connection 1744272226706_127.0.0.1_4812
2025-04-10T16:03:48.186+08:00 WARN 35784 — [wemedia-oss-nacos] [tor-127.0.0.1-9] c.a.n.c.remote.client.grpc.GrpcClient : [1744272226706_127.0.0.1_4812]Ignore error event,isRunning:false,isAbandon=false
2025-04-10T16:03:48.187+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.c.remote.client.grpc.GrpcClient : Shutdown grpc executor java.util.concurrent.ThreadPoolExecutor@2f73f0c7[Running, pool size = 10, active threads = 0, queued tasks = 0, completed tasks = 10]
2025-04-10T16:03:48.187+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : Shutdown grpc redo service executor java.util.concurrent.ScheduledThreadPoolExecutor@59e0d521[Running, pool size = 1, active threads = 0, queued tasks = 1, completed tasks = 0]
2025-04-10T16:03:48.187+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.c.a.r.identify.CredentialWatcher : [null] CredentialWatcher is stopped
2025-04-10T16:03:48.187+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] c.a.n.c.a.r.identify.CredentialService : [null] CredentialService is freed
2025-04-10T16:03:48.187+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] com.alibaba.nacos.client.naming : com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate do shutdown stop
2025-04-10T16:03:48.191+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2025-04-10T16:03:48.199+08:00 INFO 35784 — [wemedia-oss-nacos] [ main] .s.b.a.l.ConditionEvaluationReportLogger :

Error starting ApplicationContext. To display the condition evaluation report re-run your application with ‘debug’ enabled.
2025-04-10T16:03:48.207+08:00 ERROR 35784 — [wemedia-oss-nacos] [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scopedTarget.nacosConfigClientController’: Injection of autowired dependencies failed
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:499) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:600) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:523) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean 1 ( A b s t r a c t B e a n F a c t o r y . j a v a : 364 )   [ s p r i n g − b e a n s − 6.1.1. j a r : 6.1.1 ] a t o r g . s p r i n g f r a m e w o r k . c l o u d . c o n t e x t . s c o p e . G e n e r i c S c o p e 1(AbstractBeanFactory.java:364) ~[spring-beans-6.1.1.jar:6.1.1] at org.springframework.cloud.context.scope.GenericScope 1(AbstractBeanFactory.java:364) [springbeans6.1.1.jar:6.1.1]atorg.springframework.cloud.context.scope.GenericScopeBeanLifecycleWrapper.getBean(GenericScope.java:375) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.cloud.context.scope.GenericScope.get(GenericScope.java:179) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:361) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1173) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.cloud.context.scope.refresh.RefreshScope.eagerlyInitialize(RefreshScope.java:126) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.cloud.context.scope.refresh.RefreshScope.start(RefreshScope.java:118) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:113) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.cloud.context.scope.refresh.RefreshScope.onApplicationEvent(RefreshScope.java:68) ~[spring-cloud-context-4.1.0.jar:4.1.0]
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:178) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:171) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:149) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:445) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:968) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:619) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.2.0.jar:3.2.0]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:753) ~[spring-boot-3.2.0.jar:3.2.0]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:455) ~[spring-boot-3.2.0.jar:3.2.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:323) ~[spring-boot-3.2.0.jar:3.2.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1342) ~[spring-boot-3.2.0.jar:3.2.0]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1331) ~[spring-boot-3.2.0.jar:3.2.0]
at com.wemedia.NacosApplication.main(NacosApplication.java:11) ~[classes/:na]

Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder ‘config.info’ in value"${config.info}

at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:180) ~[spring-core-6.1.1.jar:6.1.1]
at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126) ~[spring-core-6.1.1.jar:6.1.1]
at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:239) ~[spring-core-6.1.1.jar:6.1.1]
at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:210) ~[spring-core-6.1.1.jar:6.1.1]
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.lambda$processProperties$0(PropertySourcesPlaceholderConfigurer.java:200) ~[spring-context-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:921) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1372) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1348) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:769) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:752) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:145) ~[spring-beans-6.1.1.jar:6.1.1]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:493) ~[spring-beans-6.1.1.jar:6.1.1]
... 27 common frames omitted

2025-04-10T16:03:48.209+08:00 WARN 35784 — [wemedia-oss-nacos] [ Thread-1] c.a.n.common.http.HttpClientBeanHolder : [HttpClientBeanHolder] Start destroying common HttpClient
2025-04-10T16:03:48.209+08:00 WARN 35784 — [wemedia-oss-nacos] [ Thread-7] c.a.nacos.common.notify.NotifyCenter : [NotifyCenter] Start destroying Publisher
2025-04-10T16:03:48.209+08:00 WARN 35784 — [wemedia-oss-nacos] [ Thread-7] c.a.nacos.common.notify.NotifyCenter : [NotifyCenter] Destruction of the end
2025-04-10T16:03:48.210+08:00 WARN 35784 — [wemedia-oss-nacos] [ Thread-1] c.a.n.common.http.HttpClientBeanHolder : [HttpClientBeanHolder] Destruction of the end

Process finished with exit code 1

错误原因分析:
1.检查关键代码

server:
  port: 8877

spring:
  application:
    name: wemedia-oss-nacos
  profiles:
    active: dev
  #    active: test
  #    active: pred
  #    active: prod
  cloud:
    nacos:
      username: 'nacos'
      password: '123456'
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: properties  #必须指定nacos中dataid的后缀类型:yaml/properties





在这里插入代码片

@RestController
@RefreshScope // 支持动态刷新功能
public class NacosConfigClientController {


    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        return configInfo;
    }

    public void setConfigInfo(String configInfo) {
        this.configInfo = configInfo;
    }
}

配置文件出现问题,有几种可能

  1. 配置文件格式写错
    配置文件的格式只能是这几种:
    在这里插入图片描述
    == 必须与你spring cloud中指定的file-extension的格式保持一致==

  2. 文件格式写错

  3. 没有按照命名规范配置配置文件
    配置文件命名方式:

${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}

Data ID:
       wemedia-oss-nacos-dev.properties

在这里插入图片描述

相关文章:

  • 基于单片机技术的手持式酒精检测电路设计
  • RAG的实现快速示例
  • Redhat红帽 RHCE8.0认证体系课程
  • Python进阶(2):函数
  • 移动端六大语言速记:第13部分 - 网络与通信
  • LLM介绍
  • 玩转代理 IP :实战爬虫案例
  • 212、【图论】字符串接龙(Python)
  • Flutter 2025 Roadmap
  • redis 免安装版本 启动方法 windows 安装包
  • 性能比拼: Redis vs Memcached
  • AI Agent类开发应避免Python独舞,奏响多技术交响曲
  • 【cesium】在vue2中使用cesium(持续更新)
  • 基于VSCode的Qt开发‘#include ui_test.h’报错没有该文件
  • 沐渥科技详解氮气柜操作指南
  • C++程序诗篇的灵动赋形:多态
  • 李沐《动手学深度学习》 | 线性神经网络-线性回归
  • 《USB技术应用与开发》第二讲:连接和枚举
  • Python实例题:Python3实现命令行动态进度条
  • WebGPU:前端图形技术的革命性进化与WebGL的未来