Python Microservices Development_ Build efficient and lightweight microservices using the Python tooling e.pdf
《Python Microservices Development_ Build efficient and lightweight microservices using the Python tooling e.pdf》由会员分享,可在线阅读,更多相关《Python Microservices Development_ Build efficient and lightweight microservices using the Python tooling e.pdf(311页珍藏版)》请在冰点文库上搜索。
PythonMicroservicesDevelopmentSecondEditionBuildefficientandlightweightmicroservicesusingthePythontoolingecosystemSimonFraserTarekZiadBIRMINGHAMMUMBAIPythonMicroservicesDevelopmentSecondEditionCopyright2021PacktPublishingAllrightsreserved.Nopartofthisbookmaybereproduced,storedinaretrievalsystem,ortransmittedinanyformorbyanymeans,withoutthepriorwrittenpermissionofthepublisher,exceptinthecaseofbriefquotationsembeddedincriticalarticlesorreviews.Everyefforthasbeenmadeinthepreparationofthisbooktoensuretheaccuracyoftheinformationpresented.However,theinformationcontainedinthisbookissoldwithoutwarranty,eitherexpressorimplied.Neithertheauthor(s),norPacktPublishingoritsdealersanddistributors,willbeheldliableforanydamagescausedorallegedtohavebeencauseddirectlyorindirectlybythisbook.PacktPublishinghasendeavoredtoprovidetrademarkinformationaboutallofthecompaniesandproductsmentionedinthisbookbytheappropriateuseofcapitals.However,PacktPublishingcannotguaranteetheaccuracyofthisinformation.Producer:
ShaileshJainAcquisitionEditorPeerReviews:
SabyDSilvaProjectEditor:
RiannaRodriguesContentDevelopmentEditor:
AlexPattersonCopyEditor:
SafisEditingTechnicalEditor:
KaranSonawaneProofreader:
SafisEditingIndexer:
RekhaNairPresentationDesigner:
PranitPadwalFirstpublished:
July2017Secondedition:
September2021Productionreference:
1280921PublishedbyPacktPublishingLtd.LiveryPlace35LiveryStreetBirminghamB32PB,UK.ISBN978-1-80107-630-ContributorsAbouttheauthorsSimonFraserisaSiteReliabilityEngineerforCiscoMeraki.Hehasovertwentyyearsofexperienceincomputing,bothdevelopingandrunningsystems.HehasworkedasaSystemsAdministratorforaninternetserviceprovider;aninfrastructureandhigh-performancecomputingengineerattheWelcomeSangerInstitute,andasaFirefoxReleaseEngineeratMozilla.HestudiedCyberneticsandComputerScienceattheUniversityofReadingandhasalsotaughtacademiccoursesinprogrammingforscientists.IwouldliketothankEmmaAptedandHelenCookfortheirfrequentsupportandencouragement,rangingfromproofreadingtomimingabookopeningwhenImclearlynotworking.MyfatherGeorgeFraserpassedonhisimmeasurableskillatfinishingaprojectjustbeforethedeadline,andmymotherElizabethFrasershowedmehowtoexplainconceptsandinformpeoplethroughyearsofteaching.ThisbookwouldalsonotbepossiblewithoutTarekZiadsworkonthe1steditionandthesolidbaseonwhichtobuild,aswellastheworkofDevelopmentEditorAlexPatterson,ReviewerWilliamKahn-Greene,andProjectEditorRiannaRodrigues.Wemightstandontheshouldersofgiants,butwealsobuildonahugestructuremadebyeveryoneofus,soIwouldalsoliketothankeveryoneIveworkedwithovertheyearsinallmydifferentteams.Yes,evenyou.TarekZiadisaSoftwareEngineer,locatedinBurgundy,France.HeworksatElastic,buildingtoolsfordevelopers.BeforeElastic,heworkedatMozillafor10years,andhefoundedaFrenchPythonusergroup,calledAFPy.TarekhasalsowrittenseveralarticlesaboutPythonforvariousmagazines,andafewbooksinFrenchandEnglish.IwouldliketothankFreya,Suki,Milo,andAminaforbeingsosupportiveofallmybookprojects.AboutthereviewerWillKahn-GreenehasbeenbuildingthingsinPythonandotherlanguagessincethelate90s.HemanagesthecrashingestionpipelineatMozillaandmaintainsseveralPythonlibraries.WhenheswaitingforCItoruntests,hebuildsthingswithwood,tendshistomatoplants,andcooksforfour.vTableofContentsPrefacexiChapter1:
UnderstandingMicroservices1Theoriginsofservice-orientedarchitecture2Themonolithicapproach3Themicroserviceapproach8Microservicebenefits10Separationofconcerns10Smallerprojects10Scalinganddeployment11Pitfallsofmicroservices12Illogicalsplitting12Morenetworkinteractions13Datastoringandsharing13Compatibilityissues14Testing14ImplementingmicroserviceswithPython15Howwebserviceswork16TheWSGIstandard16Workers,threads,andsynchronicity17Aworkerpoolapproach17Beingasynchronous18Twisted,Tornado,Greenlets,andGevent19AsynchronousPython19Languageperformance21Summary24TableofContentsviChapter2:
DiscoveringQuart25MakingsurewehavePython27HowQuarthandlesrequests28Routing32Variablesandconverters33Theurl_forfunction37Request37Response38Quartsbuilt-infeatures40Thesessionobject40Globals41Signals42Extensionsandmiddleware44Templates46Configuration48Blueprints50Errorhandling52Customerrorhandler52Amicroserviceskeleton54Summary57Chapter3:
Coding,Testing,andDocumentation:
theVirtuousCycle59Differentkindsoftests62Unittests63Functionaltests67Integrationtests70Loadtests71End-to-endtests75Usingpytestandtox76Developerdocumentation80Toolsfordocumentation81Versioncontrol85ContinuousIntegrationandContinuousDeployment86GitHubActions87Documentation88Coveralls89Badges89Summary90TableofContentsviiChapter4:
DesigningJeeves91TheJeevesbot92Userstories92Monolithicdesign93Model94Viewandtemplate97Ahuman-readableview100Slackworkspaces103Takingactions107OAuthtokens108Authenticationandauthorization109Backgroundtasks113Puttingtogetherthemonolithicdesign116Summary118Chapter5:
SplittingtheMonolith119Identifyingpotentialmicroservices119Codecomplexityandmaintenance121MetricsandMonitoring122Logging128SplittingaMonolith131FeatureFlags135RefactoringJeeves137Workflow139Summary140Chapter6:
InteractingwithOtherServices141Callingotherwebresources142Findingoutwheretogo145Environmentvariables145Servicediscovery145Transferringdata146HTTPcacheheaders147GZIPcompression149ProtocolBuffers152MessagePack153Puttingittogether155Asynchronousmessages155Messagequeuereliability156Basicqueues156TableofContentsviiiTopicexchangesandqueues157Publish/subscribe161Puttingittogether162Testing162UsingOpenAPI164Summary165Chapter7:
SecuringYourServices167TheOAuth2protocol168X.509certificate-basedauthentication170Token-basedauthentication173TheJWTstandard174PyJWT176UsingacertificatewithJWT177TheTokenDealermicroservice179TheOAuthimplementation180UsingTokenDealer184Securingyourcode186Limitingyourapplicationscope187Untrustedincomingdata189Redirectingandtrustingqueries192Sanitizinginputdata193UsingBanditlinter194Dependencies195Webapplicationfirewall195OpenResty:
Luaandnginx196Rateandconcurrencylimiting199OtherOpenRestyfeatures201Summary202Chapter8:
MakingaDashboard203BuildingaReactJSdashboard205TheJSXsyntax205Reactcomponents206Pre-processingJSX211ReactJSandQuart212Cross-originresourcesharing213Authenticationandauthorization215AnoteaboutMicroFrontends216GettingtheSlacktoken218JavaScriptauthentication220Summary221TableofContentsixChapter9:
PackagingandRunningPython223Thepackagingtoolchain224Afewdefinitions225Packaging226Thesetup.pyfile226Therequirements.txtfile231TheMANIFEST.infile234Versioning235Releasing238Distributing239Runningallmicroservices242Processmanagement243Summary245Chapter10:
DeployingonAWS247WhatisDocker?
248IntroductiontoDocker251RunningQuartinDocker255Docker-baseddeployments257Terraform259Servicediscovery261DockerCompose263DockerSwarm265Kubernetes267Summary272Chapter11:
WhatsNext?
275Automation275Scaling276ContentDeliveryNetworks277Multi-clouddeployments277LambdaFunctions277Expandingmonitoring278Makingpromises278Summary280OtherBooksYouMayEnjoy283Index287xiPrefacePythonMicroservicesDevelopmentintroducesthedesignandcreationofamicroservice-basedapplicationusingthepopularPythonprogramminglanguage,andtheQuartwebframework.Inthisbook,youwilllearnaboutmicroservicearchitectureandhowitdiffersfromthetraditionalmonolithicapproach,whatthebenefitsare,aswellaspotentialtroublethatneedsovercoming.WhothisbookisforThisbookisforpeoplefamiliarwiththefundamentalsofthePythonprogramminglanguagewhowanttostartwritingwebservices,orwhohaveinheritedawebservicetheywanttomodernize.ItisexpectedthatthereaderwillbefamiliarwithsimplePythonstructuressuchasfunctionsandloops,aswellassomemoreadvancedfeaturessuchasdecorators.Somefamiliaritywithusingwebapplicationswouldbehelpful,althoughthefundamentalsofapplicationdesignarecovered.WhatthisbookcoversChapter1,UnderstandingMicroservices,introducestheconceptsbehindmicroservices,thedifferencesbetweenmonolithicapplicationsandmicroservices,commonbenefitsandpitfalls,aswellastestingandscaling.Chapter2,DiscoveringQuart,coverstheQuartwebframeworkandthewaysinwhichitcanrespondtorequests,createtemplateddocuments,actasmiddleware,handleerrors,andreadconfiguration.PrefacexiiChapter3,Coding,Testing,andDocumentation:
theVirtuousCycle,teachesyouaboutthedifferenttypesoftestingthatarepossible,whatbenefitseachtypehas,andhowtosetupautomatictesting,aswellasgeneratingdocumentationinCIpipelines.Chapter4,DesigningJeeves,looksatJeeves,whichisthesampleapplicationweuseinthisbooktoexplainthevariousconceptsbehindmicroservices.WeintroducewhatweneedJeevestodoanddescribeamonolithicapproachtoapplicationdesign,coveringthewebAPIinterface,databaseuse,andworkerpools.Chapter5,SplittingtheMonolith,buildsonthemonolithicJeevesdescribedinpreviouschapter.Thischapteroffersguidanceonhowtoidentifycomponentsthatmaybegoodmicroservices,measuringtheeffectsofchangesonthesoftwarearchitecture,andhowtocleanlymigratefeaturestonewmicroservices.Chapter6,InteractingwithOtherSer