写点什么

Spring Cloud 1.0 – 云平台抽象化

  • 2014-06-30
  • 本文字数:1562 字

    阅读完需:约 5 分钟

Pivotal 最近发布了 Spring Cloud 1.0 开放源码库,为基于 JVM 的云应用开发提供了一种简单的开发方式。他可使应用程序连接到不同的云服务并且在运行时获取云环境的信息。无论是否基于 Spring,应用程序都可以使用 Spring Cloud。Spring Cloud 1.0 目前支持 Cloud Foundry 和 Heroku 两种云平台,也可以通过扩展来支持更多的云平台。

Spring Cloud 引入了云平台连接器(Cloud Connector)和服务连接器(Service Connector)的概念。云平台连接器是一个接口,需要由云平台提供者进行实现,以便库中的其他模块可以与该云平台协同工作。Spring Cloud 1.0 自带了 Cloud Foundry 和 Heroku 两个平台的云平台连接器。服务连接器是一个对象,它代表着与服务的连接。Spring Cloud 1.0 中包含了连接到 javax.sql.DataSource 和各 Spring Data 项目的服务连接器。开发者也可以定制开发云平台连接器和服务连接器以支持其他的云平台和服务,只需要将连接器打包成 JAR 文件并添加到 classpath 中即可。

在基于 Spring 的应用中使用 Spring Cloud,需要添加对 _spring-cloud-spring-service-connector_ 的依赖。Maven 依赖的代码片段如下:

复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-spring-service-connector</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

在不基于 Spring 的应用中使用 Spring Cloud 则需要添加对 _spring-cloud-core_ 的依赖。

复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-core</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

如果希望应用程序既能部署在 Cloud Foundry 也能部署在 Heroku 上,则需要同时添加如下两个依赖。

复制代码
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-cloudfoundry-connector</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-heroku-connector</artifactId>
<version>1.0.0.RELEASE</version>
</dependency>

下面是一个关于获取 DataSource 服务和 ApplicationInstanceInfo 的例子。ApplicationInstanceInfo 提供了这个实例中与云平台相关的信息。

复制代码
// MyController.java
@Controller
public class MyController {
@Autowired(required = false) DataSource dataSource;
@Autowired ApplicationInstanceInfo instanceInfo;
...
}
// CloudConfig.java
@Configuration
@ServiceScan
@Profile("cloud")
public class CloudConfig extends AbstractCloudConfig {
@Bean
public ApplicationInstanceInfo applicationInfo() {
return cloud().getApplicationInstanceInfo();
}
}

注解 _@Profile(“cloud”)表示我们希望这一配置只在云环境中加载。注解@ServiceScan_ 会扫描所有的服务并创建一个用于自动装配的 bean。@ServiceScan_ 类似于@ComponentScan_,不过与 _@ComponentScan_ 扫描组件和 bean 不同,_@ServiceScan_ 扫描绑定的服务。

关于如何将 Spring Cloud 部署到 Cloud Foundry 和 Heroku 的快速攻略,可以参考 Spring Cloud 简介的博客。这篇博文使用了一个 Spring Boot 的示例程序。对于不基于 Spring 的应用程序可以参考 Spring Cloud Core README 的使用说明。

查看原文链接: Spring Cloud 1.0 - Cloud Platform Abstraction


感谢马国耀对本文的审校。

给InfoQ 中文站投稿或者参与内容翻译工作,请邮件至 editors@cn.infoq.com 。也欢迎大家通过新浪微博( @InfoQ )或者腾讯微博( @InfoQ )关注我们,并与我们的编辑和其他读者朋友交流。

2014-06-30 23:505050
用户头像

发布了 75 篇内容, 共 65.2 次阅读, 收获喜欢 6 次。

关注

评论

发布
暂无评论
发现更多内容

Native开发工具之应用开发编辑器&打包发布(一),kotlin构造器

android 程序员 移动开发

offer求比较+部分大厂Android面经+真题解析(1),覆盖所有面试知识点

android 程序员 移动开发

Kotlin(五)深入理解Kotlin类与接口,androidndk开发视频

android 程序员 移动开发

万人逐鹿、十强争霸!华为云GaussDB数据库两大重量级赛事圆满落幕

华为云数据库小助手

GaussDB 大赛 华为云数据库

MaterialDesign系列文章(十一)Google2018年大会新出的控件汇总集合

android 程序员 移动开发

Meterial Design常见控件的使用(八),【面试总结】

android 程序员 移动开发

livedatabus详解,阿里是如何用他来做淘宝架构的?,android开发视频百度云

android 程序员 移动开发

Kotlin-编程核心基石—高阶函数,androidrom定制

android 程序员 移动开发

lambda表达式(3)-shawn,挑战大厂重燃激情

android 程序员 移动开发

LeakCanary源码分析以及ContentProvider的优化方案,android移动开发

android 程序员 移动开发

LeakCanary源码学习二:LeakCanary,sw实战营文件下载

android 程序员 移动开发

Meterial Design常见控件的使用(五),移动端h5开发框架

android 程序员 移动开发

Kotlin(1) lambda表达式和高阶函数操作符,android软件开发基础

android 程序员 移动开发

Kotlin下的5种单例模式,一招教你看懂Netty

android 程序员 移动开发

Kotlin系列三:空指针检查,万字长文总结Android多进程

android 程序员 移动开发

Go语言HTTPServer开发的六种实现

FunTester

HTTP Fasthttp Server Go 语言 FunTester

Meterial Design常见控件的使用(一),安卓面试题2018中高级

android 程序员 移动开发

MVVM系列之三:ViewModel,最新Android开发进阶

android 程序员 移动开发

最新消息!旺链科技加入庙行镇“乐创助企联盟”

旺链科技

区块链 产业区块链

Linux编程之权限系统与工具使用(二),【大牛系列教学】

android 程序员 移动开发

MVPArms官方快速组件化方案开源,来自5K star的信赖,安卓性能优化和内存优化

android 程序员 移动开发

Native开发工具之交叉编译移植(五),android开发基础教程视频

android 程序员 移动开发

Kotlin学习手记——注解(1),2021年字节跳动74道高级程序员面试

android 程序员 移动开发

Kotlin学习手记——注解,flutter下拉加载

android 程序员 移动开发

Kotlin的自定义View,实现带弧形的进度条,androidjetpack详解

android 程序员 移动开发

ObjectBox 集成指南,建议细读

android 程序员 移动开发

Kotlin学习手记--泛型、泛型约束、泛型型变,天呐

android 程序员 移动开发

Kotlin-新版来了,支持跨平台!,android热修复实现

android 程序员 移动开发

Kotlin-源码里成吨的-noinline-和-crossinline-是干嘛的?

android 程序员 移动开发

Kotlin写一个解释器(2)---语法分析,安卓项目开发范例大全

android 程序员 移动开发

Kotlin协程它不香吗?,kotlin开发游戏

android 程序员 移动开发

Spring Cloud 1.0 – 云平台抽象化_Java_Bienvenido David_InfoQ精选文章