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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27828675/article/details/83503364

注意:这里用到的项目都是在之前几篇文章讲解用到的项目工程基础上进行的,在这一系列博客写完后会提供源码地址。

项目源码及相关说明请查看此文:Spring Cloud入门教程-简介

这里讲解一下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”。

猜你喜欢

转载自blog.csdn.net/qq_27828675/article/details/83503364