This document discusses using Spring Cloud Netflix to connect microservices. It covers:
- An overview of Netflix and Spring Cloud
- How to use Spring Cloud Netflix libraries like Eureka, Ribbon and Hystrix to implement service discovery, load balancing and fault tolerance between microservices
- Demos of creating Eureka service registry and registering producer/consumer services
1 of 110
Downloaded 123 times
More Related Content
Spring Cloud Netflixを使おう #jsug
1. 1@Copyright 2016 Pivotal. All rights reserved. 1@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Netflixを使おう
Japan Spring User Group勉強会2016その1
Toshiaki Maki ● @making
Feb 1 2016
2. 2@Copyright 2016 Pivotal. All rights reserved.
Contents
• NetflixとSpring Cloudについて
• Spring Cloud Netflixについて
• PaaSにデプロイしよう
• Spring Cloud Services
9. 9@Copyright 2016 Pivotal. All rights reserved.
http://www.statista.com/statistics/250934/quarterly-number-of-netflix-streaming-subscribers-worldwide/
10. 10@Copyright 2016 Pivotal. All rights reserved.
http://www.slideshare.net/BruceWong3/the-case-for-chaos/12
Micro Services
Continuous
Delivery
DevOps
圧倒的スピードで成長するビジネスを支える
12. 12@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud
• 分散システムの共通パターンの実装を提供
Distributed Configuration
Service Registry
Circuit Breaker
Leadership Election
Distributed Messaging
Distributed Tracing
…
• Dr. Dave Syer, Spencer Gibbがメイン開発者
http://projects.spring.io/spring-cloud/
13. 13@Copyright 2016 Pivotal. All rights reserved.
Spring Cloudサブプロジェクト
• Spring Cloud Config
• Spring Cloud Netflix
• Spring Cloud Consul
• Spring Cloud Sleuth
• Spring Cloud Stream
• Spring Cloud Task
• Spring Cloud Data Flow
• …
14. 14@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Netflix
• Netflix OSSをSpring Bootと合わせて簡単に使う
ためのライブラリ
• Netflixでも使われている
https://youtu.be/6wvVANQ6EJ8
15. 15@Copyright 2016 Pivotal. All rights reserved.
Release Train
Spring Cloud (BOM) Angel.SR6 Brixton.M4 Brixton.BUILD-SNAPSHOT
Spring Cloud Netflix 1.0.7.RELEASE 1.1.0.M4 1.1.0.BUILD-SNAPSHOT
Spring Cloud Config 1.0.4.RELEASE 1.1.0.M4 1.1.0.BUILD-SNAPSHOT
Spring Cloud Commons 1.0.5.RELEASE 1.1.0.M4 1.1.0.BUILD-SNAPSHOT
Spring Cloud Consul - 1.0.0.M5 1.0.0.BUILD-SNAPSHOT
Spring Cloud Zookeeper - 1.0.0.M4 1.0.0.BUILD-SNAPSHOT
Spring Cloud Sleuth - 1.0.0.M4 1.0.0.BUILD-SNAPSHOT
Spring Cloud Stream - 1.0.0.M3 1.0.0.BUILD-SNAPSHOT
Spring Cloud Task - - 1.0.0.BUILD-SNAPSHOT
Spring Cloud Dataflow - 1.0.0.M2 1.0.0.BUILD-SNAPSHOT
Spring Boot 1.2.8.RELEASE 1.3.1.RELEASE 1.3.2.RELEASE
16. 16@Copyright 2016 Pivotal. All rights reserved.
Spring Cloudの使い方
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-parent</artifactId>
<version>Brixton.M4</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
17. 17@Copyright 2016 Pivotal. All rights reserved. 17@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Netflix
19. 19@Copyright 2016 Pivotal. All rights reserved.
Service Discoveryパターン
https://docs.pivotal.io/spring-cloud-services/service-registry/
Client(Consumer)が
API(Producer)へ直接
アクセスするのではな
く、Registryを経由す
る
20. 20@Copyright 2016 Pivotal. All rights reserved.
サービス名から物理アドレスを解決
Service Name IP:PORT
customer 192.168.11.1:8080
192.168.11.2:8080
…
order
192.168.11.11:8080
192.168.11.12:8080
…
catalog 192.168.11.21:8080
192.168.11.22:8080
…
21. 21@Copyright 2016 Pivotal. All rights reserved.
Eureka
• RESTベースのService Registryサーバー
• Netflixを支えるベース
http://techblog.netflix.com/2012/09/eureka.html
22. 22@Copyright 2016 Pivotal. All rights reserved.
Eureka Client
Service Consumer
Service Producer
Service Registry
Eureka Server
Eurekaを使う場合の基本構成
23. 23@Copyright 2016 Pivotal. All rights reserved.
Eureka Client
Service Consumer
Service Producer
Service Registry
Eureka Server
Service Info (Cache)
Heart Beat
Eurekaを使う場合の基本構成
24. 24@Copyright 2016 Pivotal. All rights reserved.
Eurekaを使う場合の基本構成
Eureka Client
Service Consumer
Service Producer
Service Registry
Eureka Server
Service Info (Cache)
キャシュを使うので、
Eureka Serverがダウンしても
Clientはサービス継続可能
25. 25@Copyright 2016 Pivotal. All rights reserved.
Eureka Serverの作成
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
27. 27@Copyright 2016 Pivotal. All rights reserved.
Eureka Serverの作成
@SpringBootApplication
@EnableEurekaServer
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
28. 28@Copyright 2016 Pivotal. All rights reserved.
Stand Aloneモード
# 慣習的ポート番号
server.port=8761
# Eurekaサーバーへの登録は不要
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
application.properties
50. 50@Copyright 2016 Pivotal. All rights reserved.
DiscoveryClientを使って書き直し
@Autowired DiscoveryClient client;
@RequestMapping(path = "/") String hello() {
List<ServiceInstance> instances = client
.getInstances("jsug-producer");
if (instances.isEmpty()) {
return "No ServiceRegistry!";
}
ServiceInstance instance = instances.get(0);
URI uri = instance.getUri();
String res = new RestTemplate()
.getForObject(uri, String.class);
return res + " from " + url;
}
51. 51@Copyright 2016 Pivotal. All rights reserved.
Ribbon
• HTTPクライアントと連携するクライアントサイドロ
ードバランサ
• Eurekaの情報を使ってロードバランス可能
http://techblog.netflix.com/2013/01/announcing-ribbon-tying-netflix-mid.html
52. 52@Copyright 2016 Pivotal. All rights reserved.
Service ProducerService Producer
Service Consumer
Service Producer
Service Registry
Load Balancer
Load Balancerあり
53. 53@Copyright 2016 Pivotal. All rights reserved.
@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsumerApplication {
@Autowired
RestTemplate restTemplate;
@RequestMapping(path = "/")
String hello() {
String res = restTemplate
.getForObject("http://jsug-producer", String.class);
return res + " from " + url;
}
public static void main(String[] args) {/*...*/}
}
自動でRibbonを使った
インターセプタが組み込まれる
73. 73@Copyright 2016 Pivotal. All rights reserved.
Dashboard
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
74. 74@Copyright 2016 Pivotal. All rights reserved.
Dashboardアプリケーション
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardApplication {
public static void main(String[] args) {/*...*/}
}
99. 99@Copyright 2016 Pivotal. All rights reserved.
Heroku上にデプロイ
See
https://blog.heroku.com/archives/2015/3/3/manag
ing_your_microservices_on_heroku_with_netflix_s_eure
ka
100. 100@Copyright 2016 Pivotal. All rights reserved. 100@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Services
by Pivotal Cloud Foundry
102. 102@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Services
http://docs.pivotal.io/spring-cloud-services/
103. 103@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Services
http://docs.pivotal.io/spring-cloud-services/
104. 104@Copyright 2016 Pivotal. All rights reserved.
Service Registryサービス
$ cf create-service p-service-registry standard jsug-eureka-service
自分でEureka Server
を作る必要なし
105. 105@Copyright 2016 Pivotal. All rights reserved.
<dependency>
<groupId>io.pivotal.spring.cloud</groupId>
<artifactId>spring-cloud-services-starter-service-registry</artifactId>
<groupId>1.0.2.RELEASE</groupId>
</dependency>
現時点でSpring CloudはAngel.SR4
にしか対応していないこの依存関係を追加すれば
Eureka Client側にEureka Serverの設定
が不要になる
107. 107@Copyright 2016 Pivotal. All rights reserved.
Spring Cloud Services
http://docs.pivotal.io/spring-cloud-services/
今日は説明できませんが、
3つを組み合わせることで
最大の効果を得られる
108. 108@Copyright 2016 Pivotal. All rights reserved.
概要はYoutubeで
• Config Server https://youtu.be/bJvReWt2jLc
• Service Registry https://youtu.be/tnVNiRn-TLw
• Circuit Breaker https://youtu.be/Vd243GqrkMI
109. 109@Copyright 2016 Pivotal. All rights reserved.
まとめ
• Netflixビジネスのスピードを支えるマイク
ロサービスはSpring Cloud Netflixで簡単
に構築可能
• PaaSに簡単にデプロイ可能
• Spring Cloud Servicesを使うことでより簡
単にシステムを構築可能