• SpringCloud分布式config配置中心


      微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,

    因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,

    所以一套集中式的、动态的配置管理是必不可少的。Spring Cloud 提供了 ConfigServer 来解决这个问题。

      Spring Cloud Config 分为两个部分:Config Server 和 Config Client。

    很多服务,每个服务都是自己的配置,  分多环境,  必须3份, 每个服务的配置信息位于服务下

    1)  当项目上线: 交给运维,   运维人员对项目不是很精通,  修改某个配置,  找到某个配置, 很麻烦

    2) 某个模块的微服务, 部署为集群, , 修改配置, 某个模块的集群服务都需要修改, 重复修改, 可能漏了某些服务没有修改

    3) 修改了配置,需要重启服务, 

    配置中心:

    1) 整个微服务群所有的配置集中管理

    2) 结合SpringCloud  bus(总线) 修改了配置,自动更新微服务

     

    配置中心的步骤

    1、创建一个配置中心的服务,作为Eureka的客户端

    //pom.xml文件中继承父项目
    <parent>
        <groupId>com.zl.house</groupId>
        <artifactId>house-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../house-parent/pom.xml</relativePath>
    </parent>

    2、添加Eureka-client,config-server的依赖(删除测试类依赖,并且测试文件夹删除)

    <!--添加Eureka-client的依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- config-server的依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>

    3、在启动类添加注解:

    @SpringBootApplication
    @EnableEurekaClient   //作为Eureka的client
    @EnableConfigServer  //作为配置中心
    public class HouseCloudConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(HouseCloudConfigServerApplication.class, args);
        }

    4、添加相关配置

    在资源文件夹下创建一个config的文件夹,其中创建每个服务配置的文件夹

     例:user-dev.yml  (文件名中间必须以“-”分割,另外的环境只需要更改环境名、端口号

    #user服务的开发环境的配置文件
    server: port:
    9099 spring: profiles: active: dev

    user.yml的配置 (Eureka的配置必须在自己的服务中配置,因为都要交给注册中心管理才能起效

    #存放的不同环境的公共的配置, 
    #配置中心, 访问 user-dev.yml, 自动把user.yml的配置合并到user-dev.yml中
    
    #指令日志的配置文件
    logging:
      config: classpath:logback-user.xml
      
    #Mybatisplus的配置
    mybatis-plus:
      type-aliases-package: com.zl.house.user.entity
      mapper-locations: classpath*:mapper/*Mapper.xml
      
    #mysql数据库四大参数
    spring:
      datasource:
        driver-class-name: oracle.jdbc.driver.OracleDriver
        url: jdbc:oracle:thin:@localhost:1521:orcl
        username: scott
        password: tiger
        #指定使用哪个数据源
        #type: org.apache.commons.dbcp2.BasicDataSource
        #type: com.alibaba.druid.pool.DruidDataSource
        druid: 
           initSize: 10
           maxSize: 100
           minSize: 10
      jackson:
        time-zone: Asia/Shanghai
        date-format: yyyy-MM-dd HH:mm:ss
        #json对属性值为null 忽略
        default-property-inclusion: NON_NULL
        
      application:
        name: house-userService
    
    #服务的描述信息
    info:
      app:
        name: helloServer-microcloud
      author:
        name: suke
      build:
        artifactId: $project.artifactId$
        version: $project.version$
      company:
        name: www.zl.com
      server:
        port: ${server.port}
    
    ribbon:
      eureka:
        enabled: true
    
    #允许Feign客户端使用Hystrix
    feign:
     hystrix: 
      enabled: true  

    application.yml的配置文件:

    server:
      port: 8866
      
    #eureka相关配置
    eureka:
      instance:
        hostname: 127.0.0.1
      #修改eureka-client在注册中心页面显示的名字
      #${spring.cloud.client.ipaddress} 获取服务的ip地址
        instance-id: configServer-${spring.cloud.client.ipaddress}-${server.port}
        #服务信息显示的真实的ip, 开发中一定要设置为true, 如果不设置, 其他电脑访问不到你的服务
        prefer-ip-address: true
      client:
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:10001/eureka/
    
    spring:
      application:
        name: houseCloud-configServer
          #配置中心相关的配置
      profiles:
        active: native #设置使用本地配置(默认是git,可以设置:subversion(SVN),native(本地))
      cloud:
        config:
          server:
            #如下是本地文件配置
            native:
              search-locations: classpath:/config/ #配置文件存放的目录    
          

    注意:  配置中心,每个微服务的eureka相关配置,一定不要在配置中心的配置文件写

    其他微服务拉取配置中心对应的配置信息

    1、添加依赖  config-client

    <!--config client依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>

    2、 微服务的配置文件,名字: bootstrap.yml(springBoot读取配置文件的顺序:  bootstrap.yml   --> application.yml)

    注意: 一定不能直接把application.yml改名为bootstrap.yml,  需要删除application.yml, 再创建bootstrap.yml

    spring:
      application:
        name: house-userService
      cloud:
        config:
          name: user # 指定配置文件的名字
          #uri: http://localhost:8866
          discovery:
            enabled: true
            service-id: houseCloud-configServer  # 配置中心的服务名
          profile: dev  # 指定配置文件的环境     
          label: userConfig   # 指定配置文件的目录   userConfig/user-dev.yml
          
    #eureka相关配置
    eureka:
      instance:
        hostname: 127.0.0.1
      #修改eureka-client在注册中心页面显示的名字
      #${spring.cloud.client.ipaddress} 获取服务的ip地址
        instance-id: userService-${spring.cloud.client.ipaddress}-${server.port}
        #服务信息显示的真实的ip, 开发中一定要设置为true, 如果不设置, 其他电脑访问不到你的服务
        prefer-ip-address: true
      client:
        serviceUrl:
          defaultZone: http://${eureka.instance.hostname}:10001/eureka/

    启动顺序:

      注册中心 --> 配置中心 --> 其他服务

  • 相关阅读:
    POJ 2723 Get Luffy Out(2-SAT)
    ZOJ 3613 Wormhole Transport
    HDU 4085 Peach Blossom Spring
    NBUT 1221 Intermediary
    NBUT 1223 Friends number
    NBUT 1220 SPY
    NBUT 1218 You are my brother
    PAT 1131. Subway Map (30)
    ZSTU OJ 4273 玩具
    ZSTU OJ 4272 最佳淘汰算法
  • 原文地址:https://www.cnblogs.com/64Byte/p/13295213.html
Copyright © 2020-2023  润新知