php 使用nacos部署配置中心,无侵入式部署

6 篇文章 3 订阅

1、根据nacos文档描述部署nacos服务端

https://nacos.io/zh-cn/docs/quick-start-docker.html
注:可以根据自己需求修改对应文件后在构建docker容器

2、使用go-nacos脚本

源码地址:https://github.com/wannanbigpig/go-nacos

项目已经打包好了mac和liunx环境执行文件,下载地址:
https://github.com/wannanbigpig/go-nacos/releases/tag/v0.1.0

  • 部署命令如下
./go-nacos-liunx --help  // 查看参数
    
./go-nacos-liunx -dataId=test -group=dev -path=/home/www/xxxx/ -filename=.env 
    
# // OR 后台运行
    
nohup /home/go-nacos/go-nacos-liunx -dataId=test -group=dev -path=/home/www/xxxx/ -filename=.env  > runoob.log 2>&1 &

使用 supervisor 部署参考:https://blog.csdn.net/u010324331/article/details/124989141

  • 可以执行以下命令查看参数

./go-nacos-liunx -help

Usage of bin/go-nacos:
  -dataId string
    	the dataId in nacos
  -filename string
    	Local Storage filename
  -group string
    	the group in nacos (default "DEFAULT_GROUP")
  -ip string
    	the ip in nacos (default "127.0.0.1")
  -namespace string
    	the namespace in nacos
  -password string
    	the password to authenticate with (default "nacos")
  -path string
    	Local Storage Path
  -port uint
    	the port in nacos (default 8848)
  -username string
    	the username to authenticate as (default "nacos")
  -version
    	print version and exit

ps:非常适合解决 laravel 框架多机器部署时 .env 配置要分别部署的烦恼

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Nacos配置中心支持热部署配置,即在配置文件被修改后,能够自动更新应用程序中的配置信息,无需重启应用程序。 要实现配置中心的热部署,首先需要在应用程序中使用Nacos的配置客户端来获取配置信息。然后,在Nacos配置中心修改配置信息时,Nacos会自动通知应用程序更新配置信息。 具体实现步骤如下: 1. 在应用程序中引入Nacos的配置客户端依赖,例如在Maven项目中,可以添加以下依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> ``` 2. 在应用程序的配置文件中配置Nacos配置中心地址和应用程序的配置项,例如: ```yaml spring: application: name: my-application cloud: nacos: config: server-addr: localhost:8848 namespace: your-namespace group: your-group refreshable-dataids: your-config-id ``` 其中,`server-addr`是Nacos配置中心地址,`namespace`是Nacos的命名空间,`group`是配置的分组,`refreshable-dataids`是要热部署的配置项的ID。 3. 在应用程序中使用`@Value`注解来获取配置项的值,例如: ```java @RestController public class MyController { @Value("${your-config-id}") private String configValue; @GetMapping("/config") public String getConfig() { return configValue; } } ``` 4. 在Nacos配置中心修改配置信息时,Nacos会自动通知应用程序更新配置信息。应用程序会重新加载最新的配置信息,无需重启应用程序。 需要注意的是,热部署的配置项需要在配置文件中加上`@NacosConfigurationProperties`注解,例如: ```java @Configuration @NacosConfigurationProperties(prefix = "your-config-id", dataId = "your-config-id", groupId = "your-group", autoRefreshed = true) public class MyConfig { private String configValue; public String getConfigValue() { return configValue; } public void setConfigValue(String configValue) { this.configValue = configValue; } } ``` 这样,在配置文件中修改配置信息时,应用程序会自动更新`MyConfig`类中的`configValue`属性,无需手动获取配置项的值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值