1.介绍
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。很容易添加替代实现,并使用Spring配置将其插入。
2.引入pom相关jar包,其中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.ml.honghu</groupId>
<artifactId>commonservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>commonservice-config</artifactId>
<packaging>jar</packaging>
<name>commonservice-config</name>
<description>Config Server</description>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</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>
<executions>
<execution>
<id>1</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
<execution>
<id>2</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.ml.honghu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableConfigServer
@EnableEurekaClient
@SpringBootApplication
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
server:
port: 8888
spring:
application:
name: commonservice-config-server
profiles:
active: discovery,native
cloud:
config:
server:
git:
uri: http://192.168.0.254/honghu.../honghu-config.git
username: honghu
password: 123456
searchPaths: config-dev
security:
basic:
enabled: true
user:
name: honghu
password: 123456
eureka:
client:
serviceUrl:
defaultZone: http://honghu:123456@localhost:8761/eureka/
honghuZone: http://honghu:123456@localhost:8761/eureka/
registry-fetch-interval-seconds: 300
availability-zones:
honghu: honghuZone
instance:
prefer-ip-address: true
metadataMap:
version: 1.0
variant: A
user: ${security.user.name}
password: ${security.user.password}
management:
security:
enabled: false
注意: 如果不从远程git或者svn库加载配置文件信息,可以配置加载本地地址,比如window下配置使用:
8888
spring:
application:
name: commonservice-config-server
profiles:
active: discovery,native
cloud:
config:
server:
<span >native.searchLocations: d:/honghu-config</span>
security:
basic:
enabled: true
user:
name: honghu
password: 123456
eureka:
client:
serviceUrl:
defaultZone: http://honghu:123456@localhost:8761/eureka/
honghuZone: http://honghu:123456@localhost:8761/eureka/
registry-fetch-interval-seconds: 300
availability-zones:
honghu: honghuZone
instance:
prefer-ip-address: true
metadataMap:
version: 1.0
variant: A
user: ${security.user.name}
password: ${security.user.password}
management:
security:
enabled: false
完整项目的源码来源 技术支持1791743380
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。