Contenu connexe
Similaire à クラウド時代の Spring Framework (aka Spring Framework in Cloud Era) (20)
Plus de Tsuyoshi Miyake (19)
クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)
- 1. 1 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
クラウド時代のSpring Framework
Tsuyoshi Miyake, Pivotal Software, Inc.
@tsuyokb
- 2. 2 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
アジェンダ
§ Spring Framework 振り返り
§ Spring 2015
§ Cloud Foundry
• 歴史
• Java/Spring との関連
§ Spring Cloud
• 概要
• 各種サブプロジェクトの詳細
- 3. 3 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Framework 振り返り
- 4. 4 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
“Don't call us, we'll call you
- Hollywood principle
- 5. 5 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Framework とは、その昔何だったか?
§ Expert One-on-One J2EE Development without EJB – Chapter 7
“Introducing the Spring Framework” より
• Bean factory
• Application Context
• AOP framework
• Auto-proxing
• Transaction management
• DAO abstraction
• JDBC support
• Integration with O/R mapping tools
• Web MVC framework
• Remoting Support
- 6. 6 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring 2015
- 7. 7 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring IO Platform
- 8. 8 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Cloud Foundry とは?
- 9. 9 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring と Cloud Foundry のちょっとした歴史
“Cloud Foundry will be the natural
cloud deployment choice for Spring
developers.
Launching Cloud Foundry, The Industry’s First Open PaaS
Rod Johnson, Spring Engineering Blog, Apr 12 2011
- 10. 10 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring/Java 開発者にとっての Cloud Foundry の 3 つの意味
As App Devs As Dev Ops CF Core
• UAA/Login Server
with Spring Security
• Java Buildpack
• Service Broker with
Spring Boot/MVC
• Spring + Grails native
support
• Gradle, Maven plugin
• Eclipse CF plugin
• Spring Cloud
(Today’s Topic)
- 11. 11 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud
- 12. 12 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud とは?
§ 分散環境で Cloud Native なアプリケーションを作成するためのフレームワーク
• SpringOne 2GX 2014 で発表
§ 元々は PaaS 上で利用できるサービスをアプリから使いやすくするためのもの
• 現在は Spring Cloud Connectors と呼ばれるもの
§ Spring Data 同様、複数プロジェクトを束ねるアンブレラ・プロジェクト
• Spring Cloud Config
• Spring Cloud Bus
• Spring Cloud Netflix
• Spring Cloud Connectors
- 13. 13 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud Config (1/2)
§ 設定(Profile, Property)を外出しにして REST 経由でアクセスするしくみ
§ Config Server が設定を提供、Config Client が設定を利用
§ バックエンドは Git のレポジトリ(デフォルト)、バージョン管理ができる
Config ServerGit レポジトリ
アプリ A (Config Client)
1. Push Config 2. Source Config
param: xxx
アプリ B (Config Client) アプリ C (Config Client)
param: xxx param: xxx param: xxx
3. Pull Config
- 14. 14 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud Config (2/2)
§ /{application}/{profile}[/{label}] => /foo/dev で foo-dev.properties にアクセス
§ @EnableConfigServer アノテーションで便利に ConfigServer になる
§ 暗号化のビルトイン・サポート
@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
- 15. 15 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud Bus
§ 分散環境のノード間を Lightweight な Message Broker で結ぶ仕組み
§ 現在は AMQP のみのサポート
Config ServerGit レポジトリ
アプリ A (Config Client)
1. Update Config 2. Source Config
param: yyy
アプリ B (Config Client) アプリ C (Config Client)
param: yyy param: yyy param: yyy
4b. Pull Update
3. Post /bus/refresh
Config Bus
4a. メッセージ送信 5a. メッセージ受信
5b. Pull Update
- 16. 16 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud Netflix
§ Netflix OSS を Spring Boot と組み合わせて便利(Annotation)に使う仕組み
• https://github.com/Netflix/
• http://netflix.github.io/
§ Service Discovery (Eureka)
§ Client Side Load Balancing (Ribbon)
§ Circuit Breaker (Hystrix)
§ Intelligent Routing (Zuul)
- 17. 17 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Service Discovery (Eureka)
§ いわゆる Service Locator パターン
§ 自動で負荷分散 (round-robin)
§ @EnableDiscoveryClient で DiscoveryClient が Inject される
@SpringBootApplication
@EnableDiscoveryClient
public class Application {
@Autowired
DiscoveryClient discoveryClient;
public void consume() {
InstanceInfo instance =
discoveryClient.getNextServerFromEureka("hostname”, false);
}
}
Service
Registry
Producer
1. Register
Consumer
2. Discover
3. Connect
- 18. 18 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Client Side Load Balancing (Ribbon)
§ 複数の負荷分散ルールに対応(round-robin、平均レスポンス時間、ランダム)
§ 単体で Fault tolerance 機能(サーバーリストを保持)
§ DiscoveryClient の代わりに LoadBalancerClient が Inject される
§ またはより便利に Ribbon-enabled な RestTemplate も Inject 可能
@Autowired
LoadBalancerClient lbClient;
public void consume() {
ServiceInstance instance = lbClient.choose("hostname”);
}
@Autowired
RestTemplate restTemplate;
public void consume() {
ProducerResponse response = restTemplate.getForObject(”http://producer”, ..);
}
- 19. 19 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Circuit Breaker (Hystrix) (1/2)
§ “Release It!” Circuit breakers パターン
@EnableCircuitBreaker
public class Application {
@HystrixCommand(fallback = "doFallback")
public void doSomething() {}
public void doFallback() {}
}
Closed
call falis
(count failure)
call succeeds
(reset count)
Open
failure > threshold
(trip breaker)
timeout
(attempt reset)
Half-Open
call fails
(trip breaker)call succeeds
(reset)
- 20. 20 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Circuit Breaker (Hystrix) (2/2)
- 21. 21 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring Cloud Connectors
§ クラウド内のアプリケーション環境、およびサービスの抽象化
§ 多様なクラウドへの対応
• Cloud Foundry
• Heroku
• Local
§ 拡張性
• 新クラウド環境への対応
• 新サービスへの対応
• 新フレームワークへの対応
- 22. 22 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
“Spring Cloud Connectors
= DI Container for Cloud OS
- 詠み人知らず
- 23. 23 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring が昔、目指したもの
Application
Application Information
(Context)
Service
Service
Service
- 24. 24 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Spring が今、目指しているもの
Application
Application Information
(Context)
Service
Service
Service
- 25. 25 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Cloud Foundry: アプリケーションのデプロイ
① アプリケーショ
ンとメタデータ
の push
push app
Router
② サービスの作成とバインド
③ アプリケーションのステージング
④ アプリケーションのデプロイ
⑤ アプリケーションのヘルスチェック
Pivotal CF
Elastic Runtime
Blobstore DB
Cloud
Controller
Service Broker
Node(s)
DEA
DEA
DEA
DEA
+ app MD
+ =
Service
credentials
- 26. 26 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Cloud Foundry: サービスの作成とバインド
Router
Cloud Foundry
Runtime (ERS)
DB
Service
credentials
reserve resourcescreate service (HTTP) create service (HTTP)
bind service (HTTP)bind service (HTTP)
obtain connection
data
CLI Cloud
Controller
Service
Broker
Data
Service
- 27. 27 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Service Scan (@ServiceScan)
§ Java Config (@Configuration) と併用
§ アプリケーションにバインドされたサービスをスキャン
§ 各サービスについて Bean を作成
§ (Option) クラウド環境でのみ有効にするためには @Profile(“cloud”) を追加
@Configuration
@ServiceScan
// @Profile("cloud”)
public class CloudConfig {
}
- 28. 28 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Service Scan (@ServiceScan) カスタマイズ
§ パターン 1 : 同一タイプのサービス (e.g. DataSource) が 2 つ以上ある
§ パターン 2 : 各サービスの細かな設定が必要
§ パターン 3 : 独自サービスが必要
@Configuration
public class CloudConfig extends AbstractCloudConfig {
@Bean
public DataSource mysqlDataSource() {
return connectionFactory().dataSource("mysql-service");
}
@Bean
public DataSource postgresDataSource() {
PoolConfig poolConfig = new PoolConfig(20, 200);
ConnectionConfig connectionConfig = new
ConnectionConfig("characterEncoding=UTF-8");
DataSourceConfig serviceConfig = new DataSourceConfig(poolConfig,
connectionConfig);
return connectionFactory().dataSource("postgres-service”, serviceConfig);
}
}
- 29. 29 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
新しいクラウド環境への拡張 (Cloud Platform) (1/2)
§ CloudConnector interface を実装
• isInMatchingCloud(): 意図した Cloud 環境かどうかを判定
• getApplicationInstanceInfo(): アプリケーションの情報(ID, ホスト名など)を返す
• getServiceInfos(): アプリケーションが利用可能なサービスの接続情報を返す
§ Cloud Platform の登録
• META-INF/services/org.springframework.cloud.CloudConnector に実装クラス
• java.util.ServiceLoader により load() される
public interface CloudConnector {
boolean isInMatchingCloud();
ApplicationInstanceInfo getApplicationInstanceInfo();
List<ServiceInfo> getServiceInfos();
}
- 30. 30 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
新しいクラウド環境への拡張 (Cloud Platform) (2/2)
§ CloudFoundry での実装例 (CloudFoundryConnector)
public class CloudFoundryConnector extends
AbstractCloudConnector<Map<String,Object>> {
public boolean isInMatchingCloud() {
return environment.getEnvValue("VCAP_APPLICATION") != null;
}
public ApplicationInstanceInfo getApplicationInstanceInfo() {
Map<String, Object> rawApplicationInstanceInfo =
objectMapper.readValue(
environment.getEnvValue("VCAP_APPLICATION"), Map.class);
}
protected List<Map<String,Object>> getServicesData() {
String servicesString = environment.getEnvValue("VCAP_SERVICES");
}
}
- 31. 31 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a
Creative Commons Attribution-NonCommercial license: http://creativecommons.org/licenses/by-nc/3.0/
Learn More. Stay Connected.
§ Spring Cloud Project: http://projects.spring.io/spring-cloud/
§ Spring Cloud Source: https://github.com/spring-cloud
§ Spring Cloud Sample: https://github.com/spring-cloud-samples
§ Netflix OSS: http://netflix.github.io/
§ Spring Cloud Connectors Blogs
• https://spring.io/blog/2014/06/03/introducing-spring-cloud
• https://spring.io/blog/2014/07/29/using-spring-cloud-programmatically
• https://spring.io/blog/2014/08/05/extending-spring-cloud