SpringCloud Config配置实例

SpringCloud Config配置实例

Git配置文件本地配置

在本地D:\44\mySpringCloud\microservicecloud-config路径下新建文件
microservicecloud-config-eureka-client.yml

microservicecloud-config-eureka-client.yml内容:

spring: 
  profiles: 
    active: 
      - dev
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring: 
  profiles: dev
  application:
    name: microservicecloud-config-eureka-client

eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/
---
server: 
  port: 7001 #注册中心占用7001端口,冒号后面必须要有空格

spring: 
  profiles: test
  application:
    name: microservicecloud-config-eureka-client

eureka: 
  instance: 
    hostname: eureka7001.com #冒号后面必须要有空格
  client: 
    register-with-eureka: false #当前的eureka-server自己不注册进服务列表中
    fetch-registry: false #不通过eureka获取注册信息
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/

在本地D:\44\mySpringCloud\microservicecloud-config路径下新建文件
microservicecloud-config-dept-client.yml

microservicecloud-config-dept-client.yml内容

spring: 
  profiles:
    active:
      - dev
--- 
server:
  port: 8001
spring: 
  profiles: dev
  application: 
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB01
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200 
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations:
    - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: atguigu-microservicecloud-springcloudconfig01
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$
---
server:
  port: 8001
spring: 
  profiles: test
  application: 
    name: microservicecloud-config-dept-client
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/cloudDB02
    username: root
    password: 123456
    dbcp2:
      min-idle: 5
      initial-size: 5
      max-total: 5
      max-wait-millis: 200  
mybatis:
  config-location: classpath:mybatis/mybatis.cfg.xml
  type-aliases-package: com.atguigu.springcloud.entities
  mapper-locations:
    - classpath:mybatis/mapper/**/*.xml

eureka: 
  client: #客户端注册进eureka服务列表内
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: dept-8001.com
    prefer-ip-address: true

info:
  app.name: atguigu-microservicecloud-springcloudconfig02
  company.name: www.atguigu.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$

Config版的eureka服务端

新建工程microservicecloud-config-eureka-client-7001:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-eureka-client     #需要从github上读取的资源名称,注意没有yml后缀名
      profile: dev
      label: master
      uri: http://config-3344.com:3344      #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-eureka-client

主启动类Config_Git_EurekaServerApplication

@EnableEurekaServer //启动Eureka-server服务
@SpringBootApplication
public class Config_Git_EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
    }
}

Config版的dept微服务

参考之前的8001拷贝后新建工程microservicecloud-config-dept-client-8001:

<dependencies>
        <dependency>
            <groupId>pers.zhang</groupId>
            <artifactId>microservicecloud-api</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- 将微服务provider注册进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>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-actuator</artifactId>
        </dependency>
    </dependencies>

bootstrap.yml

spring:
  cloud:
    config:
      name: microservicecloud-config-dept-client #需要从github上读取的资源名称,注意没有yml后缀名
      #profile配置是什么就取什么配置dev or test
      profile: dev
      #profile: test
      label: master
      uri: http://config-3344.com:3344  #SpringCloudConfig获取的服务地址

application.yml

spring:
  application:
    name: microservicecloud-config-dept-client

主启动类

@EnableDiscoveryClient //服务发现
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中
@SpringBootApplication
public class DeptProviderConfig8001_App {
    public static void main(String[] args) {
        SpringApplication.run(DeptProviderConfig8001_App.class, args);
    }
}

测试

test配置默认访问,http://localhost:8001/dept/list

在这里插入图片描述
可以看到数据库配置是02

在这里插入图片描述
本地换配置成dev,http://localhost:8001/dept/list
在这里插入图片描述
可以看到数据库配置是01

发布了789 篇原创文章 · 获赞 2172 · 访问量 27万+

猜你喜欢

转载自blog.csdn.net/cold___play/article/details/104452892