Config 二

Config的配置比较简单。

服务端:

pom.xml文件

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

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

启动类:

package com.yjp.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

配置:

server.port=8085
spring.application.name=microservice-config-server
#git所在的url
spring.cloud.config.server.git.uri=
#账号
spring.cloud.config.server.git.username=
#密码
spring.cloud.config.server.git.password=

然后在git上面创建properties文件,然后启动项目,在路径上面输入http://localhost:8085/cloud-config-dev.properties(在git上面创建的properties的文件名称)就可以获取到相应的配置了。

客户端:

pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>1.4.0.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka</artifactId>
    <version>1.3.5.RELEASE</version>
</dependency>
<!--用来刷新配置-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

启动类:

package com.yjp.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

配置:这里的配置不在是application.properties而是bootstrap.properties  因为我们的配置需要第一时间就去加载的。bootstrap的优先级高于application

server.port=7005
#必须和配置的服务名一致
spring.application.name=cloud-config
#需要加载的配置环境
spring.cloud.config.profile=dev
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=microservice-config-server
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
#将权限关闭 访问监控端口需要权限  在后面刷新配置的时候使用
management.security.enabled=false

controller:调用

package com.yjp.configclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
/**
 * RefreshScope  表示这个配置可能会刷新
 */
@RefreshScope
public class TestController {
    //在码云上面配置的键值对
    @Value("${name}")
    private String name;

    @RequestMapping("/name")
    public String test() {
        return this.name;
    }
}

也可以这样:

package com.yjp.configclient;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * RefreshScope  表示这个配置可能会刷新
 */
@RestController
@RefreshScope
public class TestController {
    //在码云上面配置的键值对
//    @Value(value = "${name:s}")
//    private String name;
    @Autowired
    private Environment env;

    @RequestMapping("/test")
    public String test() {
        return env.getProperty("name");
    }

}

然后访问,就可以获取配置了。

客户端应用从配置管理中获取配置信息遵从下面的执行流程:

1.应用启动时,根据bootstrap.properties中配置的应用名{application}、环境名{profile}、 分支名{label}, 向ConfigServer请求获取配置信息。

2. Config Server根据自己维护的Git仓库信息和客户端传递过来的配置定位信息去查找配置信息。

3 通过git clone命令将找到的配置信息下载到ConfigServer的文件系统中。

4. Config Server创建Spring的ApplicationContext实例, 并从Git本地仓库中加载配置文件, 最后将这些配置内容读取出来返回给客户端应用。

5. 客户端应用在获得外部配置文件后加载到客户端的ApplicationContext实例,该配置内容的优先级高于客户端Jar包内部的配置内容, 所以在Jar包中重复的内容将不再被加载。Config Server巧妙地通过git clone将配置信息存于本地, 起到了缓存的作用, 即使当Git服务端无法访问的时候, 依然可以取ConfigServer中的缓存内容进行使用。

此时我们改变码云上面的参数:重新访问服务端正常,然后在访问客户端发现并不能正常刷新。

将客户端的management.security.enabled=false设置为false,因为此时我们需要去访问refresh,需要权限,然后post请求访问客户端localhost:port/refresh就可以完成刷新了。

如果感觉这样会安全,也可以引入security来做加密设置

server:

#访问配置中心的账号
security.user.name=user
security.user.password=37cc5635-559b-4e6f-b633-7e932b813f73

Client:

spring.cloud.config.username=user
spring.cloud.config.password=37cc5635-559b-4e6f-b633-7e932b813f73

这样就完成了一个简单的加密,当然也可以自己设置更为复杂的加密。当使用安全验证后,如果是使用第一种@value的方式获取值,当账号密码不对的时候,springboot是无法启动成功的,但是第二种可以,所以推荐使用第一种方式启动springboot。

原因:当springboot在启动的时候,就会先去配置中心去拉取配置,当拉取不到的时候就直接报错了。

可以设置快速失败,这样会在启动的时候优先去拉取配置,防止springboot启动时间过长浪费时间。

spring.cloud.config.failFast= true

也可以设置重试间隔和重试的次数

pom.xml

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

配置:

#初始重试间隔时间(单位为毫秒),默认为 1000 毫秒
spring.cloud.config.retry.multiplier=
#下一间隔的乘数,默认为1.1,所以当最初间隔是1000 毫秒时,下一次失败后的间隔为1100毫秒。
spring.cloud.config.retry.initial-interval=
#最大的重试次数 默认为6
spring.cloud.config.retry.max-attempts=
#最大间隔时间,默认为 2000毫秒。
spring.cloud.config.retry.max-interval=

高可用,只需要多配置两个server端就可以了,做一个集群。

到这里就结束了。

努力吧,皮卡丘。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值