zoukankan      html  css  js  c++  java
  • SpringCloud(5)

    一、概述

    1)分布式系统面临的问题 - 配置文件的问题

    微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud 提供了 ConfigServer 来解决这个问题,我们每一个微服务自己带着一个 application.yml,如果上百个配置文件要修改的话,简直要疯了。

    2)什么是 SpringCloud-Config 分布式配置中心

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

    SpringCloud Config 分为服务端客户端两部分:

    ● 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。

    ● 客户端则是通过指定的配置中心来管理应用资源,以及业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

    3)SpringCloud config分布式配置中心能干嘛

    ● 集中管理配置文件

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

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

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

    ● 将配置信息以REST接口的形式暴露

    4)SpirngCloud Config 分布式配置中心与 Github 整合

    由于SpringCloud Config 默认使用 Git 来存储配置文件(也支持SVN和本地文件),但最推荐的还是 Git,而且使用 http / https 访问的形式。

    Git环境搭建:

    ① 在码云新建一个仓库 springcloud-config 

    创建成功后,选择Http方式克隆项目

    ② 在电脑中,某一个文件夹下,右键打开Git Bash Here,将我们刚才创建的仓库克隆下来,遇到问题,输入yes

    ③ 在克隆下来的项目目录下,新建一个文件 application.yaml,在该文件中,写一些简单的配置

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

    ④ 将我们刚写好的 application.yaml 提交到码云上

    git add .
    git commit -m "描述"
    git push origin master

    二、服务端连接Git配置

    ① 新建一个模块, springcloud-config-server-3344 

    ② 导入依赖

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

    ③ 新建 application.yaml ,编写配置

    server:
      port: 3344
    
    spring:
      application:
        name: spring-cloud-config-server
      # 连接远程仓库
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/Lin9207/springcloud-config.git

    ④ 编写主启动类,并添加注解 @EnableConfigServer 

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

    ⑤ 启动该模块,访问 http://localhost:3344/config-server-dev.yml 就可以访问到我们上传到码云中的dev配置了

    说明:

    1. 如果在GitHub上建立的仓库是私有的,那么还要加上spring.cloud.config.server.git.username和spring.cloud.config.server.git.password 这两个配置

    2. springcloud config 的URL与配置文件的映射关系如下:

    • /{application}/{profile}[/{label}]
    • /{application}-{profile}.yml
    • /{label}/{application}-{profile}.yml
    • /{application}-{profile}.properties
    • /{label}/{application}-{profile}.properties

    3. 如果github上建立的目录下的文件为configtest-dev.properties,那么当启动配置中心服务器端时,可以通过http://localhost:8050/configtest/dev/master访问配置文件,如果访问成功则表示配置中心搭建成功

    三、客户端连接服务端远程访问

    ① 在我们克隆下来的项目目录下,新建一个文件 config-client.yaml ,写一些配置

    spring:
      profiles:
        active: dev
    
    ---
    server:
      port: 8201
    # spring配置
    spring:
      profiles: dev
      application:
        name: springcloud-provider-dept
    # Eureka配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/
    
    ---
    server:
      port: 8202 
    # spring配置
    spring: 
      profiles: test
      application:
        name: springcloud-provider-dept
    # Eureka配置,服务注册到哪里
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:7001/eureka/

    ② 将该配置文件push到远程仓库

    git add .
    git commit -m "描述"
    git push origin master

    ③ 新建一个模块 springcloud-config-client-3355 ,导入依赖

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

    ④ 新建配置文件 bootstrap.yaml ,这也是 springboot 的配置文件

    bootstrap.yaml 和 application.yaml 的区别:

    • bootstrap.yaml:系统级的配置
    • application.yaml:用户级别的配置

    ● bootstrap.yaml

    spring:
      cloud:
        config:
          name: config-client
          profile: dev               # 环境
          uri: http://localhost:3344 # 地址
          label: master              # 分支

    ● application.yaml

    spring:
      application:
        name: spring-config-client-3355

    ⑤ 编写一个Controller类

    @RestController
    public class ConfigClientController {
    
        @Value("${spring.application.name}")
        private String applicationName;
    
        @Value("${eureka.client.service-url.defaultZone}")
        private String eurekaServer;
    
        @Value("${server.port}")
        private String port;
    
        @RequestMapping("/config")
        public String getConfig() {
            return "applicationName = " + applicationName + "
    ,eurekaServer = " + eurekaServer + "
    ,port = " + port;
        }
    }

    ⑥ 创建主启动类

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

    ⑦ 启动测试

    bootstrap配置了dev环境访问 http://localhost:8201/config 

    配置test环境访问 http://localhost:8202/config 

  • 相关阅读:
    C#设计一个简单的计算器,实现两个数的加,减,乘,除,求幂等计算,运行效果如下图所示:
    如何用C#中的窗体设计一款只含有加减乘除,求平方的简易版计算器?
    番外篇科普为什么1024是程序员日?2020年10月24日,程序员为啥都不放假?
    VS2019基础,C#编辑窗体时,工具箱如何调用,工具箱挡住窗体怎么办,如何改窗体名字
    C++用函数重载实现两个整数和三个浮点数的排序,按照从小到大的顺序将排序结果输出
    C++用引用形参实现两个变量的值互换
    C++用带有默认参数的函数实现,求2个或3个正整数中的最大数
    linux服务器磁盘速度测试
    CSS HACK收集
    分享一个web页面背景全屏的jquery插件Fullscreen Background
  • 原文地址:https://www.cnblogs.com/Dm920/p/13174070.html
Copyright © 2011-2022 走看看