SpringCloud的@RefreshScope 注解你了解吗

网友投稿 243 2022-12-10

SpringCloud的@RefreshScope 注解你了解吗

目录pom.xmlproperties启动类配置类controller打包springcloud对应的springboot版本参考:总结

spring-boot-starter-actuator提供服务健康检查和暴露内置的url接口。

spring-cloud-starter-config提供动态刷新的一些支持和注解。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.4.6

com.xiaobu

demo-for-mybatis-plus

0.0.1-SNAPSHOT

demo-for-mybatis-plus

demo-for-mybatis-plus

1.8

2020.0.3

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

asm

org.ow2.asm

com.baomidou

mybatis-plus-boot-starter

3.4.2

org.projectlombok

lombok

1.16.10

cn.hutool

hutool-all

5.3.2

io.springfox

springfox-swagger2

2.9.2

guava

com.google.guava

io.springfox

springfox-swagger-ui

2.9.2

com.google.guava

guava

29.0-jre

com.alibaba

easyexcel

2.0.2

junit

junit

com.xuxueli

xxl-job-core

2.3.0

mysql

mysql-connector-java

org.springframework.cloud

spring-cloud-starter-config

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-bootstrap

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

src/main/resources

src/main/java

**/*.xml

true

App

org.springframework.boot

spring-boot-maven-plugin

2.4.5

http://

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.4.6

com.xiaobu

demo-for-mybatis-plus

0.0.1-SNAPSHOT

demo-for-mybatis-plus

demo-for-mybatis-plus

1.8

2020.0.3

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

asm

org.ow2.asm

com.baomidou

mybatis-plus-boot-starter

3.4.2

org.projectlombok

lombok

1.16.10

cn.hutool

hutool-all

5.3.2

io.springfox

springfox-swagger2

2.9.2

guava

com.google.guava

io.springfox

springfox-swagger-ui

2.9.2

com.google.guava

guava

29.0-jre

com.alibaba

easyexcel

2.0.2

junit

junit

com.xuxueli

xxl-job-core

2.3.0

mysql

mysql-connector-java

org.springframework.cloud

spring-cloud-starter-config

org.springframework.boot

spring-boot-starter-actuator

org.springframework.cloud

spring-cloud-starter-bootstrap

org.springframework.cloud

spring-cloud-dependencies

${spring-cloud.version}

pom

import

src/main/resources

src/main/java

**/*.xml

true

App

org.springframework.boot

spring-boot-maven-plugin

2.4.5

http://

properties

########## Mybatis 自身配置 ##########

logging.level.com.xiaobu=debug

mybatis-plus.type-aliases-package=com.xiaobu.entity

mybatis-plus.mapper-locations=classpath:com/xiaobu/mapper/xml/*.xml

# 控制台打印sql 带参数 无法写入文件

#mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

# 将sql 写入文件 带参数

mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.slf4j.Slf4jImpl

#集成mysql数据库的配置

spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/master0?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai

spring.datasource.username=root

spring.datasource.password=root

#测试动态刷新配置

order.pay-timeout-seconds=9999

order.create-frequency-seconds=600

#暴露内置的刷新配置文件url,这个必须写,否则无法刷新配置文件

management.endpoints.web.exposure.include=refresh

#management.endpoints.web.exposure.include=env,refresh#management.endpoints.web.exposure.include=env,refresh

启动类

package com.xiaobu;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.context.properties.ConfigurationPropertiesScan;

/**

* @author 小布

*/

@SpringBootApplication

@ConfigurationPropertiesScan

public class DemoForMybatisPlusApplication {

public static void main(String[] args) {

SpringApplication.run(DemoForMybatisPlusApplication.class, args);

}

}

配置类

package com.xiaobu.config;

import lombok.Data;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.cloud.context.config.annotation.RefreshScope;

import org.springframework.stereotype.Component;

/**

* @author 小布

*/

@Component

@ConfigurationProperties(prefix = "order")

@RefreshScope

@Data

public class OrderProperties {

/**

* 订单支付超时时长,单位:秒。

*/

private Integer payTimeoutSeconds;

/**

* 订单创建频率,单位:秒

*/

private Integer createFrequencySeconds;

}

controller

package com.xiaobu.controller;

import com.xiaobu.config.OrderProperties;

import org.springframework.beans.factory.annotation.Autowired;

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.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* The type Refresh controller.

*

* @author 小布

* @version 1.0.0

* @className RefreshController.java

* @createTime 2021年09月06日 15:38:00

*/

@RestController

@RequestMapping("refresh")

@RefreshScope

public class RefreshController {

@Autowired

private OrderProperties orderProperties;

@Value(value = "${order.pay-timeout-seconds}")

private Integer payTimeoutSeconds;

/**

* Test string.

*

* @return the string

*/

@GetMapping("test")

public String test() {

return "payTimeoutSeconds:" + payTimeoutSeconds;

}

@GetMapping("test01")

public String test01() {

return orderProperties.toString();

}

}

打包

执行

mvn clean package -Dmaven.test.skip=true

cmd启动jar 并指定外部配置文件

java -jar App.jar --spring.config.location=D:/application.properties

访问:http://localhost:8080/refresh/test

修改配置文件内容:

执行 POST http://localhost:8080/actuator/refresh

再次访问:http://localhost:8080/refresh/test

访问:http://localhost:8080/refresh/test01

springcloud对应的springboot版本

参考:

springcloud对应的springboot版本

Springboot 使用@RefreshScope 注解,实现配置文件的动态加载

Spring boot 应用实现动态刷新配置

Spring Boot 指定外部启动配置文件

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注我们的更多内容!

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:详解如何把cmd黑窗口把java文件打包成jar
下一篇:springboot获取profile的操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~