当前位置:网站首页 > Vue.js开发 > 正文

swagger2.0访问路径(swagger的访问)



Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,Server提供配置文件的存储以接口的形式将配置文件的内容提供出去,Client通过接口获取数据并依据此数据初始化自己的应用。目前SpringCloud Config的Server主要是通过Git方式做一个配置中心,然后每个服务从Server获取自身配置所需的参数。

实现基于config来管理项目的配置文件

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-config</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
</dependencies></project>

server

@EnableConfigServer 注解的作用是启用 Spring Cloud Config Server 功能,使当前应用能够作为一个配置服务器来集中管理配置文件。使用 @EnableConfigServer 注解后,应用程序可以提供集中化的配置服务,允许客户端应用从该服务器拉取配置文件。 具体来说,@EnableConfigServer 会:

  1. 启动配置服务器:通过它,应用程序可以接收和处理配置请求,为其他应用提供配置数据。
  2. 支持多种存储方式:配置服务器可以从多种配置源(如 Git、文件系统、本地仓库或数据库等)加载配置文件,并根据请求路径或参数动态选择配置文件。
  3. 支持不同环境配置:它能根据客户端应用的 spring.profiles.active 等参数提供不同环境(如 devprod)的配置。

配合 @EnableConfigServer 注解的配置服务器可以通过 REST API 提供配置数据,客户端应用只需指定配置服务器的地址,即可从服务器拉取和加载所需的配置信息

package com.et;
import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication@EnableConfigServerpublic class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); }}

服务端配置文件

  • spring.cloud.config.server:表示该服务将作为配置服务器,用于向其他应用提供集中化的配置管理。
  • native:指定配置服务器的模式为“本地模式”,即配置文件保存在应用内部的目录或类路径下。这样,应用可以从本地文件系统读取配置,而不是从外部 Git 仓库或其他远程配置源读取。
  • search-locations:定义本地配置文件的路径,这里设置为 classpath:/config-repo/crmclasspath:/config-repo/client,表示配置服务器会从应用程序的类路径中的这两个文件夹加载配置文件。
  • bootstrap: true:启用 bootstrap.yml 加载过程,确保配置加载的优先级较高,通常用于在应用启动时获取外部配置源的信息。
server: port: 8888
spring: profiles: active: native cloud: config: server: # git: # uri: https://github.com/chinoukin/SpringcloudConfig native: search-locations: classpath:/config-repo/crm,classpath:/config-repo/client bootstrap: true

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>spring-cloud-config</artifactId> <groupId>com.et</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>config-client</artifactId>
<properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> </properties> <dependencies>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> </dependency>
</dependencies></project>

controller

获取服务端托管的配置文件值

package com.et.controller;
import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RestController;
/ * @author liuhaihua * @version 1.0 * @ClassName DemoController * @Description todo * @date 2024/11/01/ 13:47 */@RestControllerpublic class DemoController { @Value("${example.property}") private String exampleProperty;
@GetMapping("/property") public String getProperty() { return exampleProperty; }}

客户端启动类

Spring Cloud 使用 ConfigServicePropertySourceLocator 类来实现配置自动装载。这个类会在引导阶段根据 spring.cloud.config.uri 连接配置服务器,并获取指定的配置文件内容:

  1. 配置文件加载:客户端应用在启动时,会自动向配置服务器发送 HTTP 请求,获取以 application-nameprofile 为参数的配置文件(如 /client-app/dev)。
  2. 配置源添加ConfigServicePropertySourceLocator 会将从配置服务器获取的配置信息转换为 Spring 的 PropertySource,并添加到应用的 Environment 中,供应用程序使用。
package com.et;

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplicationpublic class ConfigClientApplication {


public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }

}

客户端配文件bootstrap.yml

  • spring.application.name:指定客户端的应用名称,这个名称用于从配置服务器上找到对应的配置文件。
  • spring.cloud.config.uri:配置服务器的 URI 地址。客户端会从这个地址获取配置。

spring: cloud: config: uri: http://127.0.0.1:8888/ #name: crm,config-client name: config-client profile: dev

以上只是一些关键代码,所有代码请参见下面代码仓库

  • https://github.com/Harries/springcloud-demo(spring cloud config)

  1. 启动server应用(http://127.0.0.1:8888/config-client-dev.yml)
  2. 启动客户端应用
  3. 访问http://127.0.0.1:8080/property
  4. 返回配置文件中配置值client-dev

  • https://docs.spring.io/spring-cloud-config/docs/current/reference/html/
  • https://www.liuhaihua.cn/archives/711631.html

到此这篇swagger2.0访问路径(swagger的访问)的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • Vue安装脚手架(vue安装脚手架3.0用管理员)2025-01-29 14:54:04
  • map转jsonobject对象(map转对象 jsonobject.fromobject)2025-01-29 14:54:04
  • Redhat9.3(redhat9.3 下载)2025-01-29 14:54:04
  • vue安装router命令(vue-router安装)2025-01-29 14:54:04
  • vue安装使用(vue的安装和使用)2025-01-29 14:54:04
  • map转json数组(map转成json字符串)2025-01-29 14:54:04
  • nvme接口引脚定义(nvme2.0接口)2025-01-29 14:54:04
  • pcie5.0能插pcie4.0么(pcie 5.0 和pcie 4区别)2025-01-29 14:54:04
  • k8s版本升级 二进制 1.15 到1.19(k8s1.20二进制部署)2025-01-29 14:54:04
  • vmware6.0密钥(vmware密钥有什么用)2025-01-29 14:54:04
  • 全屏图片