FOREWORD BY Andrew Glover

[Pages:266]IN ACTION

Craig Walls

FOREWORD BY Andrew Glover

it-

MANNING

Spring Boot in Action

CRAIG WALLS

MANNING

Shelter Island Licensed to ThomwawswS.nite-eabdo

For online information and ordering of this and other Manning books, please visit . 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@

?2016 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: Cynthia Kane Technical development editor: Robert Casazza

Copyeditor: Andy Carroll Proofreader: Corbin Collins Technical proofreader: John Guthrie

Typesetter: Gordan Salinovic Cover designer: Marija Tudor

ISBN 9781617292545 Printed in the United States of America

1 2 3 4 5 6 7 8 9 10 ? EBM ? 20 19 18 17 16 15

Licensed to ThomwawswS.nite-eabdo

contents

foreword vii preface ix about this book xii acknowledgments xv

1 Bootstarting Spring 1 1.1 Spring rebooted 2 Taking a fresh look at Spring 2 Examining Spring Boot essentials 4 What Spring Boot isn't 7 1.2 Getting started with Spring Boot 8 Installing the Spring Boot CLI 8 Initializing a Spring Boot project with Spring Initializr 12 1.3 Summary 22

2 Developing your first Spring Boot application 23 2.1 Putting Spring Boot to work 24 Examining a newly initialized Spring Boot project 26 Dissecting a Spring Boot project build 30 2.2 Using starter dependencies 33 Specifying facet-based dependencies 34 Overriding starter transitive dependencies 35

iii

Licensed to ThomwawswS.nite-eabdo

iv

CONTENTS

2.3 Using automatic configuration 37 Focusing on application functionality 37 Running the application 43 What just happened? 45

2.4 Summary 48

3 Customizing configuration 49 3.1 Overriding Spring Boot auto-configuration 50 Securing the application 50 Creating a custom security configuration 51 Taking another peek under the covers of auto-configuration 55

3.2 Externalizing configuration with properties 57 Fine-tuning auto-configuration 58 Externally configuring application beans 64 Configuring with profiles 69

3.3 Customizing application error pages 71 3.4 Summary 74

4 Testing with Spring Boot 76 4.1 Integration testing auto-configuration 77 4.2 Testing web applications 79 Mocking Spring MVC 80 Testing web security 83

4.3 Testing a running application 86 Starting the server on a random port 87 Testing HTML pages with Selenium 88

4.4 Summary 90

5 Getting Groovy with the Spring Boot CLI 92 5.1 Developing a Spring Boot CLI application 93 Setting up the CLI project 93 Eliminating code noise with Groovy 94 What just happened? 98

5.2 Grabbing dependencies 100 Overriding default dependency versions 101 Adding dependency repositories 102

5.3 Running tests with the CLI 102 5.4 Creating a deployable artifact 105 5.5 Summary 106

Licensed to ThomwawswS.nite-eabdo

CONTENTS

v

6 ApplyingGrails in Spring Boot 107 6.1 Using GORM for data persistence 108

6.2 Defining views with Groovy Server Pages 113

6.3 Mixing Spring Boot with Grails 3 115 Creating a new Grails project 116 Defining the domain 118 Writing a Grails controller 119 Creating the view 120

6.4 Summary 123

7 Taking a peek inside with the Actuator 124 7.1 Exploring the Actuator's endpoints 125 Viewing configuration details 126 Tapping runtime metrics 133 Shutting down the application 139 Fetching application information 140 7.2 Connecting to the Actuator remote shell 141 Viewing the autoconfig report 142 Listing application beans 143 Watching application metrics 144 Invoking Actuator endpoints 145

7.3 Monitoring your application with JMX 146

7.4 Customizing the Actuator 148 Changing endpoint IDs 148 Enabling and disabling endpoints 149 Adding custom metrics and gauges 149 Creating a custom trace repository 153 Plugging in custom health indicators 155

7.5 Securing Actuator endpoints 156

7.6 Summary 159

8 Deploying Spring Boot applications 160 8.1 Weighing deployment options 161

8.2 Deploying to an application server 162 Building a WAR file 162 Creating a production profile 164 Enabling database migration 168

8.3 Pushing to the cloud 173 Deploying to Cloud Foundry 173 Deploying to Heroku 177

8.4 Summary 180

appendix A appendix B appendix C appendix D

Spring Boot Developer Tools 181 Spring Boot starters 188 Configuration properties 195 Spring Boot dependencies 232

index 243

Licensed to ThomwawswS.nite-eabdo

Licensed to ThomwawswS.nite-eabdo

foreword

In the spring of 2014, the Delivery Engineering team at Netflix set out to achieve a lofty goal: enable end-to-end global continuous delivery via a software platform that facilitates both extensibility and resiliency. My team had previously built two different applications attempting to address Netflix's delivery and deployment needs, but both were beginning to show the telltale signs of monolith-ness and neither met the goals of flexibility and resiliency. What's more, the most stymieing effect of these monolithic applications was ultimately that we were unable to keep pace with our partner's innovation. Users had begun to move around our tools rather than with them.

It became apparent that if we wanted to provide real value to the company and rapidly innovate, we needed to break up the monoliths into small, independent services that could be released at will. Embracing a microservice architecture gave us hope that we could also address the twin goals of flexibility and resiliency. But we needed to do it on a credible foundation where we could count on real concurrency, legitimate monitoring, reliable and easy service discovery, and great runtime performance.

With the JVM as our bedrock, we looked for a framework that would give us rapid velocity and steadfast operationalization out of the box. We zeroed in on Spring Boot.

Spring Boot makes it effortless to create Spring-powered, production-ready services without a lot of code! Indeed, the fact that a simple Spring Boot Hello World application can fit into a tweet is a radical departure from what the same functionality required on the JVM only a few short years ago. Out-of-the-box nonfunctional features like security, metrics, health-checks, embedded servers, and externalized configuration made Boot an easy choice for us.

vii

Licensed to ThomwawswS.nite-eabdo

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download