場景
SpringCloud-服務注冊與實作-Eureka創建服務注冊中心(附原始碼下載):
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102535957
上面已經搭建好服務注冊中心,開始創建服務提供者,
當 Client 向 Server 注冊時,它會提供一些元資料,例如主機和埠,URL,主頁等,Eureka Server 從每個 Client 實體接收心跳訊息, 如果心跳超時,則通常將該實體從注冊 Server 中洗掉,
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程式猿
獲取編程相關電子書、教程推送與免費下載,
實作
參照上面創建服務注冊中心的流程,再新建目錄hello-spring-cloud-service-admin
然后依次新建pom.xml并將其進行托管,新建src/main/java目錄和src/main/resources目錄并分別進行目錄設定,
然后在java下新建包,包下新建啟動類,在resources下新建組態檔application.yml,
完成后的目錄為:

其中pom.xml代碼:
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.badao</groupId> <artifactId>hello-spring-cloud-dependencies</artifactId> <version>1.0.0-SNAPSHOT</version> <relativePath>../hello-spring-cloud-dependencies/pom.xml</relativePath> </parent> <artifactId>hello-spring-cloud-service-admin</artifactId> <packaging>jar</packaging> <name>hello-spring-cloud-service-admin</name> <url>https://blog.csdn.net/badao_liumang_qizhi</url> <inceptionYear>2019-Now</inceptionYear> <dependencies> <!-- Spring Boot Begin --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Spring Boot End --> <!-- Spring Cloud Begin --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!-- Spring Cloud End --> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.badao.hello.spring.cloud.service.admin.ServiceAdminApplication</mainClass> </configuration> </plugin> </plugins> </build></project>
注:
這里的parent標簽要與上面的統一的依賴管理對應起來,
要修改指定的程式入口類為自己相應的路徑,
然后應用啟動類的代碼:
package com.badao.hello.spring.cloud.service.admin;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClientpublic class ServiceAdminApplication { public static void main(String[] args) { SpringApplication.run(ServiceAdminApplication.class, args); }}
注:
要使用@EnableEurekaClient注解宣告這是一個Eureka Client,用來提供服務,
然后是組態檔代碼:
spring: application: name: hello-spring-cloud-service-adminserver: port: 8762eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
注:
1.服務注冊與發現是根據上面的name去尋找,
2.port表示埠號,
3.hostname表示eureka服務的地址,這里是本地所以是localhost,
4.serviceURL設定eureka的地址,與上面創建服務注冊中心時的URL對應,
然后打開Maven面板,雙擊Install,不然會提示程式找不到啟動類,
這時如果啟動應用程式,訪問8762什么也沒有,因為沒有提供具體的服務,但是訪問8761能看到服務已經被注冊和發現了,

接下來新建controller包,并在包下新建AdminController
package com.badao.hello.spring.cloud.service.admin.controller;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class AdminController { @Value("${server.port}") private String port; @RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(@RequestParam(value = https://www.cnblogs.com/badaoliumangqizhi/p/"message") String message) { return String.format("Hi,your message is : %s i am from port : %s", message, port); }}
然后停止掉原來的啟動程式,將專案重新install,然后將上面的服務與注冊發現的eureka服務啟動,然后再啟動當前服務提供者主程式,
打開瀏覽器輸入:
localhost:8762/hi?message=badao

原始碼下載
https://download.csdn.net/download/badao_liumang_qizhi/11859914
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/44247.html
標籤:架構設計
上一篇:該如何選擇訊息佇列?
下一篇:有關系統架構的高可用原則
