logo资料库

Akka-in-Action.pdf

第1页 / 共450页
第2页 / 共450页
第3页 / 共450页
第4页 / 共450页
第5页 / 共450页
第6页 / 共450页
第7页 / 共450页
第8页 / 共450页
资料共450页,剩余部分请下载后查看
Akka in Action
brief contents
contents
preface
acknowledgments
about this book
Intended audience
Roadmap
Code conventions and downloads
Software requirements
Author Online
About the authors
About the cover illustration
1 Introducing Akka
1.1 What is Akka?
1.2 Actors: a quick overview
1.3 Two approaches to scaling: setting up our example
1.4 Traditional scaling
1.4.1 Traditional scaling and durability: move everything to the database
1.4.2 Traditional scaling and interactive use: polling
1.4.3 Traditional scaling and interactive use: polling
1.5 Scaling with Akka
1.5.1 Scaling with Akka and durability: sending and receiving messages
1.5.2 Scaling with Akka and interactive use: push messages
1.5.3 Scaling with Akka and failure: asynchronous decoupling
1.5.4 The Akka approach: sending and receiving messages
1.6 Actors: one programming model to rule up and out
1.6.1 An asynchronous model
1.6.2 Actor operations
1.7 Akka actors
1.7.1 ActorSystem
1.7.2 ActorRef, mailbox, and actor
1.7.3 Dispatchers
1.7.4 Actors and the network
1.8 Summary
2 Up and running
2.1 Clone, build, and test interface
2.1.1 Build with sbt
2.1.2 Fast-forward to the GoTicks.com REST server
2.2 Explore the actors in the app
2.2.1 Structure of the app
2.2.2 The actor that handles the sale: TicketSeller
2.2.3 The BoxOffice actor
2.2.4 RestApi
2.3 Into the cloud
2.3.1 Create the app on Heroku
2.3.2 Deploy and run on Heroku
2.4 Summary
3 Test-driven development with actors
3.1 Testing actors
3.2 One-way messages
3.2.1 SilentActor examples
3.2.2 SendingActor example
3.2.3 SideEffectingActor example
3.3 Two-way messages
3.4 Summary
4 Fault tolerance
4.1 What fault tolerance is (and what it isn’t)
4.1.1 Plain old objects and exceptions
4.1.2 Let it crash
4.2 Actor lifecycle
4.2.1 Start event
4.2.2 Stop event
4.2.3 Restart event
4.2.4 Putting the lifecycle pieces together
4.2.5 Monitoring the lifecycle
4.3 Supervision
4.3.1 Supervisor hierarchy
4.3.2 Predefined strategies
4.3.3 Custom strategies
4.4 Summary
5 Futures
5.1 Use cases for futures
5.2 In the future nobody blocks
5.2.1 Promises are promises
5.3 Futuristic errors
5.4 Combining futures
5.5 Combining futures with actors
5.6 Summary
6 Your first distributed Akka app
6.1 Scaling out
6.1.1 Common network terminology
6.1.2 Reasons for a distributed programming model
6.2 Scaling out with remoting
6.2.1 Making the GoTicks.com app distributed
6.2.2 Remote REPL action
6.2.3 Remote lookup
6.2.4 Remote deployment
6.2.5 Multi-JVM testing
6.3 Summary
7 Configuration, logging, and deployment
7.1 Configuration
7.1.1 Trying out Akka configuration
7.1.2 Using defaults
7.1.3 Akka configuration
7.1.4 Multiple systems
7.2 Logging
7.2.1 Logging in an Akka application
7.2.2 Using logging
7.2.3 Controlling Akka’s logging
7.3 Deploying actor-based applications
7.4 Summary
8 Structural patterns for actors
8.1 Pipes and filters
8.1.1 Enterprise integration pattern: pipes and filters
8.1.2 Pipes and filters in Akka
8.2 Enterprise integration pattern: scatter-gather
8.2.1 Applicability
8.2.2 Parallel tasks with Akka
8.2.3 Implementing the scatter component using the recipient list pattern
8.2.4 Implementing the gather component with the aggregator pattern
8.2.5 Combining the components into the scatter-gather pattern
8.3 Enterprise integration pattern: routing slip
8.4 Summary
9 Routing messages
9.1 The enterprise integration router pattern
9.2 Balance load using Akka routers
9.2.1 Akka pool router
9.2.2 Akka group router
9.2.3 ConsistentHashing router
9.3 Implementing the router pattern using actors
9.3.1 Content-based routing
9.3.2 State-based routing
9.3.3 Router implementations
9.4 Summary
10 Message channels
10.1 Channel types
10.1.1 Point-to-point
10.1.2 Publish-subscribe
10.2 Specialized channels
10.2.1 Dead letter
10.2.2 Guaranteed delivery
10.3 Summary
11 Finite-state machines and agents
11.1 Using a finite-state machine
11.1.1 Quick introduction to finite-state machines
11.1.2 Creating an FSM model
11.2 Implementation of an FSM model
11.2.1 Implementing transitions
11.2.2 Implementing the entry actions
11.2.3 Timers within FSM
11.2.4 Termination of FSM
11.3 Implement shared state using agents
11.3.1 Simple shared state with agents
11.3.2 Waiting for the state update
11.4 Summary
12 System integration
12.1 Message endpoints
12.1.1 Normalizer
12.1.2 Canonical data model
12.2 Implementing endpoints using Apache Camel
12.2.1 Implement a consumer endpoint receiving messages from an external system
12.2.2 Implement a producer endpoint sending messages to an external system
12.3 Implementing an HTTP interface
12.3.1 The HTTP example
12.3.2 Implementing a REST endpoint with akka-http
12.4 Summary
13 Streaming
13.1 Basic stream processing
13.1.1 Copying files with sources and sinks
13.1.2 Materializing runnable graphs
13.1.3 Processing events with flows
13.1.4 Handling errors in streams
13.1.5 Creating a protocol with a BidiFlow
13.2 Streaming HTTP
13.2.1 Receiving a stream over HTTP
13.2.2 Responding with a stream over HTTP
13.2.3 Custom marshallers and unmarshallers for content type and negotiation
13.3 Fan in and fan out with the graph DSL
13.3.1 Broadcasting to flows
13.3.2 Merging flows
13.4 Mediating between producers and consumers
13.4.1 Using buffers
13.5 Rate-detaching parts of a graph
13.5.1 Slow consumer, rolling up events into summaries
13.5.2 Fast consumer, expanding metrics
13.6 Summary
14 Clustering
14.1 Why use clustering?
14.2 Cluster membership
14.2.1 Joining the cluster
14.2.2 Leaving the cluster
14.3 Clustered job processing
14.3.1 Starting the cluster
14.3.2 Work distribution using routers
14.3.3 Resilient jobs
14.3.4 Testing the cluster
14.4 Summary
15 Actor persistence
15.1 Recovering state with event sourcing
15.1.1 Updating records in place
15.1.2 Persisting state without updates
15.1.3 Event sourcing for actors
15.2 Persistent actors
15.2.1 Persistent actor
15.2.2 Testing
15.2.3 Snapshots
15.2.4 Persistence query
15.2.5 Serialization
15.3 Clustered persistence
15.3.1 Cluster singleton
15.3.2 Cluster sharding
15.4 Summary
16 Performance tips
16.1 Performance analysis
16.1.1 System performance
16.1.2 Performance parameters
16.2 Performance measurement of actors
16.2.1 Collect mailbox data
16.2.2 Collecting processing data
16.3 Improving performance by addressing bottlenecks
16.4 Configure dispatcher
16.4.1 Recognizing thread pool problems
16.4.2 Using multiple instances of dispatchers
16.4.3 Changing thread pool size statically
16.4.4 Using a dynamic thread pool size
16.5 Changing thread releasing
16.5.1 Limitations on thread release settings
16.6 Summary
17 Looking ahead
17.1 akka-typed module
17.2 Akka Distributed Data
17.3 Summary
index
Symbols
Numerics
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Raymond Roestenburg Rob Bakker Rob Williams M A N N I N G
Akka in Action RAYMOND ROESTENBURG ROB BAKKER ROB WILLIAMS M A N N I N G SHELTER ISLAND
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2017 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Development editor: Jeff Bleiel Review editor: Olivia Booth Copyeditors: Benjamin Berg, Andy Carroll Technical proofreader: Doug Warren Project editor: Kevin Sullivan Proofreader: Katie Tennant Typesetter: Dottie Marsico Cover designer: Marija Tudor ISBN 9781617291012 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 21 20 19 18 17 16
brief contents Introducing Akka 1 1 ■ 2 ■ Up and running 28 3 ■ Test-driven development with actors 49 4 ■ Fault tolerance 66 5 ■ Futures 92 6 ■ Your first distributed Akka app 118 7 ■ Configuration, logging, and deployment 147 8 ■ Structural patterns for actors 166 9 ■ Routing messages 188 10 ■ Message channels 213 11 ■ Finite-state machines and agents 233 12 ■ System integration 254 13 ■ Streaming 281 14 ■ Clustering 322 15 ■ Actor persistence 354 16 ■ Performance tips 388 17 ■ Looking ahead 416 iii
contents xi preface acknowledgments about this book xii xiii 1 Introducing Akka 1 1.1 What is Akka? 4 1.2 Actors: a quick overview 4 1.3 Two approaches to scaling: setting up our example 6 1.4 Traditional scaling 7 Traditional scaling and durability: move everything to the database 7 ■ Traditional scaling and interactive use: polling 10 ■ Traditional scaling and interactive use: polling 12 1.5 Scaling with Akka 13 Scaling with Akka and durability: sending and receiving messages 13 ■ Scaling with Akka and interactive use: push messages 16 ■ Scaling with Akka and failure: asynchronous decoupling 16 ■ The Akka approach: sending and receiving messages 17 1.6 Actors: one programming model to rule up and out 18 An asynchronous model 19 ■ Actor operations 20 1.7 Akka actors 23 ActorSystem 23 ■ ActorRef, mailbox, and actor 24 ■ Dispatchers 25 ■ Actors and the network 26 1.8 Summary 27 v
vi 2 Up and running 2.1 Clone, build, and test interface 29 CONTENTS 28 Build with sbt 30 ■ Fast-forward to the GoTicks.com REST server 31 2.2 Explore the actors in the app 34 Structure of the app 35 ■ The actor that handles the sale: TicketSeller 40 ■ The BoxOffice actor 41 ■ RestApi 43 2.3 Into the cloud 45 Create the app on Heroku 45 ■ Deploy and run on Heroku 46 2.4 Summary 48 3 Test-driven development with actors 3.1 Testing actors 50 3.2 One-way messages 52 49 SilentActor examples 53 ■ SendingActor example 56 SideEffectingActor example 61 3.3 Two-way messages 63 3.4 Summary 64 4 Fault tolerance 66 4.1 What fault tolerance is (and what it isn’t) 66 Plain old objects and exceptions 68 ■ Let it crash 73 4.2 Actor lifecycle 76 Start event 77 ■ Stop event 77 ■ Restart event 78 Putting the lifecycle pieces together 80 ■ Monitoring the lifecycle 81 4.3 Supervision 82 Supervisor hierarchy 83 ■ Predefined strategies 85 Custom strategies 86 4.4 Summary 91 5 Futures 92 5.1 Use cases for futures 93 5.2 In the future nobody blocks 97 Promises are promises 101 5.3 Futuristic errors 104
CONTENTS vii 5.4 Combining futures 108 5.5 Combining futures with actors 115 5.6 Summary 116 6 Your first distributed Akka app 118 Scaling out 119 6.1 Common network terminology 119 ■ Reasons for a distributed programming model 121 6.2 Scaling out with remoting 122 Making the GoTicks.com app distributed 123 ■ Remote REPL action 123 ■ Remote lookup 128 ■ Remote deployment 135 Multi-JVM testing 139 6.3 Summary 145 7 Configuration, logging, and deployment 147 7.1 Configuration 147 Trying out Akka configuration 148 ■ Using defaults 151 Akka configuration 153 ■ Multiple systems 154 7.2 Logging 157 Logging in an Akka application 157 ■ Using logging 159 Controlling Akka’s logging 160 7.3 Deploying actor-based applications 161 7.4 Summary 165 8 Structural patterns for actors 166 8.1 Pipes and filters 167 Enterprise integration pattern: pipes and filters 167 Pipes and filters in Akka 168 8.2 Enterprise integration pattern: scatter-gather 171 Applicability 171 ■ Parallel tasks with Akka 173 Implementing the scatter component using the recipient list pattern 174 ■ Implementing the gather component with the aggregator pattern 175 ■ Combining the components into the scatter-gather pattern 180 8.3 Enterprise integration pattern: routing slip 182 8.4 Summary 187
分享到:
收藏