springcloud config配置中心

配置中心简介

config server 可以从本地或git仓库里读取配置,配置可以放置在一个module里进行管理。

配置高可用config server

把config-server 配置到eureka-server服务里统一管理,可以把config-server配置成集群:

新建config server 模块

a)引入依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

b)配置文件 application.yml

spring:
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
#          search-locations: file:E://Java/本地磁盘
  profiles:
     active: native
  application:
    name: config-server

#把实例注册到eureka server服务里管理
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

在项目resource里建shared文件夹,并新建文件config-client-dev.yml,添加如下内容

server:
  port: 8783

foo: foo version 1

c)启动类添加注解

@EnableConfigServer

新建config client 模块

a)引入依赖

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

b)配置文件,bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      fail-fast: true
      discovery:
        enabled: true
        serviceId: config-server
  profiles:
    active: dev

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
根据{spring.application.name}-{profiles.active}名称到config-server 模块shared目录下查询配置文件

c)启动类不用添加其它注解

d)使用,和本地配置使用一样

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ConfigClientApplication {

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

    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/foo")
    public String hi(){
        return foo;
    }
}

测试

在浏览器输入地址:http://localhost:8783/foo

使用Spring Cloud Bus刷新配置

如果我们想修改配置文件,又不用重启项目立即生效,则需要用到spring cloud bus刷新配置。

修改config client模块

只需求修改config-client模块,即可实现动态刷新

a)引入依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
    

b)修改配置文件,添加如下内容,要先安装RabbitMQ ,

  rabbitmq:
    host: localhost
    port: 15672
    username: guest
    password: guest
    publisher-confirms: true
    virtual-host: /
#先把安全策略设置为false
management:
  security:
   enabled: false

c)在使用配置的类上添加 @RefreshScope注解

@RestController
@RefreshScope
public class ConfigClientApplication {
    @Value("${foo}")
    String foo;
    @RequestMapping(value = "/foo")
    public String hi(){
        return foo;
    }
}

测试

修改config-server项目 目录shared下的config-client-dev.yml配置文件内容,然后 发送 post请求 http://localhost:8781/bus/refresh,再http://localhost:8783/foo,你会发现配置文件修改内容已生效

原文地址:https://www.cnblogs.com/tanouou/p/12329434.html

时间: 2024-08-29 20:43:27

springcloud config配置中心的相关文章

springcloud应用配置中心config的安全设置

springcloud应用配置中心config的安全设置 在springcloud应用开发中,为了方便在线管理我们的配置文件,通常会配一个配置中心config-server,这里托管着应用的一些配置文件,这些配置文件中配置着我们很多的账号信息:如mysql.redis.mongodb.rabbitmq等等的账号和密码.牵扯到账号信息,想必我们要保证如何保证其安全性. 1.保证容器文件访问的安全性,即保证所有的网络资源请求都需要登录 通过springboot配置属性之security,配置secu

SpringCloud之配置中心Config

commons 工程commons 工程 - POM 文件<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://mav

spring cloud Nacos Config配置中心

概述Nacos 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台.Nacos Config就是一个类似于SpringCloud Config的配置中心接入SpringCloud项目集成Nacos Config配置中心很简单.只需要部署Nacos 客户端并在里面添加配置即可.然后引入Nacos Config动态读取即可1. 创建一个SpringCloud工程cloud-config 修改 pom.xml 文件,引入 Nacos Config Starter 前提得选引

微服务SpringCloud之Spring Cloud Config配置中心SVN

在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本篇学习下使用svn作为配置中心. 一.Server 端 1.准备配置文件 这里在本地电脑安装了下svn server,并在https://cuiyw/svn/config-repo/config目录下提交了上一博客的3个配置文件. 2.创建Spring Cloud Config SVN  Serve

SpringCloud Config 配置(基于Consul)

一,构建配置中心 1.在pom.xml文件中添加相关依赖 <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springf

十九、springcloud(五)配置中心本地示例和refresh

1.创建spring-cloud-houge-config子项目,测试需要的项目入下 2.添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId></dependency> 3.配置文件 aoolication.properties spring.applicati

SpringCloud之配置中心

前言 随着微服务越来越多,服务的配置文件也越来越多,这个时候对于配置文件的管理其实可以完全抽离出来,对于项目启动时需要的配置写入到bootstrap.yml中,那些可能会经常改变的则放到svn或者git上进行管理,项目启动时,会去git或者svn上拉取对应的配置文件启动. 创建配置中心项目 入口文件添加@EnableConfigServer注解 配置文件: #服务名称 spring: application: name: config-server cloud: config: server:

SpringCloud服务配置中心

SpringCloud Config简介 Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息.加密 / 解密信息等访问接口:而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获

springcloud分布式配置中心

什么是配置中心 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud config 组件中,分两个角色,一是config server,二是config client. 创建git 并添加一个properties类型的文件 创建springcloud_git