Spring cloud config 二


远程配置测试

Git配置

  • 在Git仓库中准备两个配置文件
  • config-eurekayml ,服务器(注册中心)的配置文件
spring:
    profiles: 
        active: dev
---
server:
  port: 7001

#spring配置
spring:
    profiles: dev
    application:
        name: springcloud-config-eureka

#Eurake 注册中心配置,基本上都这样配置
eureka:
  instance:
    hostname: eureka1.com # Eurake 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向Eureka 注册中心注册自己
    fetch-registry: false # fatch-registry 为 false表示 自己为注册中心
    service-url: # 监控页面
        #指向其他的两个配置中心
      defaultZone: http://eureka2.com:7002/eureka/,http://eureka3.com:7003/eureka/   
---
server:
  port: 7001

#spring配置
spring:
    profiles: test
    application:
        name: springcloud-config-eureka

#Eurake 注册中心配置,基本上都这样配置
eureka:
  instance:
    hostname: eureka1.com # Eurake 服务端的实例名称
  client:
    register-with-eureka: false # 表示是否向Eureka 注册中心注册自己
    fetch-registry: false # fatch-registry 为 false表示 自己为注册中心
    service-url: # 监控页面
        #指向其他的两个配置中心
      defaultZone: http://eureka2.com:7002/eureka/,http://eureka3.com:7003/eureka/
  • config-person.yml , 客户端(服务提供者)的配置文件
spring:
    profiles:
        active: dev
---
server:
  port: 8001
  
#mybatis
mybatis:
  type-aliases-package: cn.lzm.springcloud.pojo #给包起别名
  config-location: classpath:mybatis.xml      # mybatis配置文件的路径
  mapper-locations: classpath:mapper/*.xml   #mapper文件的路径

#spring配置
spring:
  profiles: dev
  application:
    name: provider-person-config
  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver #实现了driver,提供更多功能
    url: jdbc:mysql://localhost:3306/tea?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=PRC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

# Eureka 客户端配置
eureka:
  instance:
    instance-id: springcloud-provider-person-8001 #修改默认描述
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.1:7002/eureka/,http://127.0.0.1:7003/eureka/

#info配置
info:
  app.name: laosi-springcloud # app名字
  company.name: cn.lzm #公司名称
---
server:
  port: 8001
  
#mybatis
mybatis:
  type-aliases-package: cn.lzm.springcloud.pojo #给包起别名
  config-location: classpath:mybatis.xml      # mybatis配置文件的路径
  mapper-locations: classpath:mapper/*.xml   #mapper文件的路径

#spring配置
spring:
  profiles: test
  application:
    name: provider-person-config
  datasource:
    driver-class-name: org.gjt.mm.mysql.Driver #实现了driver,提供更多功能
    url: jdbc:mysql://localhost:3306/db02?useSSL=false&useUnicode=true&characterEncoding=utf8&serverTimezone=PRC
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource

# Eureka 客户端配置
eureka:
  instance:
    instance-id: springcloud-provider-person-8001 #修改默认描述
  client:
    service-url:
      defaultZone: http://127.0.0.1:7001/eureka/,http://127.0.0.1:7002/eureka/,http://127.0.0.1:7003/eureka/

#info配置
info:
  app.name: laosi-springcloud # app名字
  company.name: cn.lzm #公司名称

配置服务器(注册中心)

1. 导入依赖

  • pom.xml
    
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            org.springframework.boot
            spring-boot-devtools
        
    

2.编写配置

  • bootsrtap.yml
spring:
  cloud:
    config:
      label: main #分支名
      uri: http://localhost:3344 #配置的服务器名称
      name: config-eureka #获取的文件名
      profile: dev #环境名称
  • application.yml
spring:
  application:
    name: config-eureka-server

3. 启动类

@SpringBootApplication
@EnableEurekaServer
public class EurakeServer_7001 {
    public static void main(String[] args) {
        SpringApplication.run(EurakeServer_7001.class,args);
    }
}

配置客户端(服务提供者)

1. 导入依赖


        
        
            org.springframework.cloud
            spring-cloud-starter-config
        
        
        
            cn.lzm
            springcloud-api
            1.0-SNAPSHOT
        
        
        
            junit
            junit
        
        
        
            mysql
            mysql-connector-java
        
        
        
            com.alibaba
            druid
        
        
        
            ch.qos.logback
            logback-core
        
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        
        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-hystrix
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
    

2. 编写配置

  • bootstrap.yml
spring:
  cloud:
    config:
      name: config-person #获取的文件
      uri: http://localhost:3344 #指向的配置服务器
      profile: dev #获取的环境
      label: main #分支
  • application.yml
spring:
  application:
    name: person-config-provider-8001

3. 其他部分

  • 与代码一致