Spring-Cloud-Config

一、简介

  在分布式系统中,由于提供服务的成功非常多,为了使服务的配置文件能够统一管理,并且能够实时的更新,为了解决这个问题,可以提供一个服务来专门提供其他服务启动时所需的配置信息。而spring cloud config就有这样的功能,它分为config server和config client。

  接下来准备使用gradle来做一个简单的demo,以备查询。

  包含两个子项目,一个是config server,另外一个时config config。

二、配置

2.1 新建父项目

build.gradle文件

version = '0.0.1'
description = 'a demo parent project'

buildscript {
    repositories {
        mavenLocal()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
        maven { url "http://repo.spring.io/libs-release" }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.4.RELEASE")
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin:'io.spring.dependency-management'

    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8

    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-config:2.0.1.RELEASE'
        }
    }

    repositories {
        mavenLocal()
        maven { url 'http://maven.aliyun.com/nexus/content/groups/public' }
    }

    dependencies {
        testCompile 'junit:junit:4.12'
    }
}

2.2 新建Spring Cloud Config Server项目

  1. build.gradle文件
group 'com.spring-cloud-config-server'
dependencies {
    compile 'org.springframework.cloud:spring-cloud-config-server'
}
  1. bootstrap.yml文件
server:
  port: 8080

spring:
  application:
    name: config-server
  profiles:
    active: native
  cloud:
    config:
      name: config-server
      profile: dev
      server:
        native:
          search-locations: classpath:config
  1. config/application-dev.properties文件
username=jack
age=22
  1. 启动文件ConfigServer.java
package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServer.class, args);
    }
}
  1. 启动程序后可以在浏览器查看到配置文件的信息:
    http://localhost:8080/application/dev
{"name":"application","profiles":["dev"],"label":null,"version":null,"state":null,"propertySources":[{"name":"classpath:config/env/application-dev.properties","source":{"username":"jack","age":"22"}}]}

2.2 新建Spring Cloud Config Client项目

  1. build.gradle文件
group 'com.spring-cloud-config-client'
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.cloud:spring-cloud-starter-config'
}
  1. bootstrap.yml文件
server:
  port: 8081

spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      uri: http://localhost:8080
  1. 启动文件ConfigClient.java
package demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @Value("${username}")
    private String name;

    @GetMapping("/hello")
    public String hello() {
        return "hello " + this.name + ", welcome";
    }

    @GetMapping("/")
    public String index() {
        return "hello cloud config client";
    }
}
  1. 启动程序后可以在浏览器查看到配置文件的信息:
    http://localhost:8081/hello
hello jack, welcome
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值