lilele200706

 

SpringCloud Config配置中心

SpringCloud Config分布式配置

序:1000个服务,1000个配置,将它们集中起来配置。

分布式系统面临的配置文件的问题。

微服务意味将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中的,动态的配置管理设施是必不可少的。

SpringCloud提供了ConfigServer来解决这个问题。我们每一个服务自己带着一个application.yml,那上百个配置文件要修改起来,肯定会发疯的!

它是什么

SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。

它能干什么

  1. 集中管理配置文件

  2. 不同环境、不同配置、动态化的配置更新,分环境部署,比如/dev /test /prod /beta /release

  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息。

  4. 当配置发生变化时,服务不需要重启,就可感知到配置的变化,并应用新的配置

  5. 将配置信息以Rest接口的形式暴露。

怎么使用它

SpringCloud Config默认使用Git存储配置文件(也有其他方式,比如SVN和本地文件),推荐使用Git,使用http/https访问的形式。

一、Git客户端连接码云

1.安装git,注册码云

2.在码云上新建仓库springcloud-config

3.码云上配置公钥

添加了公钥才有拉取、推送 的权限。

  1. 打开 Git Bash Here

  2. 添加个人信息

    git config --global user.name "你的名字或昵称"  
    git config --global user.email "你的邮箱"
    #查看信息
    git config --list

     

  3. #生成公钥
    ssh-keygen -t ed25519 -C "xxxxx@xxxxx.com"  

    按照提示完成三次回车,即可生成 ssh key。通过查看 ~/.ssh/id_ed25519.pub 文件内容,获取到你的 public key

    #查看公钥
    cat ~/.ssh/id_ed25519.pub
  4. 将公钥添加到码云上

  5. 新建文件夹git

4.克隆仓库的文件

  1. 在码云上获取克隆的ssh地址

  2. 新建文件夹git

  3. git clone ssh地址

5.新建一个配置文件application.yml

spring:
  profiles:
      active: dev
       
---
spring:
  profiles: dev
  application:
      name: springcloud-config-dev
       
---
spring:
  profiles: test
  application:
      name: springcloud-config-test  

6.将文件添加到码云的仓库springcloud-config

  1. 进入springcloud-config中

  2. git add . #添加所有变化的文件
    git status #获取文件的所有状态
    git commit -m "提交信息" #提交文件并带上信息,此时代码还在本地,并未推送到码云
    git push origin master #将代码推送到码云

二、新建一个模块springcloud-config-server-3344

1.导包

<dependencies>

  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
      <version>2.1.1.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--       完善监控信息-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>

2.编写配置文件

server:
port: 3344
spring:
application:
  name: springcloud-config-server
#连接远程仓库
cloud:
  config:
    server:
      git:
        uri: https://gitee.com/lilele200709/springcloud-config.git #http,不是git
        #通过config-server可以连接到git,访问其中的资源以及配置

3.编写启动类,开启config功能

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

4.启动测试

三、新建一个模块springcloud-config-client-3355

0.编写一个config-client.yml配置文件放入码云

spring:
profiles:
  active: dev
   
---    

server:
port: 8201

#spring配置
spring:
application:
  profiles: dev
  name: springcloud-provider-dept
#Eureka配置
Eureka:
client:
  service-uri:
    defaultZone: http://localhost:7001/eureka/
     
---
#spring配置
spring:
application:
  profiles: test
  name: springcloud-provider-dept
#Eureka配置
Eureka:
client:
  service-uri:
    defaultZone: http://localhost:7001/eureka/

1.导包

<dependencies>

  <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
      <version>2.1.1.RELEASE</version>
  </dependency>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--       完善监控信息-->
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
</dependencies>

2.编写配置文件

  1. bootstrap.yml

    #系统级别的配置
    spring:
    cloud:
      config:
        uri: http://localhost:3344
        name: config-client #需要从git上读取的名称
        profile: dev
        label: master
  2. application.yml

    #用户级别的配置
    spring:
    application:
      name: springcloud-config-client-3355

3.编写一个测试类

@RestController
public class ConfigClientController {
  @Value("${spring.application.name}")
  private String applicationName;
  @Value("${eureka.client.service-url.defaultZone}")
  private String eurekaName;
  @Value("${server.port}")
  private String port;

  @RequestMapping("/config")
  public String getConfig(){
      return"applicationName"+applicationName+
          ",eurekaName"+eurekaName+
          ",port";
  }

}

4.编写一个启动类

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

5.启动测试

四、重构Eureka服务

0.编写一个config-eureka.yml配置文件放入码云

spring:
profiles:
  active: dev
   
---  
#spring配置
spring:
application:
  profiles: dev
  name: springcloud-config-eureka
   
server:
port: 7001

#Eureka的配置
eureka:
instance:
  hostname: localhost #Eureka服务端的实例名字
client:
  register-with-eureka: false #是否向注册中心注册自己
  fetch-registry: false #如何为false,表示自己为注册中心
  service-url: #监控页面
     #单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
     #集群defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/
  defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

1.导包

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

2.修改配置文件application.yml

#spring配置
spring:
application:
  name: springcloud-config-eureka

3.新增配置文件bootstrap.yml

#系统级别的配置
spring:
cloud:
  config:
    uri: http://localhost:3344
    name: config-Eureka #需要从git上读取的名称
    profile: dev
    label: master

4.启动测试

五、重构服务提供者

略(与重构Eureka服务基本一样)

posted on 2021-12-07 16:24  lilele200706  阅读(24)  评论(0)    收藏  举报

导航