网站浏览思路,企业模板网站,wordpress单击右键提示你是坏人,上海虹口建设局官方网站我的后端学习大纲 SpringCloud学习大纲 1、编码实现3377服务#xff1a;
1.1.建module: 1.2.改pom: 1.3.写YML#xff1a; 1.Nacos同Consul一样#xff0c;在项目初始化时#xff0c;要保证先从配置中心进行配置拉取#xff0c;拉取配置之后#xff0c;才能保证项目的正… 我的后端学习大纲 SpringCloud学习大纲 1、编码实现3377服务
1.1.建module: 1.2.改pom: 1.3.写YML 1.Nacos同Consul一样在项目初始化时要保证先从配置中心进行配置拉取拉取配置之后才能保证项目的正常启动为了满足动态刷新和全局广播通知springboot中配置文件的加载是存在优先级顺序的bootstrap优先级高于application a.bootstrap.properties
1.必须使用bootstrap.properties配置文件来配置Nacos Server
# nacos配置
spring:application:name: nacos-config-clientcloud:nacos:discovery:server-addr: localhost:8848 #Nacos服务注册中心地址config:server-addr: localhost:8848 #Nacos作为配置中心地址file-extension: yaml #指定yaml格式的配置# nacos端配置文件DataId的命名规则是
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yamlb.application:
1.与Nacos无关的配置写在Application中
server:port: 3377spring:profiles:active: dev # 表示开发环境#active: prod # 表示生产环境#active: test # 表示测试环境1.4.主启动
package com.atguigu.cloudalibaba;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;EnableDiscoveryClient
SpringBootApplication
public class NacosConfigClient3377{public static void main(String[] args){SpringApplication.run(NacosConfigClient3377.class,args);}
}
1.5.业务类
package com.atguigu.cloud.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 //在控制器类加入RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
public class NacosConfigClientController
{Value(${config.info})private String configInfo;GetMapping(/config/info)public String getConfigInfo() {return configInfo;}
} RefreshScope 注解来实现配置的自动更新 2、在Nacos中的匹配规则
2.1.设置DataId理论
a.官网介绍
1.Nacos中的DataId的组成格式及与SpringBoot配置文件匹配的规则 最后公式${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} b.配置DataId实操
公式${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}prefix默认值spring.application.name的值spring.profiles.active即为当前环境对应的profile可以通过配置项spring.profiles.active来配置file-extension为配置文件的数据格式
2.2.创建配置 2.3.配置DataId和对应文件
Data IDnacos-config-client-dev.yaml 说明 配置更改后可以自动刷新Nacos会记录配置文件的历史版本默认保留30天此外还有一键回滚的功能回滚也会触发配置更新