Git工具
下载完,配置一下用户名和用户邮箱
- 输入
git config --list
显示当前的config配置
- 发现没有用户名和用户邮箱的配置
- 输入
git config --global user.name "suisuinian"
配置用户名 - 输入
git config --global user.email "1906713811@qq.com"
配置邮箱 - 再输入
git config --list
显示当前的config配置
- 发现用户名和用户邮箱已经配置
使用码云进行代码托管
- 先注册码云账号
- 得到SSH公钥(通过Git工具)
- 输入
ssh-keygen -t rsa -C "1906713811@qq.com"
- 按三次回车,得到公钥
- 输入
cat ~/.ssh/id_rsa.pub
查看公钥
- 输入
- 设置SSH公钥(在码云上设置)
在码云上创建一个仓库,并下载这个远程仓库到本地
将该填的信息填一下
实现本地和远程仓库的调用
- 拿到你创建的远程仓库的SSH的地址,我这里是
git@gitee.com:asfasfasda/springcloud-config.git
- 创建一个本地仓库的文件夹
- 进入该文件夹,右键,使用Git工具命令
git clone git@gitee.com:asfasfasda/springcloud-config.git
,下载远程仓库的代码到本地仓库 - 结果:
在本地仓库新增文件,提交到远程仓库(码云上)
- 新建
application.yml
spring:
profiles:
active: dev
---
spring:
profiles: dev
application:
name: springcloud-config-dev
---
spring:
profiles: test
application:
name: springcloud-config-test
复制代码
- 进去我们创建的文件夹,右键,启动Git工具
- 输入
git add .
,添加application.yml
- 输入
git status
查看我们添加与否
-
输入
git commit -m "add application.yml"
将它提交到本地仓库,其中,-m 后写的是本次修改的信息 -
输入
git push origin master
将它提交到码云上
其中,origin
代表当前用户,master
表示提交到码云的远程仓库的master分支
- 刷新我们在码云托管的仓库,观察是否提交成功。
Config
简介
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client
和Server
两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。
实验操作
服务端
- 导入依赖
<!-- 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>
复制代码
- 编写配置,远程仓库就使用我上一篇博客创建的远程仓库
server:
port: 3344
spring:
application:
name: springcloud-config-server
# 连接远程仓库 uri为远程仓库中http的地址
cloud:
config:
server:
git:
uri: https://gitee.com/asfasfasda/springcloud-config.git
复制代码
- 编写主启动类
@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);
}
}
复制代码
- 启动测试
访问的url如下:
其中,label:远程仓库分支 profile: application.yml中的分支
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
客户端
- 再创建一个
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/
复制代码
- 导入依赖
<!--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>
复制代码
- 编写aplication.yml配置
# 用户级别的配置
spring:
application:
name: springcloud-config-client
复制代码
- 编写bootstrap.yml配置
# 系统级别的配置
spring:
cloud:
config:
name: config-client #拿到远程仓库的资源config-client.yml,这里不需要填后缀
profile: dev
label: master
uri: http://localhost:3344
复制代码
- bootstrap.yml: 系统级别的配置
- aplication.yml: 用户级别的配置
- 编写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";
}
}
复制代码
- 编写主启动类
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);
}
}
复制代码
- 启动测试
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END