web后端-SpringCloud-Config分布配置

本文涉及的产品
云数据库 RDS MySQL,集群版 2核4GB 100GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: web后端-SpringCloud-Config分布配置

N.1 SpringCloudConfig介绍

1)Spring Cloud Config 为微服务架构中的微服务提供 集中式的外部配置支持, 配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置

————————————————————————

————————————————————————

2)Spring Cloud Config 分为 服务端与客户端 两个部分:

(1)服务端 config server:

也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密、解密信息等访问接口。配置服务器官方推荐采用 Git 来存储配置信息,这样就有助于对环境配置进行版本管理,并且可通过Git客户端工具来方便的管理与访问配置信息。

(2)客户端 config client:

通过指定的服务端来管理服务的资源,以及与业务相关的配置内容,并在启动的时候从服务端获取和加载配置信息。

3)作用

(1)集中管理配置文件

(2)不同环境不同配置,动态化的配置更新,根据不同环境部署,如 dev/test/prod

(3)运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置,服务会向配置中心统一拉取自已的配置信息.

(4)当配置发生变动时,服务不需要重启即可感知到配置的变化并使用修改后的配置信息

(5)将配置信息以REST接口的形式暴露

4)与 GitHub 整合配置信息

由于 Spring Cloud Config 官方推荐使用Git来管理配置文件(也可支持其他方式,如:SVN和本地文件),

而且使用https/http访问的形式。

N.2 Config的服务端和客户端配置

N.2.1 创建配置文件与提交Github

———————————————————————— ————————————————————————

————————————————————————

1)git 命令远程窗口克隆复制命令,将远程的窗口复制到本地:

git clone https://github.com/mengxuegu/microservice-cloud-config.git

注意 先在桌面创建好文件夹,方便整理。

————————————————————————

————————————————————————

————————————————————————

————————————————————————

2)将 application.yml 推送到 github 远程库

$ cd microservice-cloud-config

# 添加到暂存区

$ git add microservice-config-application.yml

# 提交到本地库

$ git commit -m "first commit" microservice-config-application.yml

# 推送到远程库origin 的 master 主分支上

$ git push origin master

————————————————————————

————————————————————————

————————————————————————

N.2.2 新建Config服务端模块

1)配置读取规则:

(1)/{application}-{profile}.yml

[1] /读取的配置文件名-环境配置项 (默认为master分支)

[2] 如:http://localhost:5001/microservice-config-application-dev.yml

(2)/{application}/{profile}[/{label}]

[1] /读取的配置文件名/环境配置项/分支名

[2] 如:http://localhost:5001/microservice-config-application/dev/master

(3)/{label}/{application}-{profile}.yml

[1] /分支名/读取的配置文件名/环境配置项

[2] 如:http://localhost:5001/master/microservice-config-application-dev.yml

2)microservice-cloud-11-config-server-5001代码

server:

port: 5001

spring:

application:

name: microservice-config

cloud:

config:

server:

git: # 远程库的git地址

uri: https://github.com/HMTX1/microservice-cloud-config.git

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

//开启配置中心功能

@EnableConfigServer //开启配置中心功能

@SpringBootApplication

public class ConfigServer_5001 {

public static void main(String[] args) {

SpringApplication.run(ConfigServer_5001.class, args);

}

}

————————————————————————

————————————————————————

————————————————————————

————————————————————————

————————————————————————

N.2.3 Config客户端服务

1)application.yml

server:

port: 8080 #默认是8080,但是 如果读取了github的文件 就会覆盖

spring:

application:

name: microservice-config-client

2)bootstrap.yml

spring:

cloud:

config:

name: microservice-config-application #github上的配置名称,注意没有yml后缀名

profile: prod #本次访问的环境配置项 那么使用的端口就是为4002 会覆盖8080

label: master #远程库的分支名

uri: http://localhost:5001 #Config配置中心地址,通过它获取microservice-configapplication.yml配置信息

import org.springframework.beans.factory.annotation.Value;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

@RestController

public class ConfigClient {

//会从github中的microservice-config-application.yml中获取

@Value("${spring.application.name}") //注意 远程文件 不能背直接加载,所以 ,是通过本地的文件 简介加载的 所以才可以反问

private String applicationName;

@Value("${server.port}")

private String port;

//请求访问

@RequestMapping("/config")

public String getConfig() {

String content = "applicationName:" + applicationName + ", port:" + port;

System.out.println("content: " + content);

return content;

}

}

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class ConfigClient_8080 {

public static void main(String[] args) {

SpringApplication.run(ConfigClient_8080.class, args);

}

}

————————————————————————

———————————————————————— ————————————————————————

N.3 Config配置实战

N.3.1 本地文件存到github

1)microservice-config-eureka.yml

#注意:如果在记事本上编写,下面的缩进不要使用Tab来缩进,不然报错

spring:

profiles:

active: dev # 激活开发环境配置

---

server:

port: 6001 #端口号

spring:

profiles: dev # 开发环境

application:

name: microservice-config-eureka

eureka:

instance:

hostname: eureka6001.com

client:

registerWithEureka: false

fetchRegistry: false

serviceUrl:

defaultZone: http://eureka6001.com:6001/eureka/

server:

enable-self-preservation: false # 禁用自我保护机制*****************

---

server:

port: 6001 #端口号

spring:

profiles: prod # 生产环境

application:

name: microservice-config-eureka

eureka:

instance:

hostname: eureka6001.com

client:

registerWithEureka: false

fetchRegistry: false

serviceUrl:

defaultZone: http://eureka6001.com:6001/eureka/

server:

enable-self-preservation: true # 开启自我保护机制*****************

#保存时注意,选择 UTF-8 保存

2)microservice-config-product.yml

#注意:如果在记事本上编写,下面的缩进不要使用Tab来缩进,不然报错

spring:

profiles:

active: dev # 激活开发环境配置

---

server:

port: 8001

mybatis:

config-location: classpath:mybatis/mybatis.cfg.xml

type-aliases-package: com.mengxuegu.springcloud.entities

mapper-locations: classpath:mybatis/mapper/**/*.xml

spring:

profiles: dev # 开发环境

application:

name: microservice-product-config # ******服务名*******

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8 #数据库连接地址,*****注意库名 db01***************

username: root

password: a

dbcp2:

min-idle: 5

initial-size: 5

max-total: 5

max-wait-millis: 150

eureka:

client:

registerWithEureka: true

fetchRegistry: true

serviceUrl:

defaultZone: http://eureka6001.com:6001/eureka

instance:

instanceId: ${spring.application.name}:${server.port}

prefer-ip-address: true

---

server:

port: 8001

mybatis:

config-location: classpath:mybatis/mybatis.cfg.xml

type-aliases-package: com.mengxuegu.springcloud.entities

mapper-locations: classpath:mybatis/mapper/**/*.xml

spring:

profiles: prod # 生产环境

application:

name: microservice-product-config # ******服务名*******

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: com.mysql.cj.jdbc.Driver

url: jdbc:mysql://127.0.0.1:3306/springcloud_db02?serverTimezone=GMT%2B8 #数据库连接地址,*****注意库名 db02***************

username: root

password: a

dbcp2:

min-idle: 5

initial-size: 5

max-total: 5

max-wait-millis: 150

eureka:

client:

registerWithEureka: true

fetchRegistry: true

serviceUrl:

defaultZone: http://eureka6001.com:6001/eureka

instance:

instanceId: ${spring.application.name}:${server.port}

prefer-ip-address: true

————————————————————————

————————————————————————

————————————————————————

N.3.2 构建Config版的Eureka和Product

————————————————————————

————————————————————————

————————————————————————

————————————————————————

相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
16天前
|
XML 前端开发 JavaScript
视频弹幕设计网站02-----数据库设计与后端配置
视频弹幕设计网站02-----数据库设计与后端配置
|
14天前
|
Java API 网络架构
Spring Cloud Gateway的高级配置与实践
Spring Cloud Gateway的高级配置与实践
|
16天前
|
Java
大事件后端项目04-----yml配置信息书写和获取,邮箱如何设计
大事件后端项目04-----yml配置信息书写和获取,邮箱如何设计
|
21天前
|
运维 Java 测试技术
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
Spring运维之boo项目表现层测试加载测试的专用配置属性以及在JUnit中启动web服务器发送虚拟请求
19 3
|
20天前
|
缓存 前端开发 JavaScript
Parcel-极速零配置Web应用打包工具
Parcel-极速零配置Web应用打包工具
22 1
|
10天前
|
前端开发 JavaScript Linux
若依修改之后,无法访问前端项目如何解决,只能访问后端的接口,我的接口8083,端不显示咋解决?在vue.config.js文件中的映射路径要跟后端匹配,到软件商店里找到Ngnix配置代理,设80不用加
若依修改之后,无法访问前端项目如何解决,只能访问后端的接口,我的接口8083,端不显示咋解决?在vue.config.js文件中的映射路径要跟后端匹配,到软件商店里找到Ngnix配置代理,设80不用加
文本vitepress,如何设置背景图,如何插入背景图,如何插入logo,为了放背景图片,我们要新建pubilc的文件夹,插入logo要在config.js中进行配置,注意细节,在添加背景时,注意格式
文本vitepress,如何设置背景图,如何插入背景图,如何插入logo,为了放背景图片,我们要新建pubilc的文件夹,插入logo要在config.js中进行配置,注意细节,在添加背景时,注意格式
|
11天前
|
资源调度 前端开发
文本,vitepress的使用,如何使用vitevitepress没有config.js该怎么办?这里使用vitepress进行手动配置,参考只爭朝夕不負韶華的文章
文本,vitepress的使用,如何使用vitevitepress没有config.js该怎么办?这里使用vitepress进行手动配置,参考只爭朝夕不負韶華的文章
|
14天前
|
JavaScript
vue 配置【详解】 vue.config.js ( 含 webpack 配置 )
vue 配置【详解】 vue.config.js ( 含 webpack 配置 )
17 0
|
20天前
|
Nacos 数据安全/隐私保护
springCloud之nacos服务注册与发现、配置中心
springCloud之nacos服务注册与发现、配置中心
30 0

热门文章

最新文章