SpringCloud学习Day02

注:本教程均是参考"程序猿DD"大佬的SpringCloud教程:http://blog.didispace.com/spring-cloud-learning/实践结合自己的理解和总结编写

分布式配置中心

有三种角色组成。1.配置中心文件;2.配置中心服务;3.配置中心客户端。

配置中心文件:

demo:https://github.com/SpringStudent/config-client

配置中心服务:

连接github的配置仓库

a.启动类:

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

b.maven坐标

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

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

 c.application.properties

spring.application.name=config-server

server.port=1202

spring.cloud.config.server.git.uri=https://github.com/SpringStudent/config-client

#用于加密,比如数据库密码等敏感信息

encrypt.key=zhouning

eureka.client.service-url.defaultZone=http://localhost:1001/eureka/

d. 验证服务是否可用

地址:GET http://ip:port/{application}/{profile}/{label}

{application}代表仓库名称,{profile}表示环境比如dev开发default默认,{label}代表git分支

如:GET http://192.168.3.94:1202/config-client/dev/master

成功后应该返回如下:

{
    "name": "config-client",
    "profiles": [
        "dev"
    ],
    "label": "master",
    "version": null,
    "state": null,
    "propertySources": [
        {
            "name": "https://github.com/SpringStudent/config-client/config-client-dev.properties",
            "source": {
                "spring.datasource.driver-class-name": "com.mysql.jdbc.Driver",
                "spring.datasource.username": "root",
                "info.profile": "dev",
                "spring.datasource.url": "jdbc:mysql://localhost:3306/test",
                "from": "springstudent",
                "spring.datasource.password": "root"
            }
        },
        {
            "name": "https://github.com/SpringStudent/config-client/config-client.properties",
            "source": {
                "info.profile": "default"
            }
        }
    ]
}

 

值得说明的是我用了用yml格式的配置文件报错:

Caused by: org.yaml.snakeyaml.parser.ParserException: while parsing MappingNode

通过网上查找资料发现都是格式的问题,但是自己查了很久没有发现问题,把配置文件改为properties的格式,将问题解决

配置中心客户端:

和其他的客户端不同的是需要引入Maven坐标spring-cloud-starter-config,并且必须使用bootstrap.properties配置文件,config-server配置才能被加载。

spring.application.name=config-client

server.port=2001

#用来配置该监控项是否属于敏感信息,访问敏感信息需要用户具有ACTUATOR角色权限,或者使用以下配置关闭安全限制

management.security.enabled=false

 

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

#通过服务访问configServer

spring.cloud.config.discovery.enabled=true

#spring.cloud.config.uri=http://localhost:1201/

spring.cloud.config.discovery.service-id=config-server

spring.cloud.config.profile=dev

spring.cloud.config.label=master

配置成功后通过

GET http://ip:port/info

可以看到如下信息

{

"profile":"default"

}

配置文件的加密与解密

配置中心服务,提供了相应的端点如下

a./encrypt/status #查看加密功能状态

在不配置encrypt.key=zhouning的状况下返回为:

{

"description":"No key was installed for encryption service",

"status":"NO_KEY"

}

在application.properites中添加了配置encrypt.key=zhouning

{

"status":"OK"

}

b./key 查看秘钥的端点

c./encrypt 对请求的body内容进行加密

如:curl http://ip:port/encrypt -d 带加密字符串

d//decrypt 对请求的body内容进行解密

如:curl http://ip:port/encrypt -d 带解密字符串

对于配置文件通过在属性值前使用{cipher}前缀来标注该内容是一个加密值如

spring.datasource.password={cipher}加密的字符串

配置的动态刷新

1.配置中心客户端添加spring-boot-starter-actuator

2.添加management.security.enabled=false到bootstrap.properties文件

3.在需要刷新的bean上添加@RefreshScope注解

4.修改配置中心文件

5.调用GET http://ip:port/refresh接口

代码:https://github.com/SpringStudent/study-spring-cloud

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值