Java-118-SpringCloud-8-config

简介: 在传统项目中,配置信息一般都在配置文件(application.properties)、操作系统变量、jar命令启动时的环境变量参数,如果需要更改配置就需要重新构建项目再发布。并且如数据库连接信息等安全性也有风险。所以Spring Cloud 提供了Spring Cloud Config来进行集中式配置管理。

在传统项目中,配置信息一般都在配置文件(application.properties)、操作系统变量、jar命令启动时的环境变量参数,如果需要更改配置就需要重新构建项目再发布。并且如数据库连接信息等安全性也有风险。所以Spring Cloud 提供了Spring Cloud Config来进行集中式配置管理。

Spring Cloud Config分为Server端和Client端:

Spring Cloud Config Server是Spring Cloud为指定应用中所有服务提供集中式配置的一个服务,实现集中管理所有应用的配置,避免重复配置。

Spring Cloud Config Client可以通过配置获取到Server中的配置信息。


       Spring Cloud Config支持git存储配置文件,也支持本地存储。

       下面建立一个本地存储的例子试试。

一、通过Spring Cloud Config Server 获取到本地配置

1.pom.xml

<dependencies>

 <!--web 组件 -->

 <dependency>

     <groupId>org.springframework.boot</groupId>

     <artifactId>spring-boot-starter-web</artifactId>

 </dependency>

 <!-- eureka client 组件-->

 <dependency>

     <groupId>org.springframework.cloud</groupId>

     <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

 </dependency>

 <dependency>

     <groupId>org.springframework.cloud</groupId>

     <artifactId>spring-cloud-config-server</artifactId>

 </dependency>

</dependencies>

2.启动类中使用@EnableConfigServer注解启用配置服务

package com.xing.study.cloud.config;


import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

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



/**

* @author rt

*/

@SpringBootApplication

@EnableConfigServer

public class ConfigServerApplication {

   public static void main(String[] args) {

       SpringApplication.run(ConfigServerApplication.class, args);

   }

}

3.配置文件application.properties

spring.application.name=config-server

server.port=8889


# 注册到eureka

eureka.instance.instance-id=config-server

eureka.client.service-url.defaultZone=http://172.23.13.15:8881/eureka/

eureka.client.register-with-eureka=true

eureka.client.fetch-registry=true


##配置文件存Git

#spring.cloud.config.server.git.uri=http://github/xh/config.git

#spring.cloud.config.label=master

#spring.cloud.config.server.git.search-paths=/**

#spring.cloud.config.server.git.username=username

#spring.cloud.config.server.git.password=password


#配置文件存本地

#注意:文件夹要有访问权限

spring.profiles.active=native

spring.cloud.config.server.native.search-locations=D:/temp


        可以注册到eureka来实现配置动态刷新。最好是用git的方式来试试。

4.配置文件指向的D:/temp,在这里放配置文件

图片

       里面就放了一个配置:token=mi

这里的命名要和后面访问时的匹配。

application指应用名称,我们的配置文件名称应按照application-{profile}.yml命名。

profile指环境信息,比如生产环境【prod】、开发环境【dev】、测试环境【test】

label指git分支,比如master

5.访问测试

访问格式:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

结果:

图片


二、通过Spring Cloud Config Client获取到server的配置

1.pom依赖

<dependencies>

   <!--web 组件 -->

   <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>

</dependencies>

2.配置bootstrap.yml:

# 使用config server中的配置

spring:

 cloud:

   config:

     uri: http://localhost:8889 # 如果注册到eureka,可以直接配置Config Server服务名 比如config-server

     label: master

     profile: active

     fail-fast: true

图片


3.输出配置

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

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

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


/**

* @author rt

*/

@RestController

public class TokenController {

   @Value("${token}")

   private String token;


   @RequestMapping("/token")

   public String token() {

       return token;

   }

}

4.公司有网控,上不去git,所以我没有使用git来测试。。。了解了就是啦


       配置和应用进行隔离,提升了安全性,也避免了重复配置。


END

目录
相关文章
|
人工智能 并行计算 openCL
魔搭+Xinference 平台:CPU,GPU,Mac-M1多端大模型部署
随着 Llama2 的开源,以及通义千问、百川、智谱等国内大模型的问世,很多用户有了本地部署去尝试大模型的需求,然而硬件的需求阻碍了很多人的尝试,并不是所有人都拥有一块英伟达显卡的,所以 Llama2 问世不久,大神 Andrej Karpathy 的一个 weekend project 爆火——llama2.c。
魔搭+Xinference 平台:CPU,GPU,Mac-M1多端大模型部署
|
安全 Ubuntu Linux
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?
在Docker中,镜像内没有curl,kill,ipconfig等指令如何添加?
|
机器学习/深度学习 算法 PyTorch
深度学习笔记(十三):IOU、GIOU、DIOU、CIOU、EIOU、Focal EIOU、alpha IOU、SIOU、WIOU损失函数分析及Pytorch实现
这篇文章详细介绍了多种用于目标检测任务中的边界框回归损失函数,包括IOU、GIOU、DIOU、CIOU、EIOU、Focal EIOU、alpha IOU、SIOU和WIOU,并提供了它们的Pytorch实现代码。
2738 1
深度学习笔记(十三):IOU、GIOU、DIOU、CIOU、EIOU、Focal EIOU、alpha IOU、SIOU、WIOU损失函数分析及Pytorch实现
|
SQL Oracle 关系型数据库
SqlAlchemy 2.0 中文文档(六十七)(1)
SqlAlchemy 2.0 中文文档(六十七)
230 0
|
小程序 安全 搜索推荐
【微信小程序开发实战项目】——个人中心页面的制作
本文介绍了如何设计和实现一个网上花店的微信小程序,包括个人中心、我的订单和我的地址等功能模块。个人中心让用户能够查看订单历史、管理地址和与客服互动。代码示例展示了`own.wxml`、`own.wxss`和`own.js`文件,用于构建个人中心界面,包括用户信息、订单链接、收藏、地址、客服和版本信息。我的订单部分展示了订单详情,包括商品图片、名称、销量、价格和订单状态,用户可以查看和管理订单。我的地址功能允许用户输入和编辑收货信息,包括联系人、性别、电话、城市和详细地址。每个功能模块都附有相应的WXML和WXSS代码,以及简洁的样式设计。
885 0
【微信小程序开发实战项目】——个人中心页面的制作
|
数据库
uniapp 【专题详解 -- 时间】云数据库时间类型设计,时间生成、时间格式化渲染(uni-dateformat 组件的使用)
uniapp 【专题详解 -- 时间】云数据库时间类型设计,时间生成、时间格式化渲染(uni-dateformat 组件的使用)
503 0
|
缓存 关系型数据库 MySQL
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
1594 0
提升mysql性能的关键参数之innodb_buffer_pool_size、innodb_buffer_pool_instances
|
机器学习/深度学习 PyTorch 算法框架/工具
pytorch中nn.ReLU()和F.relu()有什么区别?
pytorch中nn.ReLU()和F.relu()有什么区别?
987 0
|
数据可视化 Ubuntu Linux
PyCharm连接远程服务器配置的全过程
相信很多人都遇见过这种情况:实验室成员使用同一台服务器,每个人拥有自己的独立账号,我们可以使用服务器更好的配置完成实验,毕竟自己哪有money拥有自己的3090呢。 通常服务器系统采用Linux,而我们平常使用频繁的是Windows系统,二者在操作方面存在很大的区别,比如我们实验室的服务器采用Ubuntu系统,创建远程交互任务时可以使用Terminal终端或者VNC桌面化操作,我觉得VNC很麻烦,所以采用Terminal进行实验,但是Terminal操作给我最不好的体验就是无法可视化中间实验结果,而且实验前后的数据上传和下载工作也让我头疼不已。
|
存储 关系型数据库 MySQL
MySQL数据库开发进阶:精通数据库表的创建与管理22
【7月更文挑战第22天】数据库的创建与删除,数据表的创建与管理
177 1