[菜鳥SpringCloud入門]第二章:創(chuàng)建服務(wù)提供者并在Eureka進行注冊

在這里插入圖片描述

前言

歡迎來到菜鳥SpringCloud入門實戰(zhàn)系列(SpringCloudForNoob),該系列通過層層遞進的實戰(zhàn)視角,來一步步學(xué)習(xí)和理解SpringCloud。

本系列適合有一定Java以及SpringBoot基礎(chǔ)的同學(xué)閱讀。

每篇文章末尾都附有本文對應(yīng)的Github源代碼,方便同學(xué)調(diào)試。

實戰(zhàn)版本

  • SpringBoot:2.0.3.RELEASE
  • SpringCloud:Finchley.RELEASE

-----正文開始-----

創(chuàng)建服務(wù)提供者并在Eureka進行注冊

首先創(chuàng)建子模塊eureka-hi,作為服務(wù)提供者模塊

編輯其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>

添加注解@EnableEurekaClient

注意,服務(wù)調(diào)用者是一個需要注冊到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;
    }

}

代碼重點:

  • 實現(xiàn)了一個簡易的RESTful接口
  • 添加@EnableEurekaClient

編輯配置文件application.yml

# 端口號
server:
  port: 8763
# 服務(wù)名稱,即serviceId
spring:
  application:
    name: service-hi
# 服務(wù)注冊與發(fā)現(xiàn)相關(guān)配置
eureka:
  client:
    # 服務(wù)注冊地址
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

代碼重點:

運行

同時運行剛才端口8761的eureka和端口8763的eureka-hi模塊,這里使用很方便的IDEA Run DashBoard。在Run Dashboard里可以同時對幾個應(yīng)用進行運行停止等操作。

在這里插入圖片描述

查看:http://localhost:8761/

發(fā)現(xiàn)服務(wù)端有了正在提供服務(wù)的SERVICE-HI

在這里插入圖片描述

查看:http://localhost:8763/hi/rude3knife

說明服務(wù)可以直接調(diào)用

在這里插入圖片描述

問題來了,如果關(guān)閉了8761端口的eureka server,會發(fā)生什么情況?

如果關(guān)閉了8761端口的eureka,直接訪問http://localhost:8763/hi/xxxx,eureka-hi控制臺已經(jīng)開始報錯,無法連接上Eureka注冊中心。

在這里插入圖片描述

讓我們重新運行Eureka注冊中心

在這里插入圖片描述

重新運行后,8763的eureka-hi服務(wù)又自動重新在8761的注冊中心處進行了注冊。

在這里插入圖片描述

本文教程所對應(yīng)工程代碼

https://github.com/qqxx6661/springcloud_for_noob/tree/master/02-eureka-hi

參考

Spring-Cloud筆記03:服務(wù)注冊中心Eureka Server的簡單配置、訪問控制配置以及高可用配置

https://blog.csdn.net/hanchao5272/article/details/80561199

springcloud(三):服務(wù)提供與調(diào)用

http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容