Nacos-配置中心搭建

启动Nacos Server

这里通过docker来启动Nacos Docker

github上最新的nacos-docker项目与官网描述的有一点不同,这里须要修改几个地方:

修改standalone-derby.yaml

version: "3.7"
services:
  nacos:
    image: nacos/nacos-server:latest
    container_name: nacos-standalone
    environment:
    - PREFER_HOST_MODE=hostname
    - MODE=standalone
    volumes:
    - ./standalone-logs/:/home/nacos/logs
    - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
    ports:
    - "8848:8848"
  prometheus:
    container_name: prometheus
    image: prom/prometheus:latest
    volumes:
      - ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml
    ports:
      - "9090:9090"
    depends_on:
      - nacos
    restart: on-failure
  grafana:
    container_name: grafana
    image: grafana/grafana:latest
    ports:
      - 3000:3000
    restart: on-failure

第一行的version须要和本机的docker版本对应,见版本对应

另外nacos使用了挂载:

volumes:
    - ./standalone-logs/:/home/nacos/logs
    - ./init.d/custom.properties:/home/nacos/init.d/custom.properties
volumes:
      - ./prometheus/prometheus-standalone.yaml:/etc/prometheus/prometheus.yml

须要确认对应目录是否存在,不存在须要创建。

最后执行命令:

docker-compose -f example/standalone-derby.yaml up -d

会下载须要的镜像并启动容器。

至此,Nacos Server启动完成,可通过8848端口访问nacos控制台,默认账号nacos,密码nacos

http://localhost:8848/nacos/index.html

配置中心实现

这里以Nacos Spring Cloud的配置中心为例,可下载官方example:nacos-spring-cloud-config-example

pom.xml:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>0.2.1.RELEASE</version>
</dependency>

bootstrap.properties:

spring.cloud.nacos.config.server-addr=127.0.0.1:8848

# Nacos \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A
# Data ID\uFF1Aexample.properties
# Group\uFF1ADEFAULT_GROUP
# \u914D\u7F6E\u5185\u5BB9\uFF1AuseLocalCache=true
spring.application.name=example
# \u6307\u5B9A\u914D\u7F6E\u7684\u540E\u7F00\uFF0C\u652F\u6301 properties\u3001yaml\u3001yml\uFF0C\u9ED8\u8BA4\u4E3A properties
spring.cloud.nacos.config.file-extension=properties
#spring.cloud.nacos.config.file-extension=yaml

ConfigController:

@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController {

    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    /**
     * http://localhost:8080/config/get
     */
    @RequestMapping("/get")
    public boolean get() {
        return useLocalCache;
    }
}

启动后访问http://localhost:8080/config/get

发布一个配置项:

curl -X POST "http://192.168.127.135:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEFAULT_GROUP&content=useLocalCache=true"

再次访问http://localhost:8080/config/get

值已经动态改变了,查看控制台:

已经多了一个配置项

自定义Group配置

在没有明确指定 ${spring.cloud.nacos.config.group} 配置的情况下, 默认使用的是 DEFAULT_GROUP 。如果需要自定义自己的 Group,可以通过以下配置来实现:

spring.cloud.nacos.config.group=DEVELOP_GROUP

发布一个新的group的配置项:

curl -X POST "http://192.168.127.135:8848/nacos/v1/cs/configs?dataId=example.properties&group=DEVELOP_GROUP&content=useLocalCache=true"

更详细的配置请参考官方Nacos Config

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值