spring cloud简单入门

一、Spring Cloud简介

Spring Cloud为开发人员提供了在分布式系统中快速构建一些常见模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选择、分布式会话、集群状态)。分布式系统的协调导致了锅炉板模式,使用Spring Cloud开发人员可以快速地完成实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作,包括开发人员自己的笔记本电脑、裸机数据中心和托管平台(如Cloud Foundry)。

二、Spring Cloud特征

  • Distributed/versioned configuration:分布式配置中心(分布式配置中心还有:百度的disconf、淘宝的Diamond、携程的Apollo等)
  • Service registration and discovery:服务注册和发现
  • Routing:路由
  • Service-to-service calls:服务与服务之间的调用
  • Load balancing:负载均衡
  • Circuit Breakers:裂变器
  • Global locks:分布式锁
  • Leadership election and cluster state:Leadership选举和集群状态
  • Distributed messaging:分布式信息系统

三、简单例子(推荐使用Dalston SR4版本)

创建一个maven项目用来做服务端,也就是分布式配置中心。我们需要一个main方法来运行spring cloud,并且在main方法上添加@EnableConfigServer注释,
表示启用分布式配置中心,读取远程git仓库获取配置(这里我把配置文件放在git仓库中)。
 
 启动方法: 
package cn.et;

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

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

pom.xml配置:
<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">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.et</groupId>
  <artifactId>SpringCloudConfigService</artifactId>
  <version>0.0.1-SNAPSHOT</version>


<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.9.RELEASE</version>
</parent>
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>Dalston.SR4</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>
<dependencies>
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
  </dependency>
</dependencies>
  
</project>


  
  

然后要告诉存放配置文件的git仓库的位置,可以就写在spring boot的application.properties配置文件中:

application.properties配置文件:

spring.cloud.config.server.git.uri=https://github.com/1768622969/ConfigService
server.port=8089

之后就把配置文件放在上面的git仓库中。
所有spring的配置中心的配置文件,都有以下几种格式组成:
 1、配置文件是属于哪个应用的(application)
 2、配置文件是属于哪个阶段的(profile),一般就三个阶段(开发阶段,测试阶段,产品阶段)
 3、配置文件在在git仓库的哪个分支下(label)

比如,这是我在git仓库中存放的配置文件:

jdbc.properties:


最后再创建一个项目作为客户端,此客户端需要一个bootstrap.properties来抓取配置文件。

bootstrap.properties配置文件:

spring.cloud.config.uri=http://localhost:8089
spring.application.name=jdbc
spring.profiles.active=dev

运行时,做为服务端的项目main要一直运行挂在后台,启动客户端时才可以获取得到配置文件。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值