1 spring cloud config 配置中心
eureka只做注册中心,config只做配置中心,nacos是把两部分合并到了一起
配置中心是用来集中管理各个模块的配置文件
1.1 把本地项目上传到gitee仓库
1.1.1 把所有的配置单独放到一个文件夹中
先在springcloud1主工程下新建一个名为config
的文件夹,用于存放order-service、item-service、user-service的配置文件分别复制到这个config
文件夹内,并重命名为order-service-dev.yml
、item-service-dev.yml
、user-service-dev.yml
1.1.2 选择springcloud1工程目录
菜单 VCS => create git repository,创建git本地项目,即把springcloud1变成git项目
1.1.3 点击提交(对勾按钮)
1.1.4 勾选所有文件,填写信息,点击提交
1.1.5 点击Commit and Push
,或是Ctrl + Shift + K
快捷键
会弹出下面这个框,先在gitee上创建一个空的仓库,然后把空仓库的地址填写到这里
1.1.6 点击push
- 如果遇到以下错误
hint: to the same ref. You may want to first integrate the remote changes
Done
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
则说明线上的gitee仓库里面的有东西,可以点“管理=>清空仓库"清一下再上传就可以了
1.1.6 配置不覆盖
在config
文件夹下的各个yml文件中添加如下配置,表示从配置中心下载的配置不覆盖本地的配置,比如我们对同一个项目设置了两个启动配置文件,分别设置了两个不同的端口时,如果不做如下的配置,则启动后下载了配置中心的配置后,本地设置的这个端口就被覆盖掉了
spring:
cloud:
config:
override-none: true
1.2 搭建配置中心
1.2.1 新建spring-module:sp09-config
1.2.2 添加依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sp09-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp09-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
1.2.3 配置文件
server:
port: 6001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/zidieq/mypro
search-paths: config
eureka:
client:
service-url:
defaultZone: http://172.18.6.173:2001/eureka,http://172.18.6.173:2002/eureka
instance:
ip-address: 172.18.6.192
prefer-ip-address: true
1.2.4 主程序添加 @EnableConfigServer
和 @EnableDiscoveryClient
package cn.tedu.sp09config;
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;
@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Sp09ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09ConfigApplication.class, args);
}
}
1.2.5 启动测试
启动后,访问http://localhost:6001/order-service/dev
得到类似如下内容,则说明配置成功
{
"name": "order-service",
"profiles": [
"dev"
],
"label": null,
"version": "1ddce4896dc83ff77a25fc32afc9ee5c4f3e9c55",
"state": null,
"propertySources": [
{
"name": "https://gitee.com/zidieq/mypro/config/order-service-dev.yml",
"source": {
"spring.application.name": "order-service",
"server.port": 8201,
"eureka.client.service-url.defaultZone": "http://172.18.6.173:2001/eureka,http://172.18.6.173:2002/eureka",
"eureka.instance.ip-address": "172.18.6.192",
"eureka.instance.prefer-ip-address": true,
"ribbon.MaxAutoRetries": 0,
"ribbon.MaxAutoRetriesNextServer": 1
}
}
]
}