web后端-SpringCloud-Config分布配置

本文涉及的产品
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

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

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

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

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

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
18小时前
|
前端开发 Java Go
从前端到后端:构建现代化Web应用的技术演进
本文探讨了从前端到后端的技术演进,介绍了前端、后端以及多种编程语言,如Java、Python、C、PHP和Go,以及数据库在构建现代化Web应用中的应用。通过深入剖析各个技术领域的发展和应用,读者将对构建高效、可扩展、安全的Web应用有更深入的理解。
|
3天前
|
缓存 监控 API
利用Python构建高性能的Web API后端服务
随着微服务架构的普及和RESTful API的广泛应用,构建高性能、可扩展的Web API后端服务变得尤为重要。本文将探讨如何利用Python这一强大且灵活的语言,结合现代Web框架和工具,构建高效、可靠的Web API后端服务。我们将分析Python在Web开发中的优势,介绍常用的Web框架,并通过实际案例展示如何设计并实现高性能的API服务。
|
3天前
|
移动开发 前端开发 JavaScript
Vue2 系列:vue.config.js 参数配置
Vue2 系列:vue.config.js 参数配置
11 2
|
3天前
|
前端开发 Java Go
从前端到后端:构建现代化Web应用的技术实践
本文将介绍如何通过前端和后端技术相结合,构建现代化Web应用的技术实践。我们将探讨前端开发、后端架构以及多种编程语言(如Java、Python、C、PHP、Go)在构建高效、可扩展的Web应用中的应用。
|
5天前
|
网络协议 网络虚拟化
网工配置命令总结(1)---Web访问及vlan配置
网工配置命令总结(1)---Web访问及vlan配置
5 0
|
5天前
|
前端开发 API
nuxt.config.js 配置
我们在使用Nuxt.js提供的create-nuxt-app 创建项目后,更希望对它自定义一些东西,这里我们可以在根目录下找到nuxt.config.js
21 7
|
5天前
|
Go
golang学习3,golang 项目中配置gin的web框架
golang学习3,golang 项目中配置gin的web框架
|
5天前
|
XML SQL Java
SpringCloud 基础配置
SpringCloud 基础配置
13 0
|
5天前
|
开发框架 JSON .NET
.Net4.0 Web.config 配置实践
.Net4.0 Web.config 配置实践
|
5天前
|
JSON JavaScript 前端开发
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
vue2_vite.config.js的proxy跨域配置和nginx配置代理有啥区别?
34 1

热门文章

最新文章