美文网首页
Spring Cloud 学习之路 —— Config

Spring Cloud 学习之路 —— Config

作者: youngmon3y | 来源:发表于2019-01-07 17:32 被阅读0次

    Config Server

    使用 IntelliJ Idea 创建一个Config Server项目
    config也是一个微服务,所以也需要注册
    在Dependencies选择时选择:

    • Cloud Discovery - Eureka Discovery ;
    • Cloud Config - Config Server;
      都是老套路了,启动类上增加注解
    package com.calligraphy.config;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @SpringBootApplication
    @EnableDiscoveryClient
    @EnableConfigServer
    public class ConfigApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigApplication.class, args);
        }
    
    }
    

    你现在启动就大错特错了,还没配置呢,先将git安装好,安装教程就不说了,开始写我们的配置文件
    我们需要用到远程仓库,我使用的是码云(不是马爸爸),注册完成后创建一个仓库,这是我的仓库

    主页.png
    打开仓库创建一个文件
    新建文件.png
    文件.png
    将你的公共配置给放上去
    配置.png
    注意格式,我使用的是.yml文件
    远程仓库的设置就到此为止了...
    接下来回到我们的项目
    在yml上填写你的远程仓库的用户名和密码
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8761/eureka/
    spring:
      application:
        name: config
      cloud:
        config:
          server:
            git:
              uri: https://gitee.com/hystyle/calligraphy-config
              username: xxxx
              password: xxxx
              basedir: D:\calligraphy\config\basedir  //下载到此文件夹,不要下载到自己的项目目录
    

    以上完成后,启动项目,打开http://localhost:8090/order-a.yml,至于为什么加 -a ,自己去了解一下吧,总之你不加就报404,成功之后,Config Server就完成了

    接下来看看如何使用?

    Config Client

    首先添加依赖

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

    修改yml,名字也要修改 bootstrap.yml

    spring:
      application:
        name: order
      cloud:
        config:
          discovery:
            enabled: true
            service-id: CONFIG
          profile: dev   //这里怎么描述..我有个文件名是:order-dev.yml ,
                         //order就对应了本项目的name,所以这里不用写,只用写后面的dev...大概是这样
    
    

    启动项目,正常启动...Spring Cloud Bus ,暂时有坑,我暂时不用,所以不写...以后补
    (自动更新需要用到)

    简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

    相关文章

      网友评论

          本文标题:Spring Cloud 学习之路 —— Config

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