【SpringCloud总结】13 SpringCloud Config实战

我们做一个eureka服务+一个Dept访问的微服务,将两个微服务的配置统一由于github获得实现统一配置分布式管理,完成多环境的变更

 

一、Git配置文件本地配置

1. 在本地D:\44\mySpringCloud\microservicecloud-config路径下新建文件 microservicecloud-config-eureka-server.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/

2. 在本地D:\44\mySpringCloud\microservicecloud-config路径下新建文件 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服务端

1. 新建工程 microservicecloud-config-eureka-client-7001 

2. pom文件



<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>
  
  <parent>
    <groupId>com.atguigu.springcloud</groupId>
    <artifactId>microservicecloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <artifactId>microservicecloud-config-eureka-client-7001</artifactId>
  


  <dependencies>
    <!-- SpringCloudConfig配置 -->
    <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>
</project>

3. bootstrap.yml

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

4. application.yml

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

5. 主启动类Config_Git_EurekaServerApplication

package com.atguigu.springcloud;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;


/**
 * EurekaServer服务器端启动类,接受其它微服务注册进来
 * @author zhouyang
 */
@SpringBootApplication 
@EnableEurekaServer 
public class Config_Git_EurekaServerApplication 
{
  public static void main(String[] args) 
  {
   SpringApplication.run(Config_Git_EurekaServerApplication.class, args);
  }
}

6. 测试

(1)先启动 microservicecloud-config-3344 微服务,保证Config总配置是OK的

(2)再启动microservicecloud-config-eureka-client-7001微服务

(3)http://eureka7001.com:7001/  出现eureak主页表示成功启动

 

三、Config版的dept微服务

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

2. pom文件



<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>
  
  <parent>
   <groupId>com.atguigu.springcloud</groupId>
   <artifactId>microservicecloud</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </parent>
  
  <artifactId>microservicecloud-config-dept-client-8001</artifactId>
  
  <dependencies>
   <!-- SpringCloudConfig配置 -->
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
   </dependency> 

   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-actuator</artifactId>
   </dependency>
   <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
   </dependency>
   <dependency>
     <groupId>com.atguigu.springcloud</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>
  </dependencies>
</project>

3. 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获取的服务地址

4. application.yml

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

5. 主启动类

package com.atguigu.springcloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


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

6. 测试

(1)bootstrap文件中profile值是test时

         http://localhost:8001/dept/list 浏览器返回的数据是cloudDB02的信息

(2)bootstrap文件中profile值是dev时

         http://localhost:8001/dept/list 浏览器返回的数据是cloudDB01的信息

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值