Springboot 配置中心Spring Cloud Config

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,所以需要分布式配置中心组件。

服务端

服务端作用简单理解就是从git上拉取相应配合。

新建项目

pom.xml

<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.fymod</groupId>
	<artifactId>springcloud-config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springcloud-config-server</name>
	<description>springcloud-config-server</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.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>Edgware.SR2</spring-cloud.version>
	</properties>

	<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>
	</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>


</project>

配置文件

application.yml

spring:
  application:
    name: config-server
  cloud:
    config:
      server: 
        git: 
          uri: https://github.com/keith002/springcloud-eureka/
          search-paths: config
          username: 
          password: 
server:
  port: 8081

其中,uri是我github上对应项目的地址,search-paths是路径(就是文件夹位置),username和password是用户名和密码,公开的话可以不写。然后对于上面配置好的地址和文件,上传到github上,我上传的文件只有一行代码(config-dev.properties)

name=Keith

Java代码

给启动类添加一个注解即可:@EnableConfigServer

运行

Run As运行后,地址栏输入http://localhost:8770/config/dev

{
	"name": "config",
	"profiles": ["dev"],
	"label": null,
	"version": "910fd18a674a4b1eae042b1531cf243040824617",
	"state": null,
	"propertySources": [{
		"name": "https://github.com/keith002/springcloud-eureka/config/config-dev.properties",
		"source": {
			"name": "Keith"
		}
	}]
}
如果打印基本信息则表示启动正常。
http请求地址:/{application}/{profile}[/{label}]
资源文件:
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
示例:本例中资源文件是config-dev.properties,其中config就是application,dev就是profile,匹配资源文件的第三种,访问地址就是/config(application)/dev(profile)

客户端

客户端简单理解就是从服务端获取配置信息。

新建项目

pom.xml

<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.csdn</groupId>
	<artifactId>springboot-config</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>
	<!-- 非继承方式引入maven依赖 -->
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Dalston.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<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>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-client</artifactId>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

配置文件

客户端默认优先加载bootstrap.properties,即便自己写了其他配置文件,也会加载默认的bootstrap配置文件,不写的话会提示8888端口这种错误。本例配置文件直接使用bootstrap.properties

spring.application.name=config
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8081/
server.port=8082

spring.cloud.config.label 指明远程仓库的分支
spring.cloud.config.profile
dev开发环境配置文件
test测试环境
pro正式环境
spring.cloud.config.uri= http://localhost:8770/ 指明配置服务中心的网址。

Java代码

新建测试类TestController.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @Value("${name}")
    String name;
	
    @RequestMapping(value = "/hi")
    public String hi(){
        return name;
    }
    
}

运行

Run As运行后,地址栏输入http://localhost:8771/hi ,如果打印出了github上配置文件对应value则成功

转载:https://www.kancloud.cn/fymod/springcloud/539175

猜你喜欢

转载自blog.csdn.net/Keith003/article/details/82256179