SpringCloud:創(chuàng)建服務(wù)的提供方eureka-client

下面我們創(chuàng)建提供服務(wù)的客戶(hù)端,并向服務(wù)注冊(cè)中心注冊(cè)自己。本文我們主要介紹服務(wù)的注冊(cè)與發(fā)現(xiàn),所以我們不妨在服務(wù)提供方中嘗試著提供一個(gè)接口來(lái)獲取當(dāng)前所有的服務(wù)信息。

首先,創(chuàng)建一個(gè)基本的Spring Boot應(yīng)用。命名為eureka-client,在pom.xml中,加入如下配置:

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath />
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Dalston.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

其次,實(shí)現(xiàn)/dc請(qǐng)求處理接口,通過(guò)DiscoveryClient對(duì)象,在日志中打印出服務(wù)實(shí)例的相關(guān)內(nèi)容。
<pre>
@RestController
public class DcController {

@Autowired
DiscoveryClient discoveryClient;

@GetMapping("/dc")
public String dc() {
    String services = "Services: " + discoveryClient.getServices();
    System.out.println(services);
    return services;
}

}</pre>
最后在應(yīng)用主類(lèi)中通過(guò)加上@EnableDiscoveryClient注解,該注解能激活Eureka中的DiscoveryClient實(shí)現(xiàn),這樣才能實(shí)現(xiàn)Controller中對(duì)服務(wù)信息的輸出。
<pre>
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

public static void main(String[] args) {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
}

}
</pre>
我們?cè)谕瓿闪朔?wù)內(nèi)容的實(shí)現(xiàn)之后,再繼續(xù)對(duì)application.properties做一些配置工作,具體如下:
<pre>
spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
</pre>
通過(guò)spring.application.name
屬性,我們可以指定微服務(wù)的名稱(chēng)后續(xù)在調(diào)用的時(shí)候只需要使用該名稱(chēng)就可以進(jìn)行服務(wù)的訪(fǎng)問(wèn)。eureka.client.serviceUrl.defaultZone
屬性對(duì)應(yīng)服務(wù)注冊(cè)中心的配置內(nèi)容,指定服務(wù)注冊(cè)中心的位置。為了在本機(jī)上測(cè)試區(qū)分服務(wù)提供方和服務(wù)注冊(cè)中心,使用server.port
屬性設(shè)置不同的端口。
啟動(dòng)該工程后,再次訪(fǎng)問(wèn):http://localhost:1001/??梢匀缦聢D內(nèi)容,我們定義的服務(wù)被成功注冊(cè)了。

Paste_Image.png

當(dāng)然,我們也可以通過(guò)直接訪(fǎng)問(wèn)eureka-client
服務(wù)提供的/dc
接口來(lái)獲取當(dāng)前的服務(wù)清單,只需要訪(fǎng)問(wèn):http://localhost:2001/dc,我們可以得到如下輸出返回:
<pre>
Services: [eureka-client]
</pre>
其中,方括號(hào)中的eureka-client就是通過(guò)Spring Cloud定義的DiscoveryClient接口在eureka的實(shí)現(xiàn)中獲取到的所有服務(wù)清單。由于Spring Cloud在服務(wù)發(fā)現(xiàn)這一層做了非常好的抽象,所以,對(duì)于上面的程序,我們可以無(wú)縫的從eureka的服務(wù)治理體系切換到consul的服務(wù)治理體系中區(qū)。
到此,服務(wù)的注冊(cè)中心已經(jīng)建好,請(qǐng)關(guān)注下篇:SpringCloud:服務(wù)的消費(fèi)者eureka-consumer
參考資料:Spring Cloud基礎(chǔ)教程,謝謝DIDI大神

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

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

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