如何配置SpringCloud Config 全局配置中心
1.在git上创建配置文件远程仓库
2.在将仓库目录克隆到本地
复制链接
本地磁盘创建一个文件夹
右击选择Git Bash Here 将项目导入本地
依次执行以下操作
$ git clone 你刚刚复制的路径
文件夹已经显示
在目录中创建三个配置(环境名:dev开发(development environment)、test测试(testing environment)、pro正式(production environment))
配置文件名称定义:实例名称-环境名称.yml
以我的生产者项目为例子 application:name 就是我的实例名称
创建三个配置
将你的开发工具中的applapplication.yml中的配置直接ctrl+A和ctrl+X剪切粘贴到你刚刚创建的文件夹中的三个配置中,注意通用配置如下,根据环境的不同数据库连接的ip地址肯定会发生改变,根据不同需求配置不同的数据库配置。
# testing environment
server:
port: 8092
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
提交你的springcloud-config配置文件到远程仓库,还是在当前目录下鼠标右击Git Bash Here 将项目上传
依次执行以下操作
$ git add .
$ git commit -m " "
$ git push
上传到远程仓库
3.创建config server项目选择Config Server 和 Eureka Discover
4.在它的配置类中配置信息如下:
server:
port: 8081
spring:
application:
name: springcloud-config
cloud:
config:
server:
git:
uri: Git Bush 路径
search-paths: /
username:
password:
label: master
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
uri的路径为
5.在它启动上添加注解
@EnableEurekaClient
@EnableConfigServer
package com.jk;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
//在启动上添加注解
@EnableEurekaClient
@EnableConfigServer
public class SpringcloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringcloudConfigApplication.class, args);
}
}
6.配置server client项目,以生产者项目为例,将配置文件的名称改为bootstrap.yml,注意官网规定,必须叫这个名。
7.配置bootstrap.yml文件
server:
port: 8765
spring:
application:
name: provider-news
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/math?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf-8
username: root
password: root
data:
mongodb:
uri: mongodb://127.0.0.1:27017/1809a
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
timeout: 500
cloud:
config:
label: master
profile: dev
discovery:
enabled: true
service-id: springcloud-config
mybatis:
mapper-locations: classpath:mapper/*.xml
8.启动Eureka和config项目
访问eureka注册中心,发现已经注册上了
访问成功了
结束!