zoukankan      html  css  js  c++  java
  • SpringCloud之搭建配置中心

    一、搭建config-server

    1、引入pom

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <!-- 配置中心服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- Rabbitmq模块 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <!-- 解决启动报Caused by: java.lang.ClassNotFoundException: org.eclipse.jgit.api.TransportConfigCallback -->
        <dependency>  
               <groupId>org.eclipse.jgit</groupId>  
               <artifactId>org.eclipse.jgit</artifactId>  
               <version>4.9.0.201710071750-r</version>  
           </dependency>  
    </dependencies>

    2、配置文件

    application.properties

    spring.profiles.active=@environment@
    
    # 应用名称
    spring.application.name=crm-config-server
    # 服务端口
    server.port=21200
    
    # 禁用actuator管理端鉴权
    management.security.enabled=false
    # 启用shutdown   host:port/shutdown
    endpoints.shutdown.enabled=true
    # 禁用密码验证
    endpoints.shutdown.sensitive=false

    application-local.yml

    eureka:
      client:
        serviceUrl:
          defaultZone: http://127.0.0.1:20000/eureka/
      instance:
        lease-renewal-interval-in-seconds: 10
        lease-expiration-duration-in-seconds: 30
        preferIpAddress: true
    spring:
      cloud:
        config: 
          server:
            git:
              uri: http://gitlab.xxx.com/crm/crm-config-repo.git
              username: xxx
              password: 'RHqGz$N31'
          # 这里可以写死,也可以写成{profile}来动态化 search-paths: local rabbitmq: addresses: amqp://10.18.75.231:5672 username: user_admin password: 222 virtual-host: crm

    3、Application.java

    package com.tomato.crm.config;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableConfigServer
    public class CrmConfigServerApplication {
        public static void main(String[] args) {
            SpringApplication.run(CrmConfigServerApplication.class, args);
        }
    }

    通过属性:spring.profiles.active 来控制不同环境不同的配置文件(git路径不同)

    二、配置仓库

     采用分功能(例如数据库的、redis的)、分项目的形式进行划分配置文件

    三、具体的项目(config client)

    1、引入pom

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>

    2、bootstrap.properties

    spring.application.name=crm-security-service
    server.port=23100
    
    # 默认为local,可以通过启动参数来赋值进行覆盖
    spring.profiles.active=local # 非本地的启动,注册中心采用启动参数传入,本地测试也在启动参数中注入 # 此参数请勿放开并提交,因为bootstrap的优先级最高(高于启动参数),这里不能写死 #eureka.client.serviceUrl.defaultZone=http://127.0.0.1:20000/eureka/ # 配置中心配置 spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.service-id=crm-config-server # 分支, 默认master spring.cloud.config.label=master # 环境,如果配置了则会去读取common.properties + common-local.properties/yml # spring.cloud.config.profile=local spring.cloud.config.name=common,bus,redis,security-service

    这里采用通过注册中心获取config server的形式来做多实例负载均衡。

    四、配置刷新

    方法1:

    post请求需要刷新的服务的Endpoint是:/refresh

    方法2:

    通过配置中心发起广播(MQ)刷新:手动post请求到配置中心服务的:/bus/refresh

    方法3:

    通过git lab的事件机制来post请求方法2中的url

  • 相关阅读:
    单例模式
    简单工厂模式
    MSSQL编程笔记三 left join on 和 left join where区别
    冒泡排序、选择排序和插入排序
    Extjs性能问题
    C#委托入门
    metro css for cnblogs
    委托、事件与Observer设计模式
    C# 集合类[转]
    工厂方法模式(Factory Method Pattern)
  • 原文地址:https://www.cnblogs.com/yangzhilong/p/8747009.html
Copyright © 2011-2022 走看看