×

springcloudconfig本地配置中心 pring

springcloudconfig本地配置中心(Springboot2.0 springconfig配置中心 bus-refresh发送给config-client为404)

admin admin 发表于2023-11-24 12:32:39 浏览38 评论0

抢沙发发表评论

这篇文章给大家聊聊关于springcloudconfig本地配置中心,以及Springboot2.0 springconfig配置中心 bus-refresh发送给config-client为404对应的知识点,希望对各位有所帮助,不要忘了收藏本站哦。

本文目录

Springboot2.0 springconfig配置中心 bus-refresh发送给config-client为404

我也出现了这个问题,后来发现是spring-cloud-bus-amqp版本不对,一开始这样不行:

《dependency》    《groupId》org.springframework.cloud《/groupId》    《artifactId》spring-cloud-starter-bus-amqp《/artifactId》    《version》2.2.0.M3《/version》《/dependency》

没有接口:

没有接口

后来改成这样:

《dependency》    《groupId》org.springframework.cloud《/groupId》    《artifactId》spring-cloud-starter-bus-amqp《/artifactId》    《version》2.1.3.RELEASE《/version》《/dependency》

然后就出现了bus-refresh接口:

spring cloud config加载本地配置文件,怎么访问

项目,需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改spring中配置的数据源datasource,在网上找了很多资料,最后找到了适合我的方法,下面总结一下。 spring的配置文件是在容器启动的时候就加载到内存中的,如果手动改了application.xml,我们必须要重新启动服务器配置文件才会生效。而在spring中提供了一个类WebApplicationContext,这个类可以让你获得一些bean,可以修改内存中的信息,我就是通过这个类来实现的。下面是我具体的代码。 package com.southdigital.hospital; import java.io.IOException; import javax.servlet.ServletContext; import javax.servlet.ServletException; ***隐藏网址******隐藏网址******隐藏网址***import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.WebApplicationContextUtils; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ChangeSpringConfig extends HttpServlet { private String ipAddress = "127.0.0.1"; /** * The doGet method of the servlet. 《br》 * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. 《br》 * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //先取得servleContext对象,提供给spring的WebApplicationUtils来动态修改applicationContext.xml ipAddress = request.getParameter("ipAddress"); System.out.println(ipAddress); ServletContext servletContext = this.getServletContext(); WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource");cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh"); } } 注意:通过这种方法修改applicationContext.xml文件的时候用c3p0,而不可以用dbcp,dbcp不支持动态修改读取到内存里面的数据。spring 3.1已经支持了。

SpringBoot的默认配置文件是什么

对SpringBoot来说,虽然application.yml配置文件更加常见,但是其实默认配置文件是application.properties,当然其格式可以是properties也可以是yaml格式;除此之外,其配置文件也可以是bootstrap.yml。这个配置文件是SpringCloud新增的启动配置文件,它的特点和用途:- bootstrap比application优先加载- 由于bootstrap比application更早加载,所以application不会被它覆盖- 使用配置中心Spring Cloud Config时,需要在bootstrap中配置一下配置中心地址,从而实现从配置中心拉取配置项到当前服务中如果你对默认配置文件是什么不理解,就去黑马程序员官网视频库看免费视频。

配置中心Config

随着项目日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多。某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错。配置中心就是是解决此类问题的。

市面上开源的配置中心有很多,BAT每家都出过,360的QConf、淘宝的diamond、百度的disconf都是解决这类问题。国外也有很多开源的配置中心Apache Commons Configuration、owner、cfg4j等等。

为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。

由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:SVN 仓库、本地化文件系统。

pom.xml中引入 spring-cloud-config-server 依赖,完整依赖配置如下:

创建Spring Boot的程序主类,并添加 @EnableConfigServer 注解,开启Config Server

application.properties`中配置服务信息以及git信息,例如:

Spring Cloud Config也提供 本地存储配置 方式。我们只需要设置属性spring.profiles.active=native,Config Server会默认从应用的src/main/resource目录下检索配置文件。也可以通过spring.cloud.config.server.native.searchLocations=file:F:/properties/属性来指定配置文件的位置。

准备一个 Git 仓库,在 上面创建了一个文件夹 config-repo 用来存放配置文件,为了模拟生产环境,我们创建以下三个配置文件:

其中设置了一个from属性,为每个配置文件分别设置了不同的值,如:

为了测试版本控制,在master中,我们都加入1.0的后缀,同时创建一个 config-label-test分支 ,并将各配置文件中的值用2.0作为后缀。

URL与配置文件的映射关系如下:

上面的url会映射 {application}-{profile}.properties ** 对应的配置文件, {label} 对应git上不同的分支,默认为master。 先找给定名称的配置文件,如果找不到去application.yml/application.properties找

***隐藏网址***

创建一个Spring Boot应用,在pom.xml中引入spring-cloud-starter-config依赖,完整依赖关系如下:

搭建srpingcloud-config server端,配置文件可以用application.yml 或 application.properties,config client端却要使用bootstrap.yml或bootstrap.properties。

格外注意(config client端) :上面这些属性必须配置在 bootstrap .properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而 bootstrap.properties的加载也是先于application.properties (bootstrap.yml 的加载也是先于 application.yml)。

创建一个Rest Api来返回配置中心的from属性,具体如下:

***隐藏网址***

有时,我们需要对配置内容做一些实时更新。server端会自动读取最新提交的内容,client端不会自动获取新的信息。

***隐藏网址***

下面进行改造:在config-client端增加一些内容和操作以实现配置的刷新

在config-clinet的 pom.xml 中新增 spring-boot-starter-actuator 监控模块,其中包含了 /refresh 刷新API。

增加spring-boot-starter-actuator 包,这是一套监控的功能,可以监控程序在运行时状态,其中就包括 /actuator/refresh`的功能。

需要给 加载变量的类上 面加载 @RefreshScope ,在客户端执行 /actuator/refresh 的时候就会更新此类下面的变量值。

Spring Boot 1.5.X 以上默认开通了安全认证,所以要在配置文件 application.yml 中添加以下配置以将 /actuator/refresh 这个 Endpoint 暴露出来

改造完之后,我们以 POST***隐藏网址***

加载 bootstrap. ------》连接config server 加载远程设置 --------》 加载application.的配置**

bootstrap 放一些你不想改的值

本地配置和远程配置冲突 以远程的为准

关于本次springcloudconfig本地配置中心和Springboot2.0 springconfig配置中心 bus-refresh发送给config-client为404的问题分享到这里就结束了,如果解决了您的问题,我们非常高兴。