加载思路:
创建configmap,挂载配置文件为application.yml,最好指定profile
加载bootstrap配置 -> 指定profile -> 寻找对应profile挂载的文件 -> pooling or event
bash
复制代码
## 接入 kubernetes configmap implementation ('org.springframework.cloud:spring-cloud-starter-kubernetes-client-config:2.1.4') ## 热更新 implementation("org.springframework.boot:spring-boot-actuator") implementation("org.springframework.boot:spring-boot-actuator-autoconfigure")
根目录下新增bootstrap.yml文件
yaml
复制代码
server: port: 8080 spring: application: name: server cloud: kubernetes: enabled: false
本地开发时,指定spring.cloud.kubernetes.enabled=false,关闭kubernetes配置
修改dist目录下的各个配置文件为bootstrap.yml,同时修改配置,以test环境为例
yaml
复制代码
server: port: 8080 management: endpoint: restart: enabled: true health: enabled: true info: enabled: true spring: application: name: server cloud: kubernetes: reload: enabled: true mode: event config: sources: - name: demo-configmap namespace: test profiles: active: k8s-test
kubectl create configmap demo-configmap --from-file=application.yml -n test
yaml
复制代码
server: port: 8080 # servlet.context-path: /${spring.application.name} spring: profiles: k8s-test mvc: pathmatch: matching-strategy: ant_path_matcher application: name: server cloud: kubernetes: config: sources: - name: demo-comfigmap namespace: test reload: enabled: true mode: event logging: config: classpath:logback.xml endpoint: restart: enabled: true health: enabled: true info: enabled: true
创建Role
vbnet
复制代码
apiVersion: rbac.authorization.k8s.io/v1 kind: Role metadata: namespace: test name: config-reader rules: - apiGroups: [""] resources: ["pods","configmaps","services","endpoints"] verbs: ["get", "watch", "list"]
创建ServiceAccount
yaml
复制代码
apiVersion: v1 kind: ServiceAccount metadata: name: config-reader namespace: test
绑定Role和ServiceAccount
yaml
复制代码
apiVersion: rbac.authorization.k8s.io/v1 kind: RoleBinding metadata: name: config-reader namespace: test roleRef: apiGroup: rbac.authorization.k8s.io kind: Role name: config-reader subjects: - kind: ServiceAccount name: config-reader namespace: test
arduino
复制代码
serviceAccountName: config-reader serviceAccount: config-reader
为什么使用bootstrap.yml,参考:www.jianshu.com/p/c955c44ae…
即系统级别的配置信息放在bootstrap.yml中,自定义的配置信息放在application.yml中。
系统级别以及不可变的配置放在项目代码中,不做更改
可变的、自定义的配置放在configmap中做热更新