在這裏插入圖片描述html
歡迎來到菜鳥SpringCloud入門實戰系列(SpringCloudForNoob),該系列經過層層遞進的實戰視角,來一步步學習和理解SpringCloud。git
本系列適合有必定Java以及SpringBoot基礎的同窗閱讀。程序員
每篇文章末尾都附有本文對應的Github源代碼,方便同窗調試。github
Github倉庫地址:web
https://github.com/qqxx6661/springcloud_for_noob面試
你還能夠經過如下兩種途徑查看菜鳥SpringCloud入門實戰系列:算法
SpringBoot:2.0.3.RELEASE
SpringCloud:Finchley.RELEASEspring
首先建立子模塊eureka-hi,做爲服務提供者模塊apache
編輯其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> <artifactId>eureka-hi</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka-hi</name> <packaging>jar</packaging> <description>Demo project for Spring Boot</description> <!--父工程的依賴--> <parent> <groupId>com.pricemonitor</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <dependencies> </dependencies> </project>
以後別忘了在主模塊pom加上該子模塊:
<!--子模塊--> <modules> <module>eureka</module> <module>eureka-hi</module> </modules>
注意,服務調用者是一個須要註冊到Eureka的客戶端,因此是client不是server。
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; @RestController @SpringBootApplication @EnableEurekaClient public class EurekaHiApplication { public static void main(String[] args) { SpringApplication.run(EurekaHiApplication.class, args); } /** * 獲取端口號 */ @Value("${server.port}") String port; /** * 定義一個簡單接口 * * @param name * @return */ @GetMapping("/hi/{name}") public String home(@PathVariable String name) { return "hi " + name + ",I am from port :" + port; } }
代碼重點:
# 端口號 server: port: 8763 # 服務名稱,即serviceId spring: application: name: service-hi # 服務註冊與發現相關配置 eureka: client: # 服務註冊地址 serviceUrl: defaultZone: http://localhost:8761/eureka/
代碼重點:
同時運行剛纔端口8761的eureka和端口8763的eureka-hi模塊,這裏使用很方便的IDEA Run DashBoard。在Run Dashboard裏能夠同時對幾個應用進行運行中止等操做。
在這裏插入圖片描述
述
查看:http://localhost:8761/
發現服務端有了正在提供服務的SERVICE-HI
在這裏插入圖片描述
查看:http://localhost:8763/hi/rude3knife
說明服務能夠直接調用
在這裏插入圖片描述
問題來了,若是關閉了8761端口的eureka server,會發生什麼狀況?
若是關閉了8761端口的eureka,直接訪問http://localhost:8763/hi/xxxx,eureka-hi控制檯已經開始報錯,沒法鏈接上Eureka註冊中心。
在這裏插入圖片描述
讓咱們從新運行Eureka註冊中心
在這裏插入圖片描述
從新運行後,8763的eureka-hi服務又自動從新在8761的註冊中心處進行了註冊。
在這裏插入圖片描述
https://github.com/qqxx6661/springcloud_for_noob/tree/master/02-eureka-hi
Spring-Cloud筆記03:服務註冊中心Eureka Server的簡單配置、訪問控制配置以及高可用配置
https://blog.csdn.net/hanchao5272/article/details/80561199
springcloud(三):服務提供與調用
http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
全複習手冊文章導航:經過如下兩種途徑查看
菜鳥SpringCloud實戰專欄
我是蠻三刀把刀,後端開發。
主要關注後端開發,數據安全,爬蟲等方向。
來微信和我聊聊:yangzd1102
Github我的主頁:
同步更新公衆號及如下博客
擁有專欄:
https://www.zhihu.com/people/yang-zhen-dong-1/
擁有專欄:
Java程序員面試複習手冊
LeetCode算法題詳解與代碼實現
後臺開發實戰
https://juejin.im/user/5b48015ce51d45191462ba55
https://www.jianshu.com/u/b5f225ca2376
我的公衆號:Rude3Knife
若是文章對你有幫助,不妨收藏起來並轉發給您的朋友們~