SpringCloud-Config

Git工具

下载地址:git-scm.com/download/wi…

下载完,配置一下用户名和用户邮箱

  1. 输入git config --list显示当前的config配置

image.png

  • 发现没有用户名和用户邮箱的配置
  1. 输入git config --global user.name "suisuinian"配置用户名
  2. 输入git config --global user.email "1906713811@qq.com"配置邮箱
  3. 再输入git config --list显示当前的config配置

image.png

  • 发现用户名和用户邮箱已经配置

使用码云进行代码托管

  1. 先注册码云账号
  2. 得到SSH公钥(通过Git工具)
    • 输入ssh-keygen -t rsa -C "1906713811@qq.com"
    • 按三次回车,得到公钥
    • 输入cat ~/.ssh/id_rsa.pub 查看公钥

image.png

  1. 设置SSH公钥(在码云上设置)

image.png

在码云上创建一个仓库,并下载这个远程仓库到本地

image.png

将该填的信息填一下

image.png

实现本地和远程仓库的调用

  1. 拿到你创建的远程仓库的SSH的地址,我这里是git@gitee.com:asfasfasda/springcloud-config.git

image.png

  1. 创建一个本地仓库的文件夹
  2. 进入该文件夹,右键,使用Git工具命令git clone git@gitee.com:asfasfasda/springcloud-config.git,下载远程仓库的代码到本地仓库
  3. 结果:

image.png

image.png

在本地仓库新增文件,提交到远程仓库(码云上)

  1. 新建application.yml
spring:
    profiles:
        active: dev

---
spring:
    profiles: dev
    application: 
        name: springcloud-config-dev
        
---
spring:
    profiles: test
    application: 
        name: springcloud-config-test
复制代码
  1. 进去我们创建的文件夹,右键,启动Git工具
  2. 输入git add .,添加application.yml
  3. 输入git status查看我们添加与否

image.png

  1. 输入git commit -m "add application.yml"将它提交到本地仓库,其中,-m 后写的是本次修改的信息

  2. 输入git push origin master将它提交到码云

其中,origin代表当前用户,master表示提交到码云的远程仓库的master分支

image.png

  1. 刷新我们在码云托管的仓库,观察是否提交成功。

image.png

Config

简介

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了ClientServer两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。

实验操作

服务端

  1. 导入依赖
  <!-- config 不写版本号,默认是2.2.6.RELEASE版本,别用最新的,有毒-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
复制代码
  1. 编写配置,远程仓库就使用我上一篇博客创建的远程仓库
server:
  port: 3344

spring:
  application:
    name: springcloud-config-server
  # 连接远程仓库 uri为远程仓库中http的地址
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/asfasfasda/springcloud-config.git
复制代码
  1. 编写主启动类@EnableConfigServer注解,开启服务端配置支持
package springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer//开启服务端配置支持
public class springcloud_config_server3344 {
    public static void main(String[] args) {
        SpringApplication.run(springcloud_config_server3344.class,args);
    }
}

复制代码
  1. 启动测试

访问的url如下:

其中,label:远程仓库分支 profile: application.yml中的分支

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

image.png

客户端

  1. 再创建一个config-client.yml文件,并上传到远程仓库
spring:
 profiles:
  active: dev

---
server:
 port: 8201
#spring配置
spring:
 profiles: dev
 application: 
  name: springcloud-provider-dev
#eureka配置
eureka:
 client:
  service-url:
   defaultZone: http://eureka7001:7001/eureka/

---
server:
 port: 8202
#spring配置
spring:
 profiles: test
 application: 
  name: springcloud-provider-test
#eureka配置
eureka:
 client:
  service-url:
   defaultZone: http://eureka7001:7001/eureka/
复制代码
  1. 导入依赖
        <!--config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
复制代码
  1. 编写aplication.yml配置
# 用户级别的配置
spring:
  application:
    name: springcloud-config-client
复制代码
  1. 编写bootstrap.yml配置
# 系统级别的配置
spring:
  cloud:
    config:
      name: config-client #拿到远程仓库的资源config-client.yml,这里不需要填后缀
      profile: dev
      label: master
      uri: http://localhost:3344
复制代码
  • bootstrap.yml: 系统级别的配置
  • aplication.yml: 用户级别的配置
  1. 编写controller,返回从远程仓库获取到的信息
package springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class configController {
    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServer;

    @Value("${server.port}")
    private String port;

    @RequestMapping("/config")
    public String getConfig(){
        return "applicationName:"+applicationName+"\n"
                +"eurekaServer:"+eurekaServer+"\n"
                +"port:"+port;
    }

    @RequestMapping("/a")
    public String get(){
        return "aaa";
    }
}

复制代码
  1. 编写主启动类
package springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

复制代码
  1. 启动测试

image.png

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享