美文网首页
SpringCloud(十四):SpringCloud Conf

SpringCloud(十四):SpringCloud Conf

作者: 因你而在_caiyq | 来源:发表于2019-02-23 23:01 被阅读0次

原创文章,转载请注明原文章地址,谢谢!

本地配置文件

cloud-config-eureka-client.yml

spring: 
  profiles: 
    active: 
    - dev
---
server: 
  port: 7001
spring: 
  profiles: dev
  application:
    name: cloud-config-eureka-client
eureka: 
  instance: 
    hostname: eureka7001.com
  client: 
    register-with-eureka: false
    fetch-registry: false
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/
---
server: 
  port: 7001
spring: 
  profiles: test
  application:
    name: microservicecloud-config-eureka-client
eureka: 
  instance: 
    hostname: eureka7001.com
  client: 
    register-with-eureka: false
    fetch-registry: false
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka/

cloud-config-user-client.yml

spring: 
  profiles:
    active:
    - dev
--- 
server:
  port: 8001
spring: 
   profiles: dev
   application: 
    name: cloud-config-user-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/user-db01
    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.cloud.entity
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml
eureka: 
  client:
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: user8001.com
    prefer-ip-address: true
---
server:
  port: 8001
spring: 
   profiles: test
   application: 
    name: cloud-config-user-client
   datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/user-db02
    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.cloud.entity
  mapper-locations:
  - classpath:mybatis/mapper/**/*.xml
eureka: 
  client:
    service-url: 
      defaultZone: http://eureka7001.com:7001/eureka
  instance:
    instance-id: user8001.com
    prefer-ip-address: true
Config配置Eureka服务端

首先参照cloud-eureka-7001模块,新建cloud-config-eureka-client-7001模块。添加config相关的maven依赖,其他内容和cloud-eureka-7001模块基本一致。

<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>

bootstrap.yml

spring: 
  cloud: 
    config: 
      name: cloud-config-eureka-client
      profile: dev 
      label: master 
      uri: http://config3001.com:3001

application.yml

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

接着在主启动类上贴上注解@EnableEurekaServer,测试,启动cloud-config-3001,cloud-config-eureka-client-7001,访问eureka7001.com:7001,若出现eureka主页,则表示启动成功。

Config配置Eureka客户端

参照cloud-provider-user-8001模块,新建cloud-config-user-client-8001模块。添加Config相关的maven依赖。

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

同样和配置服务端类似,添加一个bootstrap.yml文件。

spring:
  cloud:
    config:
      name: cloud-config-user-client
      profile: dev
      #profile: test
      label: master
      uri: http://config3001.com:3001

application.yml

spring:
  application:
    name: cloud-config-user-client

在主启动类上添加@EnableEurekaClient和@EnableDiscoveryClient注解。最后测试一下,启动cloud-config-3001,cloud-config-user-client-8001,访问localhost:8001/user/list,修改bootstrap.yml文件中的profile,再次访问localhost:8001/user/list,观察数据的变化。即通过外部配置bootstrap.yml中的profile的变化,客户端成功读取到了对应的配置信息。

博客内容仅供自已学习以及学习过程的记录,如有侵权,请联系我删除,谢谢!

相关文章

网友评论

      本文标题:SpringCloud(十四):SpringCloud Conf

      本文链接:https://www.haomeiwen.com/subject/nxquyqtx.html