spring-cloud项目 - 2 - 远程配置

Spring Cloud Config

配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion、码云。

也就是说,我们可以使用Spring Cloud Config来获取远程服务器上的配置信息。

可以分为两个部分:

服务端: 配置服务端,服务管理配置信息
客户端:客户端调用server端暴露接口获取配置信息

配置服务器端,
1、创建一个maven model
2、pom:
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
你没看错,仅需要一个jar
3、启动类:
@SpringBootApplication
@EnableConfigServer
public class ConfigBootStrap {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    SpringApplication.run(ConfigBootStrap.class, args);
}

}
4、application.yml
server:
port: 7001

git管理配置

spring:
cloud:
config:
server:
git:
uri: xxxxxxxxxx #git仓库地址
searchPaths: application* #搜索路径
username: xxx
password: xxxx
application:
name: xxxxxx
5、启动服务,访问


image.png

说明远程配置已经拿到;
客户端配置:
pom:
<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.saas</groupId>
<artifactId>my-saas</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>my-saas-admin</artifactId>
<dependencies>

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


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

</dependencies>
</project>
bootstrap.yml:
PS:记住一定要使用”bootstrap.yml,存在一个加载先后顺序的问题使用application很有可能报错”
server:
port: 8091

spring:
application:
name: my-saas-admin
cloud:
config:
label: master
profile: dev
uri: http://127.0.0.1:8092/

注册中心指向start

eureka:
instance:
instance-id: my-saas-admin
appname: ${spring.application.name}
client:

注册中心

service-url: 
  defaultZone: http://127.0.0.1:8090/eureka/

启动类:
@SpringBootApplication
@EnableEurekaClient //Eureka Client
@EnableDiscoveryClient
public class AdminBootStrap {
public static void main(String[] args) {
// TODO Auto-generated method stub
new SpringApplicationBuilder(AdminBootStrap.class).web(true).run(args);
}

}
加入@EnableDiscoveryClient
配置中心也需要加@EnableDiscoveryClient。

远程配置完成!

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容