Spring Cloud项目(二)——集成Nacos作为配置中心

项目结构

在这里插入图片描述

代码编写

1、pom文件

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

2、application文件

spring:
  profiles:
# 表示你当前所使用的分支,dev开发分支,prod生产分支
#    active: prod
    active: dev

3、bootstrap文件

文件说明
同application.yml文件一样,都是springboot的配置文件,但是springboot优先读取bootstrap.yml文件

server:
  port: 8082

spring:
  application:
    name: nacos-config

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 # 注册中心路径
      config:
        server-addr: 127.0.0.1:8848 # 配置中心路径
        file-extension: yaml  # 后缀名
#        group: DEV_GROUP  # 所在组
#        namespace: DEV  # 命名空间
# 读取的文件命名规则:{应用名}-{所处环境}.{文件后缀名},比如 nacos-config-dev.yaml

4、启动类

package com.lx;

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

/**
 * description TODO
 *
 * @author 流星
 * @date 2022/8/18 15:51
 */
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigApp {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApp.class,args);
    }
}

5、controller

package com.lx.controller;

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;

@RestController
@RefreshScope
public class ConfigController {

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

    @RequestMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

测试结果

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

结语

下面关于Spring Cloud学习系列的博客的连接有的是不能使用的,因为博主还在努力创作中,敬请期待

附:Spring Cloud学习系列

Spring Cloud项目(一)——集成Nacos作为注册中心
Spring Cloud项目(二)——集成Nacos作为配置中心
Spring Cloud项目(三)——实现Nacos数据信息持久化到MySQL
Spring Cloud项目(四)——使用Ribbon作为负载均衡
Spring Cloud项目(五)——使用openFeign作为服务调用
Spring Cloud项目(六)——使用sentinel作为流量管理
Spring Cloud项目(七)——使用sentinel作为限流和熔断
Spring Cloud项目(八)——使用gateway作为服务网关

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

--流星。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值