转载

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: gemini123

启动项相关

@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仓库)
原文  http://www.cnblogs.com/lori/p/9205062.html
正文到此结束
Loading...