【Spring全家桶】Spring Cloud Config案例(通过GitHub获取仓库文件)
一. 在GitHub中创建仓库浏览器输入地址https://github.com/,并登录,进入页面后,单击【New】,为我们的Spring Cloud Config创建仓库,存储配置文件输入信息在SourceTree中创建新的分支2. 创建config-server修改pom.xml文件修改appliation.properties文件为application.yml,并加入下面...
·
一. 在GitHub中创建仓库
- 浏览器输入地址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 完成产品的增删改查
更多推荐
已为社区贡献1条内容
所有评论(0)