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 boot的application.properties配置可以放在apollo上么? #577

Closed
blue-ali opened this issue Mar 30, 2017 · 11 comments
Closed

Comments

@blue-ali
Copy link

原来spring cloud的工程application.properties(yml)是通过cloud config server加载的,apollo能这样加载cloud的配置么?比如eureka,feign,ribbon的一些配置

@nobodyiam
Copy link
Member

nobodyiam commented Mar 30, 2017

可以的,apollo注入配置的原理和spring cloud config一样的。参考Java客户端使用指南即可。

补充一点,目前注入的配置需要时properties格式,yml格式还不支持。

@blue-ali
Copy link
Author

谢谢,在使用过程中还碰到一些问题想问
我自己写了一个demo,加载不到配置,在启动的时候会有WARN
2017-03-30 10:30:28.156 WARN 305728 --- [ main] c.c.f.a.i.AbstractConfigRepository : Sync config failed, will retry. Repository class com.ctrip.framework.apollo.internals.RemoteConfigRepository, reason: Get config services failed from http://localhost:8080 -Dfat_meta=http://localhost:8080 -Duat_meta=http://localhost:8080 -Dpro_meta=http://localhost:8080/services/config?appId=ApolloTest&ip=192.168.11.10 [Cause: Could not complete get operation [Cause: For input string: "8080 -Dfat_meta=http:" [Cause: For input string: "8080 -Dfat_meta=http:"]]]

我想可能是不是我build的时候出的问题,因为在win环境开发,我把build.sh改成了build.bat,内容如下
`:: apollo config db info
set apollo_config_db_url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8
set apollo_config_db_username=root
set apollo_config_db_password=000000

:: apollo portal db info
set apollo_portal_db_url=jdbc:mysql://localhost:3306/ApolloPortalDB?characterEncoding=utf8
set apollo_portal_db_username=root
set apollo_portal_db_password=000000

:: meta server url
set dev_meta=http://localhost:8080
set fat_meta=http://localhost:8080
set uat_meta=http://localhost:8080
set pro_meta=http://localhost:8080

set META_SERVERS_OPTS="-Ddev_meta=%dev_meta% -Dfat_meta=%fat_meta% -Duat_meta=%uat_meta% -Dpro_meta=%pro_meta%"

:: # =============== Please do not modify the following content =============== #
echo %META_SERVERS_OPTS%
pause
cd ..

rem package config-service and admin-service
echo "==== starting to build config-service and admin-service ===="

call mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=dev -Dspring_datasource_url=%apollo_config_db_url% -Dspring_datasource_username=%apollo_config_db_username% -Dspring_datasource_password=%apollo_config_db_password%

echo "==== building config-service and admin-service finished ===="
pause
echo "==== starting to build portal ===="

call mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=dev -Dspring_datasource_url=%apollo_portal_db_url% -Dspring_datasource_username=%apollo_portal_db_username% -Dspring_datasource_password=%apollo_portal_db_password% %META_SERVERS_OPTS%

echo "==== building portal finished ===="
pause
echo "==== starting to build client ===="

call mvn clean install -DskipTests -pl apollo-client -am %META_SERVERS_OPTS%

echo "==== building client finished ===="
pause`

看起来问题似乎出在set META_SERVERS_OPTS上?这里应该怎么改?
这个meta地址指的就是configservice的地址还是configservice里eureka的地址?如果我想把服务注册到自己的eureka上可以么?
还有打包只能通过这种脚本的方式打包么?

@nobodyiam
Copy link
Member

看上去是脚本有点问题。。你如果打开apollo-core.jar里面的apollo-env.properties就知道了。

建议还是装一个Git Bash,然后跑build.sh吧。

@blue-ali
Copy link
Author

谢谢回答,我用git bash build后,在eclipse里通过vm参数
-Dapollo_profile=dev -Dspring.datasource.url=jdbc:mysql://localhost:3306/ApolloConfigDB?characterEncoding=utf8 -Dspring.datasource.username=root -Dspring.datasource.password=000000 -Dlogging.file=d:/logs/apollo-assembly.log
这样启动没有问题,但是用java -jar 加上vm参数的方式启动会报错
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: NO)
似乎是vm参数没有加载到?改用target的zip包里的startup.sh启动还是一样的问题,我又修改了工程里的application.yml加上了datasource的配置但是还是不管用,这个数据库的配置应该放在哪里加载?感觉自己已经完全懵逼了。。

@nobodyiam
Copy link
Member

build.sh里面有数据库配置的,你照着做就没问题啊。这部分配置实际上是会替换apollo-common下的application-dev.properties,你打完包看一下就明白了。

vm参数没有加载到的问题,这个可以在启动过程中看一下进程参数,如Linux下是ps aux | grep java,Windows不太了解。。。

@blue-ali
Copy link
Author

谢谢,已经解决了,我build.sh里配置的问题。
我还有点疑问,为什么数据库这些配置不放在application.yml里,而是在build的时候用参数打到包里?感觉参数配置这有点乱,像eureka的地址配置是在数据库里的,而configservice工程里的bootstrap里也配了,但是好像没起作用,最后读取的还是数据库里的地址。

@nobodyiam
Copy link
Member

数据库配置是放在application-dev.properties,和application.yml是一个道理。build的时候传入参数最终就是写入了application-dev.properties。至于为啥不直接写在文件中很好理解吧,一个是因为每个环境不一样,一个是开源代码不能写啊。

eureka的配置因为会动态调整的,所以文件只能是一个fallback,最终还是要以数据库为准的。

@blue-ali
Copy link
Author

十分感谢~

@wzxit
Copy link

wzxit commented May 17, 2017

application.properties中使用,如:spring.datasource.url: ${someKeyFromApollo:someDefaultValue}
缺点是这类注入的值在变化后不会重新注入,需要重启才会更新。

这是要重新启动实例才能加载吗?

@wzxit
Copy link

wzxit commented May 17, 2017

还有,有没有spring boot 使用apollo的详细点的例子呢?

@nobodyiam
Copy link
Member

spring placeholder在运行时重新注入有几种方式

  1. 重启
  2. 监听config change listener手动重新注入
  3. 使用RefreshScope

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

3 participants