บทความข้างต้นได้เขียนวิธีการสร้างศูนย์ลงทะเบียนแล้ว มันยังห่างไกลพอที่จะมีศูนย์ลงทะเบียนดังนั้นเราต้องลงทะเบียนในศูนย์ลงทะเบียนและให้บริการ สิ่งนี้เรียกว่าผู้ให้บริการลงทะเบียน
หลักฐาน
อ่านด้านบนและสร้างศูนย์ลงทะเบียนได้สำเร็จโดยไม่ต้องเปลี่ยนสภาพแวดล้อม
การก่อสร้างโครงการ
ที่นี่เราต้องสร้างโครงการ Maven ใหม่ ชื่อโครงการยังไม่ได้รับการยกมาก่อน ให้ฉันอ้างถึงที่นี่ ของฉันคือ SpringCloudDemo ไม่สนใจรายละเอียดเหล่านี้!
ในการแก้ไขไฟล์ POM โปรดดูสิ่งต่อไปนี้:
หมายเหตุ: โปรดดูหมายเลขเวอร์ชันของแพ็คเกจ JAR เหล่านี้ ในตอนท้ายของบทความฉันจะโพสต์เส้นทาง GitHub ของการสาธิตง่าย ๆ สองแบบที่ฉันสร้างขึ้นมาก่อน
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/ XSI: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" <ArtIfactId> SpringCloudDemo </artifactId> <sersion> 0.0.1-Snapshot </เวอร์ชัน> <packaging> jar </packaging> <name> SpringCloudDemo </name> <scription> โครงการสาธิตสำหรับการบูตฤดูใบไม้ผลิ </คำอธิบาย> <carent> <ArtIfactId> Spring-Boot-Starter-Parent </artifactid> <sersion> 1.5.9.release </เวอร์ชัน> <inelypath/> <!-การค้นหาผู้ปกครองจากที่เก็บ-> </parent> <การพึ่งพาการจัดการ> <การพึ่งพา> <perdency> <Sersion> CAMDEN.SR3 </เวอร์ชัน> <pley> POM </type> <pope> นำเข้า </cope> </dercendency> </การพึ่งพาอาศัย> </derpendency> </การพึ่งพาการจัดการ> <properties> <project.build.sourceencoding> <Project.Reporting.UutputEncoding> UTF-8 </Project.Reporting.UutputPutenCoding> <Java.Version> 1.8 </Java.Version> </premerties> <การพึ่งพา> <การพึ่งพา </permentency> <predency> <roupId> org.springframework.boot </groupId> <ratifactid> การทดสอบสปริง-สตาร์เทสต์ </artifactid> <pope> ทดสอบ </cope> </การพึ่งพา> <!-ใช้เพื่อตรวจสอบโครงการและให้ข้อมูลสถานะในโครงการ <ArtIfactId> Spring-Boot-Starter-Actuator </artifactId> </derpendency> <!-การทดสอบ junit-> <การพึ่งพา> <sdependency> <roupId> Junit </groupId> <ratifactid> Junit </artifactid> <cersion> 4.8.2 </เวอร์ชัน> <ArtIfactId> Spring-Cloud-Starter-Eureka </artifactId> </dermentrency> <การพึ่งพา> <roupId> org.springframework.cloud </groupid> <ratifactid> Spring-Cloud-config-Server </artifactid> <RoupID> org.springframework.boot </groupId> <ratifactid> Spring-Boot-Maven-Plugin </artifactid> </plugin> <plugin> <loupid> org.apache.maven.plugins </Groupid> <source> 1.8 </source> </การกำหนดค่า> </plugin> </plugins> </uffer> </project>
แม้ว่าหมายเลขเวอร์ชันจะแตกต่างจากโครงการ Eurekaserver Center Center แต่ก็สามารถใช้งานได้ตามปกติผ่านการฝึกฝน โปรดพักผ่อน
สร้างคลาสเริ่มต้นใหม่ (ในทุกโครงการ Springboot)
แพ็คเกจ com.hellxz.springcloudhelloworld; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.cloud.client.discovery.discovery Eurekaclient * @date: 2018/4/13 16:57 */@enablediscoveryclient@springbootapplicationpublic คลาส SpringCloudDemoApplication {โมฆะสาธารณะคงที่หลัก (สตริง [] args) {springapplication.run - สร้างคลาสคอนโทรลเลอร์ใหม่และทิ้งไว้สำหรับการทดสอบในภายหลัง
แพ็คเกจ com.hellxz.springcloudhelloworld; นำเข้า org.apache.log4j.logger; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.cloud.client.serviceinstance; org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.requestmethod; นำเข้า org.springframework.web.bind.annotation.restcontroller; 11:36 */ @restcontrollerpublic คลาส SpringbootController {@autowired Private DiscoveryClient ไคลเอนต์; // Inject Discovery Client ส่วนตัว Logger สุดท้าย Logger = logger.getLogger (SpringBootController.class); @RequestMapping (value = "/hello", method = requestMethod.get) สตริงสาธารณะสวัสดี () {// รับอินสแตนซ์บริการและฟังก์ชั่นคือการแสดงผลของคอนโซลบริการ ServiceInstance ServiceInstance = client.getLocalserviceInstance (); logger.info ("/hello host:"+serviceinstance.gethost ()+"service_id:"+serviceInstance.getServiceId ()); กลับ "สวัสดี"; -สร้าง application.yml ในโฟลเดอร์ SRC/Resources เวลานี้ใช้ YAML สำหรับการกำหนดค่า หากคุณต้องการลองใช้วิธีไฟล์คุณสมบัติโปรดดูที่ด้านบน โปรดดูการกำหนดค่าของศูนย์ลงทะเบียนสำหรับที่อยู่บริการที่กำหนดค่าไว้ที่นี่
เซิร์ฟเวอร์: พอร์ต: 8080SPRING: แอปพลิเคชัน: ชื่อ: Hello-ServiceEureka: ไคลเอน
ตกลงเราจะเรียกใช้โครงการนี้บนพอร์ต 8080 และสามารถไปที่ศูนย์ลงทะเบียนเพื่อลงทะเบียนบริการ
เริ่มโครงการในศูนย์การลงทะเบียนก่อนและหลังจากเริ่มต้นแล้วให้เริ่มโครงการ
ทดสอบ
ป้อน URL ของศูนย์ลงทะเบียนเพื่อดู: LocalHost: 1111
เข้าถึงเส้นทางคอนโทรลเลอร์ที่คุณเพิ่งกำหนดค่า: http: // localhost: 8080/สวัสดี
ดังที่แสดงในภาพทางด้านขวาการลงทะเบียนจะประสบความสำเร็จ
ในเวลานี้เราสามารถใช้โครงการนี้เพื่อให้บริการ
ตัวอย่างตัวอย่าง:
https://github.com/hellxz/eurekaserver
https://github.com/hellxz/eurekaclient
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น