Python 官方文档:入门教程 => 点击学习
目录pom.xmlproperties启动类配置类controller打包SpringCloud对应的SpringBoot版本参考:总结 spring-boot-starter-ac
spring-boot-starter-actuator提供服务健康检查和暴露内置的url接口。
spring-cloud-starter-config提供动态刷新的一些支持和注解。
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance" xmlns="http://Maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiaobu</groupId>
<artifactId>demo-for-mybatis-plus</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo-for-mybatis-plus</name>
<description>demo-for-mybatis-plus</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<!--Spring Boot-->
<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>
<exclusions>
<exclusion>
<artifactId>asm</artifactId>
<groupId>org.ow2.asm</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<!-- lomback -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.2</version>
</dependency>
<!-- 引入swagger2依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.Google.guava</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>com.xuxueli</groupId>
<artifactId>xxl-job-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>Mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- spring-cloud config-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- sprinGCloud 高版本需要引入 spring-cloud-starter-bootstrap 否则刷新不起效-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**
@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;
@Component
@ConfigurationProperties(prefix = "order")
@RefreshScope
@Data
public class OrderProperties {
private Integer payTimeoutSeconds;
private Integer createFrequencySeconds;
}
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;
@RestController
@RequestMapping("refresh")
@RefreshScope
public class RefreshController {
@Autowired
private OrderProperties orderProperties;
@Value(value = "${order.pay-timeout-seconds}")
private Integer payTimeoutSeconds;
@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版本
Spring boot 应用实现动态刷新配置
Spring Boot 指定外部启动配置文件
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注编程网的更多内容!
--结束END--
本文标题: SpringCloud的@RefreshScope 注解你了解吗
本文链接: https://lsjlt.com/news/135137.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0