javascript
SpringCloud-服务注册与实现-Eureka创建服务提供者(附源码下载)
場景
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 @EnableEurekaClient public 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;@RestController public class AdminController {@Value("${server.port}")private String port;@RequestMapping(value = "hi", method = RequestMethod.GET)public String sayHi(@RequestParam(value = "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
總結
以上是生活随笔為你收集整理的SpringCloud-服务注册与实现-Eureka创建服务提供者(附源码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DevExpress的TreeList实
- 下一篇: gradle idea java ssm