Spring Cloud Config获取配置信息

Spring Cloud Config配置信息是从git上面获取的,本文提供四个配置文件

https://gitee.com/sgqing/SpringCloudConfig.git 

原理:

  在分布式系统中,每一个功能都可以拆分成不一个独立的服务,一次请求的完成,可能会使用到多个服务的组件来完成,为了方便服务配置文件的统一管理,更易于部署,维护,所以就需要用到分布式配置中心的组件,在Spring Colud中,有分布式配置中心组件Spring Cloud config ,它支持配置文件放在服务的内存中,也支持放在远程的git仓库中,在引入Spring Colud Config之后,我们的外部的配置文件就可以集中放置在一个Git仓库里,在新建一个Config Server 用来管理所有的配置文件,维护的时候需要更改配置时,只需要在本地修改完成之后,推送到远程仓库,所有的服务实例都可以通过 Config Server来获取配置文件的,这时每个服务实例就相当于配置服务的客户端,Config client 为了保证系统的稳定,配置服务端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>

	<groupId>com.springCloud.client</groupId>
	<artifactId>democlient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>democlient</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR1</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <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>
	</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>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

在application.yml中配置连接到git的配置,

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8762
spring:
  application:
    name: server-hi
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/sgqing/SpringCloudConfig.git
#          配置文件所在的路径
          search-paths: /**
#          设置分支
      label: master
      username: sgqing
      password: *******
#服务商的git仓库地址


设置git连接地址,分支用户名称、密码

在启动类上面加上 @EnableConfigServer的注解

package com.springcloud.client.democlient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
//表示是Eureka的消费方
@EnableEurekaClient
@RestController
@EnableAutoConfiguration
//表示开启ConfigService去获取文件的配置信息
@EnableConfigServer
public class DemoclientApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoclientApplication.class, args);
    }
    //  用于获取 配置文件中的配置数据
    @Value("${server.port}")
    private String port;
    @RequestMapping("/hi")
    public String home(@RequestParam String name) {
        return "hi"+name+".."+"port";
    }



}

因为在文章开始就说明,Spring Cloud提供的Config Server 来进行配置文件的获取,所以在启动类上面加@EnableConfigServer用于加载git仓库中的配置文件

  测试:

猜你喜欢

转载自blog.csdn.net/weixin_38297879/article/details/81565476