Spring Cloud เป็นคอลเลกชันที่ได้รับคำสั่งของชุดของเฟรมเวิร์ก มันใช้ความสะดวกในการพัฒนาของ Spring Boot เพื่อลดความซับซ้อนของการพัฒนาโครงสร้างพื้นฐานระบบแบบกระจายอย่างชาญฉลาดเช่นการลงทะเบียนการค้นพบบริการศูนย์การกำหนดค่าบัสข้อความการปรับสมดุลโหลดเบรกเกอร์วงจรการตรวจสอบข้อมูล ฯลฯ ซึ่งสามารถเริ่มต้นและปรับใช้ในการคลิกเดียว ฤดูใบไม้ผลิไม่ได้ผลิตล้อซ้ำ ๆ มันรวมกรอบการบริการที่ค่อนข้างเป็นผู้ใหญ่และใช้งานได้จริงที่พัฒนาโดย บริษัท ต่าง ๆ มันจัดทำใหม่และป้องกันการกำหนดค่าที่ซับซ้อนและหลักการการใช้งานผ่านสไตล์การบูตฤดูใบไม้ผลิและในที่สุดก็ทำให้นักพัฒนาซอฟต์แวร์มีความเรียบง่ายเข้าใจง่ายปรับใช้และง่ายต่อการดูแลชุดเครื่องมือพัฒนาระบบแบบกระจาย
ต่อไปเราจะใช้ SpringCloud เพื่อใช้สถาปัตยกรรม Microservice อย่างง่าย
รหัสต่อไปนี้ทั้งหมดได้รับการเปิดให้กับ GitHub, ที่อยู่: https://github.com/lynnlovemin/sofservice
ยูเรก้า (การลงทะเบียนและการค้นพบ)
แรกแนะนำแพ็คเกจการพึ่งพาที่เกี่ยวข้อง
<carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.5.9.release </version> <inelypath/> <! <Project.build.sourceencoding> UTF-8 </project.build.sourceencoding> <project.raporting.OutputUntencoding> UTF-8 </project.Reporting.outputencoding> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-eureka </artifactid> </serpendency> <perdency> <droupId> org.springframework.cloud </groupid> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Web </artifactid> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> org.springframework.boot </groupid> </sderctency> </dermentrency> <การพึ่งพา> <roupId> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Dependencies </artifactid> <version> Dalston.rc1 </Sidependency> <plugins> <splousid> org.springframework.boot </groupid> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> <url> https://repo.spring.io/milestone </url> <napshots> <enabled> false </enabled> </napshots> </repository> </repositories>
กำหนดค่า application.yml
เซิร์ฟเวอร์: พอร์ต: 8761eureka: อินสแตนซ์: ชื่อโฮสต์: ไคลเอนต์ localhost: registerweheadeureka: felte fetchregistry: false serviceurl: defaultzone: http: // $ {eeureka.instance.hostname}: $ {server.port}/eureka/ieureka/สร้างแอปพลิเคชันคลาสเริ่มต้น
@enableeurekaserver@springbootapplicationpublic คลาสแอปพลิเคชัน {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (application.class, args); - เรียกใช้วิธีหลักและการเข้าชมเบราว์เซอร์: http: // localhost: 8761 และเราสามารถดูอินเทอร์เฟซต่อไปนี้ในเบราว์เซอร์:
หมายความว่ายูเรก้าเริ่มประสบความสำเร็จ
ต่อไปเราตระหนักถึงยอดคงเหลือหนี้เบรกเกอร์เกตเวย์และลูกค้า บริการทั้งหมดควรลงทะเบียนในยูเรก้าและโดยการเยี่ยมชมยูเรก้าคุณสามารถดูบริการที่ลงทะเบียนทั้งหมดได้
ไคลเอนต์ (ไคลเอนต์)
pom.xml
<carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.5.9.release </version> <inelypath/> <! <Project.build.sourceencoding> UTF-8 </project.build.sourceencoding> <project.raporting.OutputUntencoding> UTF-8 </project.Reporting.outputencoding> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-eureka </artifactid> </serpendency> <perdency> <sderndency> <RoupID> org.springframework.boot </groupId> <ratifactid> การทดสอบสปริง-สตาร์เทสต์ </artifactid> <pope> ทดสอบ </cope> </predency> <ArtIfactId> สปริงคลาวด์-พึ่งพาอาศัยกัน </artifactid> <persion> dalston.rc1 </version> <ply> pom </type> <pope> นำเข้า </cope> </predency> </predency> <ArtIfactId> Spring-Boot-Maven-Plugin </artifactId> </plugin> </plugins> </uffle> <positories> <ที่เก็บ> <id> Spring-Milestones </id> <name> MILESTONES </name> </snapshots> </repository> </repository> </repository> </repository> </repository> </repository>
application.yml
ยูเรก้า: ไคลเอนต์: ServiceUrl: DefaultZone: http: // localhost: 8761/uureka/#register ที่นี่ในเซิร์ฟเวอร์ยูเรก้า: พอร์ต: 8763Spring: แอปพลิเคชัน: ชื่อ: บริการฮิ
คลาสแอปพลิเคชัน
@springbootapplication@enableeurekaclient@retcontrollerpublic คลาสแอปพลิเคชัน {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (applicatioin.class, args); } @Value ("$ {server.port}") พอร์ตสตริง; // ที่นี่เรามีอินเทอร์เฟซ @RequestMapping ("/hi") สตริงสาธารณะบ้าน (@requestparam ชื่อสตริงชื่อ) {return "hi"+name+"ฉันมาจากพอร์ต:"+พอร์ต; -Feign (ดุลหนี้, เบรกเกอร์)
pom.xml
<carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.5.9.release </version> <inelypath/> <! <Project.build.sourceencoding> UTF-8 </project.build.sourceencoding> <project.raporting.OutputUntencoding> UTF-8 </project.Reporting.outputencoding> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-eureka </artifactid> </serpendency> <perdency> <sderndency> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-Feign </artifactid> </derpendency> <perdency> <sderctId> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-Hystrix-Dashboard </artifactid> </การพึ่งพาอาศัย> <การพึ่งพา> <roupid> org.springframework.boot </groupid> </การพึ่งพา> </การพึ่งพา> </การพึ่งพา> <การพึ่งพาการจัดการ> <การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.cloud </groupid> <ratifactid> การพึ่งพาสปริง-พึ่งพา </artifactid> <persion> dalston.rc1 < </serperencyManagement> <uffer> <plugins> <plugin> <molin> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> </plugins> </uffories> <url> https://repo.spring.io/milestone </url> <napshots> <enabled> false </enabled> </napshots> </repositories>
application.yml
ยูเรก้า: ไคลเอนต์: ServiceUrl: DefaultZone: http: // localhost: 8761/uureka/เซิร์ฟเวอร์: พอร์ต: 8765Spring: แอปพลิเคชัน: ชื่อ: Service-Feignfeign: Hystrix: เปิดใช้งาน: จริง
คลาสแอปพลิเคชัน
@springbootapplication@enablecoveryclient@enablefeignclients@enablehystrixdashboardpublic คลาสแอปพลิเคชัน {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (application.class, args); -จากนั้นให้บริการฟังก์ชั่นของมันคือการทำยอดคงเหลือหนี้และฟังก์ชั่นเบรกเกอร์วงจร
@feignClient (value = "service-hi", fallback = schedualServiceHihystrict.class) ส่วนต่อประสานสาธารณะ schedualServiceHi {@requestmapping (value = "/hi", method = requestmethod.get) string sayshifromclientone (@requestparam @ComponentPublic คลาส SchedualServiceHihyStrict ใช้ schedualServiceHi {@Override สตริงสาธารณะ SaysHifRomClientOne (ชื่อสตริง) {return "ขออภัย"+ชื่อ; -FeignClient เราระบุชื่อ: Service-HI, ทางเลือกที่ระบุเมื่อสร้างไคลเอนต์มาก่อน ด้วยวิธีนี้เมื่อเราเริ่มต้นลูกค้าหลายรายเราจะเห็นว่าพอร์ตแสร้งที่แตกต่างกันจะถูกเข้าถึงอีกสลับกันเมื่อคำขอ HTTP เมื่อไคลเอนต์หยุดการเข้าถึงอินเทอร์เฟซจะส่งคืนข้อความแสดงข้อผิดพลาด
Zuul (Service Gateway)
โดยทั่วไปเราจะไม่เปิดเผยลูกค้าไปยังโลกภายนอกโดยตรง แต่ส่งต่อผ่านทางเกตเวย์บริการ บริการภายในได้รับการสื่อสารภายในเครือข่ายท้องถิ่นและไม่สามารถเข้าถึงได้จากโลกภายนอก ผ่านเกตเวย์บริการเรายังสามารถทำการตรวจสอบความปลอดภัยของอินเทอร์เฟซและสกัดกั้นได้อย่างสม่ำเสมอ โปรดดูรหัส:
pom.xml
<carent> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Starter-Parent </artifactid> <version> 1.5.9.release </version> <inelypath/> <! <Project.build.sourceencoding> UTF-8 </project.build.sourceencoding> <project.raporting.OutputUntencoding> UTF-8 </project.Reporting.outputencoding> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-eureka </artifactid> </serpendency> <perdency> <sderndency> <RoupID> org.springframework.cloud </groupId> <ratifactid> Spring-Cloud-Starter-Zuul </artifactid> </การพึ่งพาอาศัย> <การพึ่งพา> <roupId> org.springframework.boot </groupid> </permentency> <การพึ่งพาการจัดการ> <การพึ่งพา> <การพึ่งพา> <roupId> org.springframework.cloud </groupid> <ratifactid> สปริงคลาวด์-พึ่งพาอาศัยกัน </artifactid> <uild> <plugins> <roupId> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> <url> https://repo.spring.io/milestone </url> <napshots> <enabled> false </enabled> </napshots> </repository>
application.yml
Eureka: ไคลเอนต์: ServiceUrl: DefaultZone: http: // localhost: 8761/uureka/เซิร์ฟเวอร์: พอร์ต: 8080spring: แอปพลิเคชัน: ชื่อ: Service-Zuulzuul: เส้นทาง: API-B: PATH:/API/** บริการ: บริการ
คลาสแอปพลิเคชัน
@enablezuulproxy@enableeurekaclient@springbootapplicationpublic คลาสแอปพลิเคชัน {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {springapplication.run (application.class, args); -เริ่มแอปพลิเคชันและการเข้าถึง: http: // localhost: 8080/API/HI เพื่อเข้าถึงอินเทอร์เฟซที่เรากำหนดไว้ก่อนหน้านี้ ต่อไปเราสกัดกั้นอินเทอร์เฟซ:
/*** FilterType: ส่งคืนสตริงที่แสดงถึงประเภทของตัวกรอง สี่ประเภทตัวกรองที่มีวงจรชีวิตที่แตกต่างกันถูกกำหนดไว้ใน Zuul ดังต่อไปนี้: ก่อน: ก่อนการกำหนดเส้นทาง: เมื่อโพสต์การกำหนดเส้นทาง: หลังจากการกำหนดเส้นทางข้อผิดพลาด: ส่งข้อผิดพลาดการโทร FilterOrder: ตัวกรอง: คำสั่งตัวกรองควรฟิลเตอร์: ที่นี่คุณสามารถเขียนคำตัดสินเชิงตรรกะ Run: ตรรกะเฉพาะของตัวกรอง มันซับซ้อนมากในการใช้งานรวมถึงการตรวจสอบ SQL และ NOSQL เพื่อตรวจสอบว่าคำขอมีสิทธิ์เข้าถึงหรือไม่ */@componentPublic คลาส MyFilter ขยาย ZuulFilter {logger แบบคงที่ส่วนตัว = loggerFactory.getLogger (myFilter.class); @Override Public String FilterType () {return "pre"; } @Override public int filterOrder () {return 0; } @Override บูลีนสาธารณะควร filter () {return true; } @Override วัตถุสาธารณะรัน () {requestcontext ctx = requestcontext.getCurrentContext (); httpservletRequest Request = ctx.getRequest (); log.info (string.format (" %s >>> %s", request.getMethod (), request.getRequesturl (). toString ())); Object AccessToken = request.getParameter ("โทเค็น"); if (accessToken == null) {log.warn ("โทเค็นว่างเปล่า"); ctx.setsendzuulresponse (เท็จ); CTX.SetResponSestatusCode (401); ลอง {ctx.getResponse (). getWriter (). เขียน ("โทเค็นว่างเปล่า"); } catch (exception e) {} return null; } log.info ("ตกลง"); คืนค่า null; - ด้วยวิธีนี้เราจะเรียกใช้วิธีการเรียกใช้ในคลาส MyFilter ก่อนที่จะเรียกอินเทอร์เฟซ ในวิธีนี้เราสามารถทำการตรวจสอบความปลอดภัยได้หลายชุดเช่นโทเค็น
ตกลงมีการสร้างสถาปัตยกรรม microservice อย่างง่าย
รหัสข้างต้นทั้งหมดได้รับการเปิดให้กับ GitHub ที่อยู่: https://github.com/lynnlovemin/sofservice
ด้านบนเป็นสถาปัตยกรรม Microservice อย่างง่ายที่ตัวแก้ไขแนะนำให้คุณรู้จัก ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับทันเวลา