31
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

クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

Embed Size (px)

Citation preview

Page 1: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

1 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under a

Creative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

クラウド時代のSpring Framework

Tsuyoshi Miyake, Pivotal Software, Inc.@tsuyokb

Page 2: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

2 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

アジェンダ

§ Spring Framework 振り返り

§ Spring 2015

§ Cloud Foundry• 歴史• Java/Spring との関連

§ Spring Cloud• 概要• 各種サブプロジェクトの詳細

Page 3: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

3 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring Framework 振り返り

Page 4: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

4 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

“Don't call us, we'll call you- Hollywood principle

Page 5: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

5 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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

Page 6: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

6 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring 2015

Page 7: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

7 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring IO Platform

Page 8: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

8 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Cloud Foundry とは?

Page 9: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

9 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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

Page 10: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

10 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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)

Page 11: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

11 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring Cloud

Page 12: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

12 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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

Page 13: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

13 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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: xxx3. Pull Config

Page 14: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

14 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring Cloud Config (2/2)

§ /{application}/{profile}[/{label}] => /foo/dev で foo-dev.propertiesにアクセス

§ @EnableConfigServer アノテーションで便利に ConfigServerになる

§ 暗号化のビルトイン・サポート

@Configuration@EnableAutoConfiguration@EnableConfigServerpublic class ConfigServerApplication {

public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args);

}}

Page 15: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

15 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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 Update3. Post /bus/refresh

Config Bus

4a. メッセージ送信 5a. メッセージ受信

5b. Pull Update

Page 16: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

16 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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)

Page 17: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

17 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Service Discovery (Eureka)

§ いわゆる Service Locator パターン

§ 自動で負荷分散 (round-robin)

§ @EnableDiscoveryClient で DiscoveryClientが Inject される

@SpringBootApplication@EnableDiscoveryClientpublic class Application {

@AutowiredDiscoveryClient discoveryClient;

public void consume() {InstanceInfo instance =

discoveryClient.getNextServerFromEureka("hostname”, false);}

}

ServiceRegistry

Producer

1. Register

Consumer

2. Discover

3. Connect

Page 18: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

18 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Client Side Load Balancing (Ribbon)

§ 複数の負荷分散ルールに対応(round-robin、平均レスポンス時間、ランダム)

§ 単体で Fault tolerance 機能(サーバーリストを保持)

§ DiscoveryClientの代わりに LoadBalancerClientが Inject される

§ またはより便利に Ribbon-enabled な RestTemplate も Inject 可能

@AutowiredLoadBalancerClient lbClient;

public void consume() {ServiceInstance instance = lbClient.choose("hostname”);

}

@AutowiredRestTemplate restTemplate;

public void consume() {ProducerResponse response = restTemplate.getForObject(”http://producer”, ..);

}

Page 19: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

19 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Circuit Breaker (Hystrix) (1/2)

§ “Release It!” Circuit breakers パターン

@EnableCircuitBreakerpublic 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)

Page 20: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

20 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Circuit Breaker (Hystrix) (2/2)

Page 21: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

21 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring Cloud Connectors

§ クラウド内のアプリケーション環境、およびサービスの抽象化

§ 多様なクラウドへの対応

• Cloud Foundry• Heroku• Local

§ 拡張性

• 新クラウド環境への対応• 新サービスへの対応• 新フレームワークへの対応

Page 22: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

22 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

“Spring Cloud Connectors= DI Container for Cloud OS

-詠み人知らず

Page 23: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

23 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring が昔、目指したもの

Application

Application Information(Context)

ServiceServiceService

Page 24: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

24 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Spring が今、目指しているもの

Application

Application Information(Context)

ServiceServiceService

Page 25: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

25 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Cloud Foundry: アプリケーションのデプロイ

① アプリケーションとメタデータの push

push app

Router② サービスの作成とバインド

③ アプリケーションのステージング

④ アプリケーションのデプロイ

⑤ アプリケーションのヘルスチェックPivotalCFElasticRuntime

Blobstore DB

Cloud Controller

Service Broker Node(s)

DEADEA

DEADEA

+ app MD

+ =

Servicecredentials

Page 26: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

26 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Cloud Foundry:サービスの作成とバインド

Router

CloudFoundryRuntime(ERS)

DBServicecredentials

reserve resourcescreate service (HTTP) create service (HTTP)

bind service (HTTP)bind service (HTTP) obtain connection data

CLI Cloud Controller

ServiceBroker

DataService

Page 27: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

27 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Service Scan (@ServiceScan)

§ Java Config (@Configuration) と併用

§ アプリケーションにバインドされたサービスをスキャン

§ 各サービスについて Bean を作成

§ (Option) クラウド環境でのみ有効にするためには@Profile(“cloud”) を追加

@Configuration@ServiceScan// @Profile("cloud”)public class CloudConfig {}

Page 28: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

28 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.org/ licenses/by-nc/3.0/

Service Scan (@ServiceScan) カスタマイズ

§ パターン 1 : 同一タイプのサービス (e.g. DataSource) が 2 つ以上ある

§ パターン 2 : 各サービスの細かな設定が必要

§ パターン 3 : 独自サービスが必要

@Configurationpublic class CloudConfig extends AbstractCloudConfig {

@Beanpublic DataSource mysqlDataSource() {

return connectionFactory().dataSource("mysql-service");}@Beanpublic 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);

}}

Page 29: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

29 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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();

}

Page 30: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

30 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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");

}}

Page 31: クラウド時代の Spring Framework (aka Spring Framework in Cloud Era)

31 Unless otherwise indicated, these slides are © 2015 Pivotal Software, Inc. and licensed under aCreative Commons Attr ibut ion-NonCommercial l icense: http:/ /creat ivecommons.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