https://github.com/unnunique/SpringCloundDakaLearning/tree/master/Chapter-6
首先对于任何一个应用,所有随着部署环境改变而要改变的配置,都不应该写死在代码里面。
也就是说,代码中的配置,要统一放到一个文件中管理。
而spring-clound 应用中,由于一个应用,可能对应着多个微服务,那么,每个微服务都有自己的配置文件。
有些配置文件是可以共用的,有些则是单个服务的特殊需求。
由此,对于这些配置文件的处理,Spring-Clound 有多种方式实现应用配置的统一管理。
注意配置中心的jar 包依赖如下:
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-serverartifactId>
dependency>
完整pom 依赖如下:
Chapter-6
com.sydney.dream.springclound
1.0.0
4.0.0
config
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
#spring.cloud.config.server.git.uri:配置git仓库地址
#spring.cloud.config.server.git.searchPaths:配置仓库路径
#spring.cloud.config.label:配置仓库的分支
#spring.cloud.config.server.git.username:访问git仓库的用户名, 公有仓库,可以不填
#spring.cloud.config.server.git.password:访问git仓库的用户密码, 公有仓库,可以不填
spring.application.name=config-server
server.port=8888
spring.cloud.config.server.git.uri=https://github.com/unnunique/SpringCloundDakaLearning
spring.cloud.config.server.git.searchPaths=Chapter-6/config-center
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
注意添加 @EnableConfigServer 注解
package com.sydney.dream.chapter6.web;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
*
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigApp {
public static void main(String[] args) {
SpringApplication.run(ConfigApp.class, args);
}
}
<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">
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.1.4.RELEASEversion>
<relativePath/>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>chapter-6-clientartifactId>
<properties>
<java.version>1.8java.version>
<spring-cloud.version>Greenwich.SR1spring-cloud.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-config-clientartifactId>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${spring-cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
spring.application.name=app1
spring.cloud.config.label=master
spring.cloud.config.profile=dev
spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.request-read-timeout=6000000
server.port=8881
package com.sydney.dream.chapter6.client;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ClientApp {
public static void main(String[] args) {
SpringApplication.run(ClientApp.class, args);
}
}
Controller
${demokey}即为配置在远程上课key的值
package com.sydney.dream.chapter6.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HiController {
@Value("${demokey}")
String demokey;
@RequestMapping(value = "/hi")
public String hi(){
return demokey;
}
}
访问如下链接:
http://localhost:8881/hi
得到的响应如下:
demokey-dev
有可能6666 端口被占用, 重新换个端口即可。
1, 可能 propeties 配置重复。
2, 可能pom 文件间依赖问题,
本文是pom 文件间依赖的问题。
2019-04-28 20:33:44.317 INFO 1804 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2019-04-28 20:33:47.047 INFO 1804 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=app1, profiles=[dev], label=master, version=aa44f4c407746a727385dad53d5efc71b0e71245, state=null
2019-04-28 20:33:47.048 INFO 1804 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='configService', propertySources=[MapPropertySource {name='configClient'}, MapPropertySource {name='https://github.com/unnunique/SpringCloundDakaLearning/Chapter-6/config-center/app1-dev.properties'}]}
2019-04-28 20:33:47.057 INFO 1804 --- [ main] c.s.dream.chapter6.client.ClientApp : No active profile set, falling back to default profiles: default
2019-04-28 20:33:48.026 INFO 1804 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=abbe48a3-433d-3930-a2f4-54bba3574488
2019-04-28 20:33:48.045 INFO 1804 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$70437cb9] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2019-04-28 20:33:49.491 INFO 1804 --- [ main] c.s.dream.chapter6.client.ClientApp : Started ClientApp in 8.945 seconds (JVM running for 9.70
问题: 父 pom 文件依赖如下:
parent
com.sydney.dream.springclound
1.0.0
4.0.0
Chapter-6
pom
Client
Config
1.8
Greenwich.SR1
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
子 pom 1 配置文件:
Chapter-6
com.sydney.dream.springclound
1.0.0
4.0.0
config
子pom 2,
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
4.0.0
chapter-6-client
1.8
Greenwich.SR1
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-actuator
org.springframework.cloud
spring-cloud-config-client
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
比如提示找不到demokey, cannot find “${demoke}”
可能问题,两边的依赖的spring-boot-parent 不一致。
management.endpoints.web.exposure.include=refresh
@RefreshScope
package com.sydney.dream.chapter6.client;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RefreshScope
public class HiController {
@Value("${demokey}")
String demokey;
@RequestMapping(value = "/hi")
public String hi(){
return demokey;
}
}
注意是post 请求
curl -X POST http://localhost:8881/actuator/refresh
http://localhost:8881/hi
结果:
demokey-dev-test-reflesh1