web后端-SpringCloud-Config分布配置

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 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天前
|
前端开发 JavaScript 关系型数据库
从前端到后端:构建现代化Web应用的技术探索
在当今互联网时代,Web应用的开发已成为了各行各业不可或缺的一部分。从前端到后端,这篇文章将带你深入探索如何构建现代化的Web应用。我们将介绍多种技术,包括前端开发、后端开发以及各种编程语言(如Java、Python、C、PHP、Go)和数据库,帮助你了解如何利用这些技术构建出高效、安全和可扩展的Web应用。
|
1月前
|
弹性计算 负载均衡 容灾
slb配置后端服务器组
配置阿里云SLB后端服务器组涉及四个主要步骤:创建服务器组、添加ECS实例、关联监听规则和设定负载均衡策略。这使得流量根据业务需求和服务器特性进行转发,便于应用架构的灵活管理和扩展,支持蓝绿部署、灰度发布,并通过多可用区提升系统可用性和容灾能力。
26 3
|
1月前
|
缓存 关系型数据库 API
后端开发:构建高效、可扩展的Web应用程序的关键
后端开发:构建高效、可扩展的Web应用程序的关键
22 0
|
2天前
|
前端开发 JavaScript Java
前端与后端:构建现代Web应用的双翼
前端与后端:构建现代Web应用的双翼
|
11天前
|
缓存 负载均衡 数据库
优化后端性能:提升Web应用响应速度的关键策略
在当今数字化时代,Web应用的性能对于用户体验至关重要。本文探讨了如何通过优化后端架构和技术手段,提升Web应用的响应速度。从数据库优化、缓存机制到异步处理等多个方面进行了深入分析,并提出了一系列实用的优化策略,以帮助开发者更好地应对日益增长的用户访问量和复杂的业务需求。
16 1
|
19天前
|
SpringCloudAlibaba Java Nacos
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
SpringCloud Alibaba微服务 -- Nacos使用以及注册中心和配置中心的应用(保姆级)
|
26天前
Springcloud-ribbon和hystrix配置
Springcloud-ribbon和hystrix配置
8 0
|
1月前
|
数据库
最全三大框架整合(使用映射)——struts.xml和web.xml配置
最全三大框架整合(使用映射)——数据库资源文件jdbc.properties
10 0
|
1月前
|
消息中间件 SpringCloudAlibaba Java
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
【Springcloud Alibaba微服务分布式架构 | Spring Cloud】之学习笔记(八)Config服务配置+bus消息总线+stream消息驱动+Sleuth链路追踪
785 0
|
1月前
|
Java 关系型数据库 应用服务中间件
JAVA Web项目开发eclipse工具包配置(第一天)
JAVA Web项目开发eclipse工具包配置(第一天)