spring cloud 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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>config</name>
<description>config project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2.1.0.RELEASE</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<!-- 集成Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
#记住每个属性和值之间,要在冒号后面加一个空格
#指定启用端口
server:
port: 8767
eureka:
instance:
hostname: peer3
client:
registerWithEureka: false
fetchRegistry: true
serviceurl:
defaultZone: http://peer1:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: https://github.com/MichardChen/struts
searchPaths: WebContent
label: master
#指定git的uri,
uri是git仓库地址
searchPaths是指仓库路径,也就是配置文件目录
label是仓库分支
username访问git仓库的用户名
password访问git仓库用户名密码,如果仓库是公开仓库,username和password可以不写
Application
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
格式化json
{
"name": "webname",
"profiles": ["dev"],
"label": null,
"version": "f8eedc8a1f42654d1403b79d07b6d394dc52db4e",
"state": null,
"propertySources": [{
"name": "https://github.com/MichardChen/struts/WebContent/application-dev.properties",
"source": {
"server.port": "8080",
"foo": "99",
"webname": "spring"
}
}, {
"name": "https://github.com/MichardChen/struts/WebContent/application-dev.yml",
"source": {
"server.port": 8767,
"eureka.instance.hostname": "peer3",
"eureka.client.registerWithEureka": false,
"eureka.client.fetchRegistry": true,
"eureka.client.serviceurl.defaultZone": "http://peer1:8761/eureka/",
"spring.application.name": "config",
"spring.cloud.config.server.git.uri": "https://github.com/MichardChen/struts",
"spring.cloud.config.server.git.searchPaths": "WebContent"
}
}, {
"name": "https://github.com/MichardChen/struts/WebContent/application.yml",
"source": {
"server.port": 8767,
"eureka.instance.hostname": "peer3",
"eureka.client.registerWithEureka": false,
"eureka.client.fetchRegistry": true,
"eureka.client.serviceurl.defaultZone": "http://peer1:8761/eureka/",
"spring.application.name": "config",
"spring.cloud.config.server.git.uri": "https://github.com/MichardChen/struts",
"spring.cloud.config.server.git.searchPaths": "WebContent"
}
}]
}
我们发现读取了github对应的目录下的文件,
http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
===================================
创建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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>config-client</name>
<description>config project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring.cloud.version>2.1.0.RELEASE</spring.cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- eureka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
<!-- 集成Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
<version>${spring.cloud.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Application
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
bootstrap.yml
#记住每个属性和值之间,要在冒号后面加一个空格
#指定启用端口
server:
port: 8768
eureka:
instance:
hostname: peer3
client:
registerWithEureka: false
fetchRegistry: true
serviceurl:
defaultZone: http://peer1:8761/eureka/
spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
uri: http://localhost:8767/
IndexController.class
@RestController
@RequestMapping("/index")
public class IndexController {
@Value("${webname}")
String webname;
@RequestMapping("/test")
public String index(){
return webname;
}
}
访问/index/test,请求成功
使用springcloud bus实现自动更新配置,这里大概说下,我们使用kafka,server加入依赖。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-kafka</artifactId> <version>1.3.2.RELEASE</version> </dependency>
application.yml加入
#是否需要权限拉去,默认是true,如果不false就不允许你去拉取配置中心Server更新的内容 management: security: enabled: false
client需要加入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-kafka</artifactId> <version>1.3.2.RELEASE</version> </dependency>
@RestController
//这里面的属性有可能会更新的,git中的配置中心变化的话就要刷新,没有这个注解内,配置就不能及时更新
@RefreshScope
public class TestController {
@Value("${name}")
private String name;
@Value("${age}")
private Integer age;
@RequestMapping("/test")
public String test(){
return this.name+this.age;
}
}
这个使用kafka,实现自动更新的,可以参考https://www.cnblogs.com/huangjuncong/p/9077099.html