当前位置: 首页 > news >正文

阿里巴巴做轮播网站辽宁建设厅官方网站

阿里巴巴做轮播网站,辽宁建设厅官方网站,重庆免费自助建站模板,徐州做网站沈阳厂商文章目录 nacosnacos下载nacos启动nacos相关配置demo-dev.yamldemo-test.yamluser.yaml 代码pom.xmlUserConfigBeanAutoRefreshConfigExampleValueAnnotationExampleDemoApplicationbootstrap.yml测试结果补充.刷新静态配置 nacos nacos下载 下载地址 一键傻瓜试安装即可,官…

文章目录

    • nacos
      • nacos下载
      • nacos启动
      • nacos相关配置
        • demo-dev.yaml
        • demo-test.yaml
        • user.yaml
    • 代码
      • pom.xml
      • UserConfig
      • BeanAutoRefreshConfigExample
      • ValueAnnotationExample
      • DemoApplication
      • bootstrap.yml
      • 测试结果
      • 补充.刷新静态配置

nacos

nacos下载

下载地址

一键傻瓜试安装即可,官网写的很清楚这里不在赘述 http://nacos.io/zh-cn/docs/v2/quickstart/quick-start.html

nacos启动

将模式改为单机模式

请添加图片描述

启动成功

请添加图片描述

nacos相关配置

请添加图片描述

demo-dev.yaml
server:port: 8001config:info: "config info for dev from nacos config center"
demo-test.yaml
server:port: 3333config:info: "config info for test from nacos config center"
user.yaml
user:name: zs1112222age: 10address: 测试地址

请添加图片描述

代码

整合nacos配置中心,注册中心,完整项目地址 gitee地址

pom.xml

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.2.2.RELEASE</version>
</parent><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2.2.2.RELEASE</version></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2.2.2.RELEASE</version></dependency>
</dependencies>

UserConfig

@Data
@Configuration
@ConfigurationProperties(prefix = "user")
public class UserConfig {private String name;private Integer age;private String address;}

BeanAutoRefreshConfigExample

@RestController
public class BeanAutoRefreshConfigExample {@Autowiredprivate UserConfig userConfig;@GetMapping("/user/hello")public String hello(){return userConfig.getName() + userConfig.getAge() + userConfig.getAddress();}}

ValueAnnotationExample

@RestController
@RefreshScope
public class ValueAnnotationExample {@Value("${config.info}")private String configInfo;@GetMapping("/config/info")public String getConfigInfo() {return configInfo;}}

DemoApplication

@SpringBootApplication
public class DemoApplication {public static void main(String[] args) {SpringApplication.run(DemoApplication.class, args);}}

bootstrap.yml

spring:profiles:# 指定环境 切换环境active: devapplication:name: democloud:# nacos server dataId# ${spring.application.name)}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}nacos:# Nacos服务注册中心discovery:serverAddr: @serverAddr@group: DEMO_GROUPnamespace: 25af15f3-ae79-41c3-847d-960adb953185username: @username@password: @password@# Nacos作为配置中心config:server-addr: @serverAddr@file-extension: yamlgroup: DEMO_GROUPnamespace: 25af15f3-ae79-41c3-847d-960adb953185username: @username@password: @password@# 加载多配置extension-configs:- data-id: user.yamlgroup: DEMO_GROUPrefresh: true

测试结果

请添加图片描述

请添加图片描述

补充.刷新静态配置

有时候一些老项目或者一些写法会遇到静态的配置,这时候可以利用Java的反射特性来刷新静态变量.

大致原理为: 监听nacos配置改动,通过nacos改动确定改动的配置,进而缩小更新范围,通过反射更新变量.

<!-- https://mvnrepository.com/artifact/com.purgeteam/dynamic-config-spring-boot-starter -->
<dependency><groupId>com.purgeteam</groupId><artifactId>dynamic-config-spring-boot-starter</artifactId><version>0.1.1.RELEASE</version>
</dependency>
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId>
</dependency>

@NacosRefreshStaticField

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NacosRefreshStaticField {String configPrefix() default "";}

NacosListener

@Slf4j
@Component
@EnableDynamicConfigEvent
public class NacosListener implements ApplicationListener<ActionConfigEvent> {@Autowiredprivate ApplicationContext applicationContext;@SneakyThrows@Overridepublic void onApplicationEvent(ActionConfigEvent environment) {Map<String, HashMap> map = environment.getPropertyMap();for (Map.Entry<String, HashMap> entry : map.entrySet()) {String key = entry.getKey();Map changeMap = entry.getValue();String before = String.valueOf(changeMap.get("before"));String after = String.valueOf(changeMap.get("after"));log.info("配置[key:{}]被改变,改变前before:{},改变后after:{}",key,before,after);String[] configNameArr = key.split("\\.");String configPrefix = configNameArr[0];String configRealVal = configNameArr[configNameArr.length-1];AtomicReference<Class<?>> curClazz = new AtomicReference<>();Map<String, Object> refreshStaticFieldBeanMap = applicationContext.getBeansWithAnnotation(NacosRefreshStaticField.class);for (Map.Entry<String, Object> mapEntry : refreshStaticFieldBeanMap.entrySet()) {String beanName = mapEntry.getKey();if (ObjectUtil.isEmpty(beanName)) {continue;}String fullClassName = refreshStaticFieldBeanMap.get(beanName).toString().split("@")[0];Class<?> refreshStaticFieldClass;try {refreshStaticFieldClass = Class.forName(fullClassName);} catch (ClassNotFoundException e) {throw new ClassNotFoundException("监听nacos刷新当前静态类属性,未找到当前类",e);}NacosRefreshStaticField refreshStaticConfig = refreshStaticFieldClass.getAnnotation(NacosRefreshStaticField.class);if (Objects.nonNull(refreshStaticConfig) && refreshStaticConfig.configPrefix().equalsIgnoreCase(configPrefix)) {curClazz.set(refreshStaticFieldClass);}}Class<?> aClass = curClazz.get();if (Objects.isNull(aClass)) {return;}// 利用反射动态更新 静态变量Field[] declaredFields = aClass.getDeclaredFields();for (Field declaredField : declaredFields) {if (declaredField.getName().equalsIgnoreCase(configRealVal)) {log.info("刷新当前配置 更新当前类[{}] 静态属性 [{}]",aClass.getSimpleName(),declaredField.getName());declaredField.setAccessible(true);declaredField.set(null,after);}}}}
}

CommonWebConfig

@Data
@Component
@ConfigurationProperties(prefix = "common")
@RefreshScope
public class CommonWebConfig {private String apiUrl;}

使用

@Component
@NacosRefreshStaticField(configPrefix="common")
public class ExampleComponent {public static String apiUrl = SpringUtil.getBean(CommonWebConfig.class).getApiUrl();
}
http://www.yayakq.cn/news/237702/

相关文章:

  • 邯郸网站建设选哪家深圳外贸有限公司
  • 小白如何做网站建设公众号网站整站下载器 下载后台
  • 建个淘宝那样的网站需要多少钱wordpress没有页面模板
  • 邵阳建设银行网站是多少wordpress 面包屑导航修改
  • 做网站有什么用出网络广告策划书2000字
  • 安阳网站建设优化渠道艾瑞指数
  • 前端网页设计师海南搜索引擎优化
  • 沈阳微信网站制作营销型网站免费模板下载
  • flash制作网站top国外网站建设接单
  • 商城网站如何搭建上海专业优化排名工具
  • 网站建设文化机构定制开发一般多少钱
  • 电影天堂网站用什么程序做的做网站和推广工资多少
  • 泉州专业网站建设费用用wps网站栏目做树形结构图
  • 新塘做网站公司提高网站访问量
  • dw可以用来做网站吗建立良好的公共秩序教学反思
  • 咸阳做网站开发公司义县网站建设
  • 四川省建设厅职改办网站网站制作好公司
  • 专业推广网站秦皇岛网站制作人才招聘
  • 汽车报价网站建筑公司有哪些
  • 网站开发的缓存技术贵阳做网站的大公司
  • 山东网站建设排行榜仿虎嗅网 wordpress
  • 海淘网站佛山市手机网站建设
  • 电子商务毕业设计设计电商网站建设爱装网
  • 前程无忧怎么做网站重庆有没有做网站的
  • 什么网站可以做软件有哪些东西吗如何注册网上商城
  • 内江建设网站设计公司网站是什么是重要的
  • 量化交易网站开发wordpress商务版
  • 做新媒体国外网站网站开发与服务器匹配
  • 苏州高新区建设局网站上海工程咨询协会官网
  • 织梦网站建设实训心得软件定制开发费用云鲸互创信任