Nacos配置中心入门

1.1、创建nacos_config

pox.xml

  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
</dependencies>

1.1.2、创建bootstrap.yml

  • 注意:
    • 客户端配置文件的名称必须为bootstrap.yml
  • bootstrap/ application 的应用场景:
    • bootstrap.ymlapplicaton.yml 优先加载,应用于系统级别参数配置,一般不会变动;
    • application.yml应用于SpringBoot项目的自动化配置;
server:
  port: 8081
spring:
  cloud:
    nacos:
      config:
        server-addr: 192.168.10.135:8848
        file-extension: yaml #后缀名,只支持 properties 和 yaml 类型
        prefix:  nacos-config #文件名,如果没有配置则默认为 ${spring.appliction.name}

1.1.3、创建启动类

package com.bjpowernode;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigApp {

    public static void main(String[] args) {
        SpringApplication.run(NacosConfigApp.class);
    }
}

1.1.4、创建controller

package com.bjpowernode.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //重新从BeanFactory获取一个新的实例(该实例使用新的配置)
public class ConfigController {

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;
    @Value("${spring.datasource.url}")
    private String url;
    @Value("${spring.datasource.username}")
    private String username;
    @Value("${spring.datasource.password}")
    private String password;
    @Value("${spring.datasource.type}")
    private String type;

    @GetMapping("/config/info")
    public String getConfigInfo() {
        System.out.println(this);
        String configInfo = driverClassName+"<br>"+url+"<br>"+username+"<br>"
                +password+"<br>"+type;
        return configInfo;
    }
}

1.2.往Nacos添加配置信息

1.2.1.Nacos的Data ID

  • 在 Nacos 中,dataId(配置文件的命名的规则) 的完整格式如下:

    ${spring.cloud.nacos.config.prefix}.${spring.cloud.nacos.config.file-extension}

  • 说明:

    • spring.cloud.nacos.config.prefix:默认是当前服务的服务名称
    • spring.cloud.nacos.config.file-extension:配置文件的格式(后缀),目前只支持yaml和properties

1.2.2.创建配置文件

1.2.2.1.新建配置文件

1.2.2.2.添加配置文件

server:
  port: 8081
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.10.135:8848
  application:
    name: nacos-config
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://192.168.10.133:3306/health?characterEncoding=UTF-8
    username: root
    password: 1111
    type: com.alibaba.druid.pool.DruidDataSource
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值