Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spring-cloud-config #2

Open
zhuzhuhong opened this issue Dec 3, 2017 · 10 comments
Open

spring-cloud-config #2

zhuzhuhong opened this issue Dec 3, 2017 · 10 comments

Comments

@zhuzhuhong
Copy link

你好,我看了你写的《Spring Cloud与Docker微服务架构实战》,其中关于config server 这一块有些疑惑,到网上也没找到合适的答案,所以冒昧的来请教您:是否是所有的配置项都可以从config server来获取,譬如说数据库的连接信息,是否需要自己获取这些配置内容再来手动创建相应的数据库连接,还是有什么方法可以自动实现,谢谢

@eacdy
Copy link
Contributor

eacdy commented Dec 3, 2017

是否是所有的配置项都可以从config server来获取

可以的。

是否需要自己获取这些配置内容再来手动创建相应的数据库连接,还是有什么方法可以自动实现,谢谢

自动与否得看情况。引入spring-cloud-config-client后,就会有两个上下文,引导上下文bootstrap context以及applicationcontext。启动时,bootstrap context负责获取远程的配置文件属性。
对于数据源,spring boot会自动创建,只要你有相关的starter;而如果你没有相关的starter,那就得自己new了。
说白了,引入config的作用其实就是将嵌入在jar包中的配置实现集中化的远程管理。其他的原先怎么样现在还是怎么样。

@zhuzhuhong
Copy link
Author

不好意思,又来打扰了,可能是我有些地方理解得还不是很透彻,不知道是否可以写一篇关于从config-server获取数据库配置信息并依据该配置信息实现hibernate对数据库操作,或者把关键的配置信息贴出来,谢谢

@eacdy
Copy link
Contributor

eacdy commented Dec 6, 2017

  1. 微服务集成config-client
  2. git仓库里存数据库url、账号、密码等信息;
  3. 自动拿。
  4. 如果你愿意,可以加密。

跟我开源书里用法一样。

@wsh231314
Copy link

貌似Config client 在启动的时候报错,
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'miss-lili' in string value "${miss-lili}"

网上查了不少资料,好像是bootstrap的启动顺序不太对

@eacdy
Copy link
Contributor

eacdy commented Apr 7, 2018

从报错来看,是应用没有能够正常从git仓库拿到配置文件。
可能的原因有很多:

  1. 应用的spring.application.name 和git仓库里包含miss-lili属性的的配置文件名称不匹配;
  2. config server无法正常连接上git仓库。建议在启动config client微服务之前用config server的REST端点测试并确认。
  3. config client连接的config server地址不对。例如:将应用名称、config server地址等等配置在了application.* 中,而非bootstrap.* 等。

@mrsWangxing
Copy link

对于config这块一直也有一个小疑问,这个模块有个/refresh 手动刷新的功能,它的可以动态刷新配置,但是类似线程池的配置 项目启动时候就初始化了 后期如果想修改线程池大小,这个手动刷新是不是也不能直接完成修改线程池大小 还得重启程序。
PS:网上有些博客写的给我的感觉 只要refresh了 什么配置都能修改 不用重启项目,如果不用重启项目,那config这个组件是如何实现动态修改线程池类似的配置呢?

@eacdy
Copy link
Contributor

eacdy commented Sep 20, 2018

@mrsWangxing 刷新是有前置要求的

  1. 要刷新的属性所在的类上得有@RefreshScope,否则无法刷新
  2. 应用得继承config-cilent以及spring-boot-starter-actuator,否则没有refresh端点
    刷新的本质,其实就是调用了这行代码,有兴趣看看:org.springframework.cloud.context.refresh.ContextRefresher#refresh ,网上的文章可以看,不过看完文章,然后就靠感觉判断xxx,我觉得不靠谱。还是做实验去验证自己的感觉比较OK。

@mrsWangxing
Copy link

比如线程池那样的 服务器启动 直接加载初始化的 对象 加上 @RefreshScope等前置要求 不启动服务器也可以实现 刷新吗 ?之前还真没试过。

@eacdy
Copy link
Contributor

eacdy commented Sep 20, 2018

@mrsWangxing 这是spring cloud提供的注解。不一定要有web容器(Tomcat),只要你的bean是个spring管理的对象。

@mrsWangxing
Copy link

OK 懂了 谢谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants