Config分布式配置中心

首先,我们的这个配置是放在Github上面的远程配置
在这里插入图片描述
然后把这个空仓库拉下来
拉下来的代码非常简单,找到个文件夹,然后 git clone [url]
warning不用管
在这里插入图片描述
然后你劈里啪啦一顿操作,在本地写出了个配置文件:
在这里插入图片描述
接着我们想要把这个配置文件上传上去。

上传的代码也不难,简单解释下

//设置我们要上传的远程仓库地址
git remote add origin https://github.com/SunEnjoy/JustARep.git
//如果origin已经被占用,可以使用这个命令(如果没有的话,直接忽略就好了)
git remote set-url origin https://github.com/SunEnjoy/JustARep.git
//将当前的分支切到main主分支上
git branch -M main
//确认push关联上传的位置为origin main
git push --set-upstream origin main
//将所有Commit了的文件推送到main这个分支上去,你直接写push也行
git push -u origin main

常见问题:
fatal: unable to access ‘https://github.com/…’: OpenSSL SSL_read: Connection was reset, errno 10054
解决:
产生原因:一般是因为服务器的SSL证书没有经过第三方机构的签署,所以才报错
参考网上解决办法:解除ssl验证,git config --global http.sslVerify “false”

常见问题:
failed to connect to github.com port 443
解决:
git config --global --unset http.proxy
git config --global --unset https.proxy

上传完成之后,我们咋用这个GitHub里面的配置文件呢?
很简单,按照以下步骤来就行了:
第一步,当然还是一样导入相关依赖

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

第二步,设置启动类,且开启相关服务

@SpringBootApplication
@EnableConfigServer // 启用config server功能
public class ConfigServerApp {

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

第三步,最重要!配置git的地址呀!以及设置你要使用的分支

server:
  port: 9527

spring:
  application:
    name: config-server
  # spring cloud config
  cloud:
    config:
      server:
        # git 的 远程仓库地址
        git:
          uri: https://github.com/SunEnjoy/remote_config.git
      label: main # 分支配置

在这里插入图片描述
别的就不说了,直接666,老子好无敌。
那么接下来,我们就来看看Client如何使用这个配置文件吧。
第一步,Client中配置bootstrap.yml文件(优先级高于application.yml)

# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
  cloud:
    config:
      # 配置config-server地址
      uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  config-dev.yml
      label: master # 分支

我们之前已经测试过http://localhost:9527/main/config-dev.yml畅通无阻了
所以其实这里就是获取config-dev.yml的值呀!

  @Value("${name}")
    private String name;

    Goods goods = goodsService.findOne(id);

    goods.setTitle(goods.getTitle() + ":" + port+":"+name);//将端口号,设置到了 商品标题上
    return goods;

在这里插入图片描述
这个时候,我们希望我们在配置文件中的修改可以实时的反应到我们的客户端上,而不是每次要重启程序。所以我们还需要配置一下。
第一步,引入依赖(监视器的依赖):

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

第二步,添加注解:

@RestController
@RequestMapping("/goods")
@RefreshScope //开启刷新功能,也就是你这个类里面的路径一发送,那么我们就会刷新获得数据
public class GoodsController {....}

第三步,在Bootstrap.yml里面加上暴露点(其实我们只用暴露refresh即可)

management:
  endpoints:
    web:
      exposure:
        include: '*' # 全部暴露

第四步,发送Curl消息:curl -X POST http://localhost:8001/actuator/refresh
在这里插入图片描述

最后,我们还要把这个配置中心集成到Eureka服务器上,进行统一的管理。
其实换句话说就是把这个Config Server注册为Eureka Client。
这个我们就常做了。
第一步,导入依赖:

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

第二步,配置中标注自己为Eureka的Client,并告知服务器的地址(可选:你也可以在启动类中标注一下这是Eureka的Client @EnableEurekaClient //该注解 在新版本中可以省略)

eureka:
   client:
    service-url:
      defaultZone: http://localhost:8761/eureka

接下来我们就可以动态获取了,我们可以看到同样身为Client的provider:

spring:
  cloud:
    config:
      # 配置config-server地址
      uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  会自动拼接成config-dev.yml
      label: main # 分支

这个uri是写死的!!!
那么我们希望他是动态的,所以在provider的配置中加上discovery的配置即可

spring:
  cloud:
    config:
      # 配置config-server地址
      # uri: http://localhost:9527
      # 配置获得配置文件的名称等信息
      name: config # 文件名
      profile: dev # profile指定,  会自动拼接成config-dev.yml
      label: main # 分支
      discovery:
        enabled: true # 允许在Server中发现
        service-id: CONFIG-SERVER # 在哪个服务中发现呢?就是CONFIG-SERVER啦
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值