简介
Spring Cloud是一个基于Spring Boot实现的微服务架构开发工具。它为微服务架构中设计的配置管理、服务管理、断路器、智能路由、微代理、控制总线、全局锁、决策竞选、分布式会话和集群状态管理等操作提供了一种简单易用的开发方式,帮助开发人员构建弹性,可靠和协调的微服务系统。

Spring Cloud包含了多个子项目,并且在不断地增加,最新项目及介绍请参看Spring Cloud。
Component | Edgware.SR3 | Finchley.RC1 | Finchley.BUILD-SNAPSHOT |
---|---|---|---|
spring-cloud-aws | 1.2.2.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-bus | 1.3.2.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-cli | 1.4.1.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-commons | 1.3.3.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-contract | 1.2.4.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-config | 1.4.3.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-netflix | 1.4.4.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-security | 1.2.2.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-cloudfoundry | 1.1.1.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-consul | 1.3.3.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-sleuth | 1.3.3.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-stream | Ditmars.SR3 | Elmhurst.RELEASE | Elmhurst.BUILD-SNAPSHOT |
spring-cloud-zookeeper | 1.2.1.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-boot | 1.5.10.RELEASE | 2.0.1.RELEASE | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-task | 1.2.2.RELEASE | 2.0.0.RC1 | 2.0.0.RELEASE |
spring-cloud-vault | 1.1.0.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-gateway | 1.0.1.RELEASE | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
spring-cloud-openfeign | 2.0.0.RC1 | 2.0.0.BUILD-SNAPSHOT |
Spring Cloud是基于Spring Boot构建的,其版本也是有对应关系的,在构建项目时,注意版本之间的对应关系,版本对不上会有问题。
Spring Cloud | Spring Boot |
---|---|
Camden | 1.4.x |
Dalston | 1.5.x |
Edgware | 1.5.x |
Finchley | 2.0.x |
Brixton 和 Angel在2017年7月宣布停止更新支持,慎用。
在这里,我选用Edgware版,因为Finchley依赖2.0.x版本的Spring Boot,而2.0.x版本的Spring Boot依赖Spring Framework 5,这个版本相对以往的版本有很大的改动(当然都是改进、优化)。鉴于我以往项目都是基于Spring Boot 1.5.x版本的,所以选用了Edgware版,相对来说比较稳定,出了问题有较多的解决方案,更适合实际项目开发。
用IDEA构建空gradle项目


GroupId一般写公司名或者个人名,一般是以网址倒过来的形式。
ActifaceId一般写项目具体名称,例如‘cloud-demo’



导入spring cloud依赖
group 'com.zhuangqf.demo'
version '1.0-SNAPSHOT'
buildscript {
ext {
springBootVersion = '1.5.13.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
allprojects {
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
dependencyManagement {
imports {
mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Edgware.SR3'
}
}
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven { url "https://jitpack.io" }
}
dependencies {
// https://mvnrepository.com/artifact/ch.qos.logback/logback-classic
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.2.3'
// https://mvnrepository.com/artifact/ch.qos.logback/logback-core
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.2.3'
// https://mvnrepository.com/artifact/org.slf4j/slf4j-api
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
testCompile "junit:junit:4.12"
testCompile "org.springframework.boot:spring-boot-starter-test"
testCompile "org.easymock:easymock-parent:3.4"
testCompile "org.easymock:easymock:3.4"
}
}
增加子模块




写一个Hello World的微服务
我们新建了一个example的微服务,并为它导入了spring boot web的依赖包。接下来,我们将实现一个简单的接口/hello:
我们按Spring Boot的套路来,先写一个main方法作为应用程序的入口:
package com.zhuangqf.demo.cloud.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author zhuangqf
* @date 2018/5/17
*/
@SpringBootApplication
public class App {
public static void main(String[] arg) {
SpringApplication.run(App.class,arg);
}
}
接着写一个controller:
package com.zhuangqf.demo.cloud.example.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author zhuangqf
* @date 2018/5/17
*/
@RestController
@RequestMapping("/demo")
public class GreetController {
@RequestMapping("/hello")
public String greet(@RequestParam(value="name",required=false,defaultValue="my friend") String name) {
return "Hi " + name + ", Welcome!";
}
}
在resources目录下新增application.yml:
spring:
application:
name: example
server:
port: 8081
在这个文件中,我们设定应用名为example,服务的端口为8081

OK,我们的脚手架已经搭建完成了。没错,这就是一个Spring Boot应用程序。Spring Cloud中的微服务就是一个个Spring Boot应用程序。
以后,我将会一步步添加Spring Cloud中的子项目,逐步丰富我们的分布式系统。
参考
- Spring Cloud
- 《Spring Cloud微服务实战》
网友评论