springcloud 配置中心

配置中心简介

Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持。配置服务器为各应用的所有环境提供了一个中心化的外部配置。它实现了对服务端和客户端对Spring Environment和PropertySource抽象的映射,所以它除了适用于Spring构建的应用程序,也可以在任何其他语言运行的应用程序中使用。作为一个应用可以通过部署管道来进行测试或者投入生产,我们可以分别为这些环境创建配置,并且在需要迁移环境的时候获取对应环境的配置来运行

配置中心原理

在这里插入图片描述

注册用户:
进入 https://github.com 注册用户。
在这里插入图片描述

创建项目:

在这里插入图片描述

创建成功,点击进入
在这里插入图片描述

添加文件:
在这里插入图片描述

启用配置中心

在这里插入图片描述

Main类:

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

文件 引入依赖:
网址:https://cloud.spring.io/spring-cloud-static/Edgware.SR5/single/spring-cloud.html#_spring_cloud_config
依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
   </dependency>
</dependencies>

配置:

@SpringBootApplication
@EnableConfigServer
public class ConfigServer {

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

配置存储库: application。

server:
  port: 1111
spring:
  cloud:
    config:
      server:
        git:
          uri: https://github.com/346913/yudu

测试:
浏览器格式:{[/{name}-{profiles}.properties],methods=[GET]}"
浏览器查询:http://localhost:1111/userservice-dev.properties (用properties 取得properties 格式显示)

{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}
http://localhost:1111/userservice-dev.yml (用yml 取得yml 格式显示)

bootstrap.yml (以后所有配置中都不会有application.yml文件了,都用bootstrap.yml代替。)
在这里插入图片描述

spring:
  application:
    name: userservice
  profiles:
    active: dev
#配置中心的位置ip和端口
#http://localhost:1111/userservice-dev.properties
  cloud:
    config:
      uri: http://localhost:1111

默认不会自动调用,要加入依赖

 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>

运行:localhost:1111/userservice/dev

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值