ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springcloud入门(七)之config配置中心

2021-12-28 23:03:03  阅读:134  来源: 互联网

标签:入门 spring 配置 eureka springcloud server config


1 springcould config分布式配置

1.1 概述

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

1.2 什么是springcloud config配置中心

springcloud config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。
springcloud config分为服务端和客户端两部分:
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密,解密信息等访问接口
客户端则是通过指定的配置中心来管理应用资源,以及与业务先关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git
来存储配置信息,这样就有助于对环境配置进行版本管理并且可以通过git客户端工具来方便管理和访问配置内容。

1.3 springcloud config分布式配置中心能干嘛

  • 配置管理配置文件
  • 不同环境,不同配置,动态化的配置更新,分环境部署,比如:dev、test、prod、beta、release
  • 运行期间动态调整配置,不需要在每个无服务器的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发送变动时,服务不需要启动,即可感知到配置的变化,并应用新的配置
  • 将配置信息以rest接口的形式暴露

1.4 springcloud config分布式配置中心与GitHub整合

由于springcloud config默认使用git来存储配置文件(也有其他方式,比如支持svn和并本地文件),但是最推荐还是Git而且使用的是http/https访问的形式

1.5 环境搭建

  • 先注册一个码云地址,地址点我:
  • 先建立码云仓库的server
  • 创建一个模块工程 config-server-3344
  • 创建一个模块工程 config-eureka-8000
  • 创建一个模块工程 config-provider-8001

1.5.1 config-server-3344

项目创建好后老规矩代码,三大步上篮

pom

添加依赖

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

yml

编写配置

server:
  port: 3344
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/damonYuanlai/springcloud-config-server.git #仓库地址

启动类

开启注解

@SpringBootApplication
@EnableConfigServer//开启注解
public class ConfigApplication3344 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication3344.class,args);
    }
}

测试访问

测试前需要在服务仓库上配置一个application.yml文件

# 这个3344项目只是为了读取配置,不干别的事,我这里配了 2 套环境为了测试,

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

在这里插入图片描述
访问地址:

  • http://localhost:3344/application-test.yml
  • http://localhost:3344/application-dev.yml
  • http://localhost:3344/master/application-dev.yml
    在这里插入图片描述
    能得到上述信息,说明配置成功!

1.5.2 config-eureka-8000

老规矩,还是三大步上篮

pom

添加依赖


    <dependencies>
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
        <!--erueka  server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
            <version>1.4.7.RELEASE</version>
        </dependency>
        <!--热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

yml

修改配置: 系统级别的 可以覆盖application.yml 用户级别的
bootstrap.yml

# 系统级别的 可以覆盖application.yml
spring:
  cloud:
    config:
      uri: http://localhost:3344 # 直接3344项目获取配置文件
      name: config-eureka # 需要从git上读取资源名称
      profile: dev #使用环境
      label: master # 使用分支

appliction.yml

#用户级别
spring:
  application:
    name: config-eureka-8000

启动类

@SpringBootApplication
@EnableEurekaServer //EnableEurekaServer 表示注册服务的启动类,可以接收别人注册进来
public class ConfigEurekaServer8000 {
    public static void main(String[] args) {
        SpringApplication.run(ConfigEurekaServer8000.class,args);
    }
}

访问测试

测试访问前需要在git仓库中添加一个config-eureka.yml

spring:
  profiles:
    active: dev

---
server:
  port: 8000
 
spring:
  profiles: dev
  application:
    name: springcloud-config-eureka-dev

#Eureka 配置
eureka:
  instance:
    hostname: eureka0.com #服务端实例名称,如果名字一样,会当中同一个集群导致集群失败,所有我们需要改回来
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #如果为false 则表示自己为注册中心
    service-url: #监控页面地址
      #单机模式下配置自己一个就够了
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
      # 集群:我们需要在8100 下挂载8000和8200
      #defaultZone: http://eureka0.com:8000/eureka,http://eureka2.com:8200/eureka
---
server:
  port: 8000
 
spring:
  profiles: test
  application:
    name: springcloud-config-eureka-test

#Eureka 配置
eureka:
  instance:
    hostname: eureka0.com #服务端实例名称,如果名字一样,会当中同一个集群导致集群失败,所有我们需要改回来
  client:
    register-with-eureka: false #表示是否向eureka注册中心注册自己
    fetch-registry: false #如果为false 则表示自己为注册中心
    service-url: #监控页面地址
      #单机模式下配置自己一个就够了
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
      # 集群:我们需要在8100 下挂载8000和8200
      #defaultZone: http://eureka0.com:8000/eureka,http://eureka2.com:8200/eureka

访问地址:http://localhost:3344/config-eureka-test.yml
在这里插入图片描述

1.5.3 config-provider-8001

最后就是服务提供者了,新建模块

标签:入门,spring,配置,eureka,springcloud,server,config
来源: https://blog.csdn.net/weixin_45189427/article/details/122196936

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有