Spring Cloud入门教程-配置中心 Config

Config Server 从本地读取配置文件

Config Server可以从本地仓库读取配置文件,也可以从远处Git仓库读取。本地仓库是指将所有的配置文件统一写在 Config Server工程目录下 Config Sever暴露 Http apI接口, Config Client通过调用 Config Sever的 Http Api接口来读取配置文件。

创建新Module config-server ,pom.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.springcloud.demo</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>
 
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--构建高可用的Config Server 将config server 作为eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

在ConfigServerApplication 上添加注解 @EnableConfigServer 。

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

添加配置

server.port=8799
spring.application.name=config-server
 
#从本地读取配置文件####################################################
spring.profiles.active=native
spring.cloud.config.server.native.search-locations=classpath:/shared
#####################################################################

在resource 下创建文件夹shared,创建文件 config-client-dev.properties,并添加如下配置:

server.port=8700
application.name=config-server-v1

创建新Module config-client ,pom.xml 如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.springcloud.demo</groupId>
		<artifactId>springcloud-demo</artifactId>
		<version>1.0-SNAPSHOT</version>
	</parent>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
 
	<name>config-client</name>
	<description>Demo project for Spring Boot</description>
 
 
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
 
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
 
 
		
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
 
 
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
 
 
</project>

创建bootstrap.porperties添加配置:

server.port=8798
spring.application.name=config-client
 
 
spring.profiles.active=dev
spring.cloud.config.uri=http://localhost:8797
spring.cloud.config.fail-fast=true
 

注意这里用的是bootstrap.porperties,不是application.perproties,bootstrap 具有优先执行顺序。
{spring.application.name} 和{spring.profiles.active} 与“-”相连构成了向 Config Server 读取的文件名。这里也就是config-server shared文件夹中的config-client-dev.properties 文件。
为了进一步验证,编写一个controller 获取{application.name}。

@RefreshScope
@RestController
public class MainController {
    @Value("${application.name}")
    private String name;
 
    @GetMapping("main")
    public String main() {
        return name;
    }
}

启动 config-server 和config-client.

查看打印日志,config-client端口变为8700,
在这里插入图片描述
请求http://localhost:8700/main 返回 “config-server-v1”。


版权声明:本文为CSDN博主「天青色等烟雨11」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_27828675/article/details/83503364

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值