springcloud~配置中心實例搭建


server端

build.gradle相關

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server',
            'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    )
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

bootstrap.yml配置

server:
  port: 8200
spring:
  application:
    name: lind-config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/bfyxzls/lindconfig.repo.git/
          search-paths: config-repo
          username: bfyxzls@sina.com
          password: xxxx

啟動項相關

@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
class Application {
  public static void main(String[] args) {
    // demo http://localhost://8200/email-svt.yml
    SpringApplication.run(Application.class, args);
  }
}

客戶端

build.gradle相關

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web',
            'org.springframework.cloud:spring-cloud-starter-config',
            'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

bootstrap.yml配置

spring:
  application:
    name: lind
  cloud:
    config:
      uri: http://localhost:8200
      profile: svt
      label: master #當 ConfigServer 的后端存儲的是 Git 的時候,默認就是 master

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

啟動項相關

@EnableEurekaClient
@SpringBootApplication
public class Application {

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

@RestController
public class HomeController {

  @Autowired
  private Environment profile;

  @RequestMapping("/")
  public String index(@RequestParam String key) {

    return key + "=" + profile.getProperty(key);
  }
}

配置文件倉庫

這是一個GIT倉庫,主要存儲我們的配置文件的,以application.name作為文件名,profile作為后綴,我們客戶端就是某個應用程序,它以后從倉庫獲取配置信息。

程序配置例子

graph TD B(lind.yml)-->A(lind項目最終配置) C(lind.svt.yml)-->A D(lind.production.yml)-->A

lind.yml 默認的配置

lind.svt.yml 測試環境配置,將集成默認配置

lind.production.yml 生成環境配置,將集成默認配置

配置中心例子

graph TD A(用戶服務)-->B(配置中心) C(訂單服務)-->B D(商品服務)-->B B-->E(Git倉庫)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM