一. 在GitHub中创建仓库

  1. 浏览器输入地址https://github.com/,并登录,进入页面后,单击【New】,为我们的Spring Cloud Config创建仓库,存储配置文件
    在这里插入图片描述输入信息
    在这里插入图片描述在本地创建config文件夹,文件夹中创建eureka文件夹,将eureka的配置文件拷贝进去,名字前面加入eureka,如下:
    在这里插入图片描述并分别创建dev,test,prod文件,然后将config文件夹拖进GitHub
    在这里插入图片描述

二. 使用ssh生成密钥

注意,需要RSA的密钥,如果你生成的私钥中写明为OPENSSH的,需要重新生成RSA的 ssh-keygen -t rsa -C “email”
在这里插入图片描述

三. Github中配置公钥

如果没有配置SSH,请单击【New SSH key】进行配置,输入自定义名称及公钥内容(id_rsa.pub的内容)
在这里插入图片描述

四. 创建config server

在这里插入图片描述在这里插入图片描述
修改appliation.properties文件为application.yml,并加入下面内容:

server:
  port: 9002
spring:
  application:
    name: configserver
  cloud:
    config:
      server:
        git:
          uri: git@github.com:itzhiya/springcloudconfig.git
          ignore-local-ssh-settings: true
          privateKey: |
            -----BEGIN RSA PRIVATE KEY-----
            Proc-Type: 4,ENCRYPTED
            DEK-Info: AES-128-CBC,7626CB639E11749A9BBA405DFF4CCE67
			.......此处省略
            -----END RSA PRIVATE KEY-----
          passphrase: 1314521888nv #生成ssh密钥时设置的密码
          search-paths:
            - config/eureka

启动类中加入注解@EnableConfigServer

@SpringBootApplication
@EnableConfigServer
public class SpringcloudconfigserverApplication {

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

}

五. 创建spring cloud config client

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
在resources中创建bootstrap.yml文件,内容如下:

spring:
  application:
    name: springcloudconfigclient
  cloud:
    config:
      uri: http://localhost:9002/
      name: eurekaapplication
      profile: dev  

创建Controller,进行测试

package com.itzhiya.spring.cloud.config.client;

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

@RestController
public class ApplicationController {

    @GetMapping("hello")
    public String getApplicationName(){
        return "hello, welcome to spring cloud config!";
    }
}

启动服务
浏览器输入路径http://localhost:8001/hello
在这里插入图片描述
推荐文章

系统上线后雪崩!让我们来学习 Spring Cloud Hystrix 及监控来解决雪崩问题

10 分钟教会你 Spring Boot 集成 Thymeleaf、MyBatis 完成产品的增删改查

【高阶用法】一个实例学会 Spring Cloud 的注册中心 Eureka的使用

Spring Cloud gateway与注册中心Eureka的完美集成

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐