spring cloud config配置

 

 

参考:

http://www.ityouknow.com/springcloud/2017/05/22/springcloud-config-git.html

http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html

https://www.cnblogs.com/xiaoliu66007/p/8963934.html

 

关系图

 

版本控制

差异文件内容

 wly-config-dev.properties

local.ip=192.168.1.0
local.port=8080

 wly-config-test.properties

local.ip=192.168.1.1
local.port=8081

 wly-config-product.properties

local.ip=192.168.1.2
local.port=8082

 

spring boot版本:2.0.3.RELEASE

spring cloud config server配置

pom.xml

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

 

application.yml

server:
  port: 8080
spring:
  application:
    name: spring-cloud-config-server-wly
  cloud:
    config:
      server:
        # git配置:项目/${search-paths}/${application}-${profile}.properties
        # cloud-config-wly/config/config/neo-config-dev.properties
        git:
          # 项目地址,换成自己的项目地址
          uri: git@xxx:xxxx/cloud-config-wly.git
          # 表示文件路径
          search-paths: config
          # 项目用户名,换成项目用户名
          username: xxx
          # 项目密码,换成项目密码
          password: xxx

 

启动类(新增@EnableConfigServer注解)

package com.wang.springcloudconfig;

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

@EnableConfigServer
@SpringBootApplication
public class SpringcloudconfigApplication {

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

 

启动服务

http://localhost:8080/wly-config/dev

 

 

spring cloud config client配置

pom.xml

<!-- 引入spring cloud config 客户端配置 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 引入web模块 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

 

application.yml同级目录(src/main/resources)新增配置文件:bootstrap.properties

此文件会先于application.yml加载

# 相当于${application}
spring.cloud.config.name=wly-config
# 相当于${profile}
spring.cloud.config.profile=product
# 配置中心server服务url
spring.cloud.config.uri=http://localhost:8080/
# 相当于git分支
spring.cloud.config.label=master

 

application.yml

spring:
  application:
    name: spring-cloud-config-client-wly
server:
  port: 8081

 

controller

package com.wang.springcloudconfigclient.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wly
 * @Date 2018/7/2 11:35
 */
@RestController
public class HelloController {
    @Autowired
    private Environment env;

    @RequestMapping("/test")
    public String test() {
        return env.getProperty("local.ip") + "," + env.getProperty("local.port");
    }
}

 访问

http://localhost:8081/test

页面输出

192.168.1.2,8082

 

基于注册中心版

在spring cloud config client端写死spring cloud config server的ip、port不适用于复杂多变的场景,比如集群的配置,ip变化等。所以我们可以基于注册中心配置

启动一个注册中心的服务spring-cloud-eureka,端口号8765

 

pom.xml

spring cloud config server、spring cloud config client均引入eureka客户端

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 

启动类

spring cloud config server、spring cloud config client启动类均添加注册注解(@EnableDiscoveryClient)

package com.wang.springcloudconfig;

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

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class SpringcloudconfigApplication {

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

 

spring cloud config server application.yml

添加注册中心配置

server:
  port: 8080
spring:
  application:
    name: spring-cloud-config-server-wly
  cloud:
    config:
      server:
        # git配置:项目/${search-paths}/${application}-${profile}.properties
        # cloud-config-wly/config/config/neo-config-dev.properties
        git:
          # 项目地址
          uri: git@xxx:xxx/cloud-config-wly.git
          # 表示文件路径
          search-paths: config
          # 项目用户名
          username: xxxx
          # 项目密码
          password: xxxx
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8765/eureka

 

spring cloud config client bootstrap.properties

# 相当于${application}
spring.cloud.config.name=wly-config
# 相当于${profile}
spring.cloud.config.profile=dev
# 配置中心server服务url,使用注册中心配置后,此句注释即可
#spring.cloud.config.uri=http://localhost:8080/
# 相当于git分支
spring.cloud.config.label=master
# 启用注册发现 spring.cloud.config.discovery.enabled=true
# 读取服务,即config server的spring.application.name spring.cloud.config.discovery.serviceId=spring-cloud-config-server-wly
# 注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8765/eureka

 

顺序启动注册中心、spring cloud config server、spring cloud config client3个服务

注册中心

访问(效果与之前一致~)

http://localhost:8080/wly-config/dev

http://localhost:8081/test

 

代码

server:https://gitee.com/wlyfree/springcloudconfig

client:https://gitee.com/wlyfree/springcloudconfigclient

 

posted @ 2018-07-02 14:31  鱼我所欲也  阅读(446)  评论(0编辑  收藏  举报