SpringCloud学习之快速搭建分布式配置

简介: 一. 关于spring-cloud中的分布式配置   Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性 ,通常情况下我们可以把需要管理的配置文件放置在svn或者git上进行做统一的配置仓库。

一. 关于spring-cloud中的分布式配置

  Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性 ,通常情况下我们可以把需要管理的配置文件放置在svn或者git上进行做统一的配置仓库。

 

二 创建git远程仓库远程仓库中创建所需的配置文件

  本例子中是demo-local.yml , 配置文件内容如下:

  

student:
  name: test
  age: 28
View Code

 

三 创建config-server端

1) 创建gradle模块config-server并添加gradle依赖

dependencies {
    compile('org.springframework.cloud:spring-cloud-config-server')
}
View Code

2)编辑application.yml配置

server:
  port: 8000
spring:
  cloud:
    config:
      server:
        git:
          uri: git@gitlab.com:xxxx/config.git
      enabled: true
  profiles:
    active: local
View Code

 其中spring.cloud.config.server.git是配置远程仓库的地址,如果不使用SSH协议请配置username和password属性

3)编写启动类

package com.bdqn.lyrk.config;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;


@SpringBootApplication
@EnableConfigServer
public class ConfigServer {

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

注意在启动类上添加@EnableConfigServer注解

四 创建config-client端

 1)创建gradle模块demo-server并添加gradle依赖

dependencies{
    compile('org.springframework.cloud:spring-cloud-starter-config')
}
View Code

添加bootstrap.yml (Bootstrap.yml(bootstrap.properties)在application.yml(application.properties)之前加载)

server:
  port: 8001
spring:
  cloud:
    config:
      uri: http://localhost:8000
      profile: local
  application:
    name: demo
View Code

注意几点:

  配置服务从 /{name}/{profile}/{label} 提供属性源,客户端应用程序中的默认绑定

  “name”= ${spring.application.name}
  “profile”= ${spring.profiles.active} (实际上是 Environment.getActiveProfiles() )

  “label”=“master” 

  这里面的spring.application.name与远程仓库的配置文件名demo-local对应

  

编写DemoConfig类

 1 package com.bdqn.lyrk.server.demo.config;
 2 
 3 import org.springframework.boot.context.properties.ConfigurationProperties;
 4 import org.springframework.context.annotation.Configuration;
 5 
 6 @Configuration
 7 @ConfigurationProperties(prefix = "student")
 8 public class DemoConfig {
 9     private String name;
10     private int age;
11 
12     public String getName() {
13         return name;
14     }
15 
16     public void setName(String name) {
17         this.name = name;
18     }
19 
20     public int getAge() {
21         return age;
22     }
23 
24     public void setAge(int age) {
25         this.age = age;
26     }
27 }
View Code

注意 @ConfigurationProperties(prefix = "student")注解,该注解作用是:获取yml配置文件的前缀student,配置文件中余下部分用javabean的属性替换即可

编写启动类:

 1 package com.bdqn.lyrk.server.demo;
 2 
 3 import com.bdqn.lyrk.server.demo.config.DemoConfig;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6 import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
 7 import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator;
 8 import org.springframework.context.ApplicationContext;
 9 
10 @SpringBootApplication
11 public class DemoProvider {
12 
13     public static void main(String[] args) {
14         ApplicationContext applicationContext = SpringApplication.run(DemoProvider.class, args);
15         DemoConfig demoConfig = applicationContext.getBean(DemoConfig.class);
16         System.out.println(demoConfig.getName());
17     }
18 }
View Code

运行后得到上述结果,此时我们已经从远程配置中获取到所需的信息了

 

目录
相关文章
|
2天前
|
消息中间件 负载均衡 Java
【Spring Cloud 初探幽】
【Spring Cloud 初探幽】
8 1
|
4天前
|
Java 开发者 微服务
Spring Cloud原理详解
【5月更文挑战第4天】Spring Cloud是Spring生态系统中的微服务框架,包含配置管理、服务发现、断路器、API网关等工具,简化分布式系统开发。核心组件如Eureka(服务发现)、Config Server(配置中心)、Ribbon(负载均衡)、Hystrix(断路器)、Zuul(API网关)等。本文讨论了Spring Cloud的基本概念、核心组件、常见问题及解决策略,并提供代码示例,帮助开发者更好地理解和实践微服务架构。此外,还涵盖了服务通信方式、安全性、性能优化、自动化部署、服务网格和无服务器架构的融合等话题,揭示了微服务架构的未来趋势。
28 6
|
8天前
|
JSON Java Apache
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
Spring Cloud Feign 使用Apache的HTTP Client替换Feign原生httpclient
|
8天前
|
负载均衡 Java 开发者
Spring Cloud:一文读懂其原理与架构
Spring Cloud 是一套微服务解决方案,它整合了Netflix公司的多个开源框架,简化了分布式系统开发。Spring Cloud 提供了服务注册与发现、配置中心、消息总线、负载均衡、熔断机制等工具,让开发者可以快速地构建一些常见的微服务架构。
|
9天前
|
Java Nacos 微服务
全面学习SpringCloud框架指南
V 哥建议:如果你正在学习SpringCloud,你可以把这篇文章作为学习指南,如果你已经学过了,可以查漏补缺,因为 V 哥这篇文章全面介绍了SpringCloud的方方面面,当然你还需要结合官方文档的细节和源码部分去学习,成功拿下SpringCloud不是问题。加油兄弟!
|
10天前
|
消息中间件 Java RocketMQ
Spring Cloud RocketMQ:构建可靠消息驱动的微服务架构
【4月更文挑战第28天】消息队列在微服务架构中扮演着至关重要的角色,能够实现服务之间的解耦、异步通信以及数据分发。Spring Cloud RocketMQ作为Apache RocketMQ的Spring Cloud集成,为微服务架构提供了可靠的消息传输机制。
27 1
|
10天前
|
Dubbo Java 应用服务中间件
Spring Cloud Dubbo: 微服务通信的高效解决方案
【4月更文挑战第28天】在微服务架构的发展中,服务间的高效通信至关重要。Spring Cloud Dubbo 提供了一种基于 RPC 的通信方式,使得服务间的调用就像本地方法调用一样简单。本篇博客将探讨 Spring Cloud Dubbo 的核心概念,并通过具体实例展示其在项目中的实战应用。
14 2
|
10天前
|
监控 Java Sentinel
Spring Cloud Sentinel:概念与实战应用
【4月更文挑战第28天】在分布式微服务架构中,确保系统的稳定性和可靠性至关重要。Spring Cloud Sentinel 为微服务提供流量控制、熔断降级和系统负载保护,有效预防服务雪崩。本篇博客深入探讨 Spring Cloud Sentinel 的核心概念,并通过实际案例展示其在项目中的应用。
21 0
|
10天前
|
Cloud Native Java Nacos
Spring Cloud Nacos:概念与实战应用
【4月更文挑战第28天】Spring Cloud Nacos 是一个基于 Spring Cloud 构建的服务发现和配置管理工具,适用于微服务架构。Nacos 提供了动态服务发现、服务配置、服务元数据及流量管理等功能,帮助开发者构建云原生应用。
17 0