springcloud微服务-config

配置项分类:

1.静态配置

  1)环境配置 Eureka配置中心地址,数据库连接等

  2)安全配置 公钥私钥,密码等

2.动态配置

  1)功能配置 功能开关,人工熔断开关,蓝绿发布等

  2)业务规则 动态文案,当日外汇利率等

  3)应用参数 网关黑白名单,缓存过期时间等

 

配置管理的要求:

  1.高可用

  2.版本管理

  3.业务需求  如动态推送,内容加密等

  4.配置分离,中心化管理

 

config-server工作流程:

1.在github上创建配置文件,注意文件命名规则application-profile.suffix

2.创建config-server,指定依赖jar包,配置git路径,search-paths,username,password

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/hongzuiliyu91/config.git
username:
password:
search-paths: '{application}'
force-pull: true
overrides:
test: mytest

#开启所有actuator-endpoint
management:
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
security:
enabled: false

eureka:
client:
serviceUrl:
defaultZone: http://localhost:20000/eureka/
server:
port: 20003

3.config-consumer端引入依赖,添加config配置,需要放在bootstrap.yml中,读取相应配置文件,需要动态配置的地方加上@refreshScope注解,调用actuator的refresh方法进行刷新配置。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
profile: dev
label: master

 

配置文件的动态刷新流程:

1.利用actuator的refresh接口,引入actuator依赖,并且在配置文件中暴露actuator所有接口。

2.调用的类上加上@refreshscope注解

3.向config-client项目发起post请求,/actuator/refresh,强制刷新。

4.重新请求接口,得到最新配置。

 

config-server高可用

1.利用eureka注册中心

2.config-server引入eureka依赖,加上@enableDiscoveryclient注解

3.config-client引入eureka依赖,加上@enableDiscoveryclient注解

4.config-client的配置文件中,把写死的spring.cloud.config.uri改成从注册中心获取

 

 

 

配置文件加密

1.使用对称密钥或者非对称密钥,前者效率更高,安全性较低,后者采用公钥私钥,安全性较高,效率偏低。公钥也并非公共,必须是有资格获取的人才可以获得使用。

对称加密:

  1.如果jdk版本较低,需要替换相应的jce文件,去oracle官网下载

  2.在bootstrap.yml中配置encrypt.key作为加解密的密钥

  3.加密

    1)post访问localhost:60001/encrypt,raw-text格式,获取加密后的字符串

    2)把加密后的字符串放到git文件中,注意加上{cipher}前缀,否则会被当做普通字符串,而不会进行对应的解密操作,‘{cipher}加密字符串’

  4.解密是config-server自动进行的

 bus消息总线使用方式:

1.引入ampq或者kafka依赖包

2.配置文件添加消息中间件连接信息

bus的作用:批量刷新+自动通知

posted @ 2020-08-30 12:22  红嘴鲤鱼  阅读(248)  评论(0编辑  收藏  举报