Springcloud的configs配置使用

一、创建配置文件svn配置存放项目

1、创建项目名称为config-server,下面文件夹为cust,存放在svn下面

134523_zA0X_2404345.png

2、application-dev.yml配置如下

---  
spring:
  datasource:
    name: mydb
    type: com.zaxxer.hikari.HikariDataSource
    url: jdbc:mysql://127.0.0.1:3306/custdb?characterEncoding=utf8&useSSL=false
    username: root
    password: root
    driverClassName: com.mysql.jdbc.Driver
    minIdle: 5
    maxActive: 10
    initialSize: 1
    timeBetweenEvictionRunsMillis: 3000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
  rabbitmq:
    host: localhost
    port: 5672
    username: zhb
    password: zhb
    virtual-host: cloud-zhb

mybatis:
  #姝ら厤缃厤鍚坸mlUserMapper鐨勪娇鐢�
  mapperLocations: classpath:/mappers/*.xml
  typeAliasesPackage: com.bincai.cloud.cust.domain.model
eureka:
  instance:
    preferIpAddress: true
    metadata-map:
      cluster: MAIN
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
 

bootstrap-dev.yml

server:
  port: 8012
spring:
  application:
    name: cloud-cust-domain

开发和测试环境分开配置,名称分别以dev和test后缀区分

二、创建配置配置中心提供服务

1、pom.xml配置如下,注意增加svn插件的支持

<?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://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <artifactId>love-life-config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>love-life-config-server</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
         <version>1.3.5.RELEASE</version>
    </parent>

    <properties>
         <start-class>com.love.life.config.server.ConfigApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
     <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- eureka依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
    <!-- 配置服务依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!-- svn依赖 -->
    <dependency>
        <groupId>org.tmatesoft.svnkit</groupId>
        <artifactId>svnkit</artifactId>
    </dependency>
    </dependencies>    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>
 

2、启动文件配置如下,增加如下标签

package com.love.life.config.server;

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

/**
 * 项目的启动类,指定扫描的包路径
 * @author 
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
@EnableAutoConfiguration
public class ConfigApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(ConfigApplication.class, args);
    }
}
 

配置文件application.yml配置如下

spring:
  cloud:
    config:
      server:
        svn:
          uri: svn地址
          username: 账号
          password: z密码
      enabled: true
  profiles:
    active: subversion
eureka:
  instance:
    preferIpAddress: true
    statusPageUrlPath: /info.html
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

bootstrap.yml配置如下

server:
  port: 8028
spring:
  application:
    name: love-life-config-server

三、客户端应用配置中心配置

1、客户端依赖pom.xml,注意依赖spring-cloud-starter-config

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.bincai.cloud</groupId>
  <artifactId>cloud-cust-domain</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>cloud-cust-domain</name>
  <url>http://maven.apache.org</url>
    <parent>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-parent</artifactId>
     <version>1.3.5.RELEASE</version>
   </parent>
    <properties>
        <start-class>com.taikang.boot.UserApplication</start-class>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Brixton.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!-- eureka依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
         <!-- 配置 -->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-config</artifactId>
     </dependency>
    <!-- hystrix依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
    </dependency>
    <!-- ribbon依赖 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
    </dependency>
    <!-- sleuth依赖 -->
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-sleuth</artifactId>
     </dependency>
     <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
     </dependency>
      <dependency>
         <groupId>org.springframework.cloud</groupId>
         <artifactId>spring-cloud-sleuth-stream</artifactId>
     </dependency>
     <!-- mybatis依赖 -->
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <dependency>  
        <groupId>org.mybatis.spring.boot</groupId>  
        <artifactId>mybatis-spring-boot-starter</artifactId>  
        <version>1.1.1</version>  
    </dependency>
    <dependency>  
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>  
    </dependency>
        <!-- json start -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.8</version>
        </dependency>
        <!-- json end -->
  </dependencies>
   <build>  
      <plugins>  
          <plugin>  
              <groupId>org.springframework.boot</groupId>  
              <artifactId>spring-boot-maven-plugin</artifactId>  
          </plugin>  
      </plugins>  
  </build>
</project>
 

2、本地配置文件bootstrap.yml

spring:
  cloud:
    config:
      name: application,bootstrap    //引入两个配置文件名称的前缀名字
      uri: http://localhost:8028/
      profile: ${config.profile:dev}     //引入配置文件名称的后面的环境标示位
      label: ${config.label:cust}      //引入配置中心的配置文件夹

3、启动后可以看到加载配置中心配置的信息

 

转载于:https://my.oschina.net/Seaside20151225/blog/745283

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值