SpringCloud微服務(wù)架構(gòu)——服務(wù)注冊與發(fā)現(xiàn)

Eureka用于實現(xiàn)服務(wù)治理。
1.創(chuàng)建『服務(wù)注冊中心』
首先創(chuàng)建springboot工程,命名eureka-server,并在pom.xml中引入依賴內(nèi)容:
<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-server</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>

引入pom內(nèi)容之后通過在啟動類中添加注解@EnableEurekaServer啟動一個服務(wù)中心提供給其他應(yīng)用進行對話:

@EnableEurekaServer

@SpringBootApplication

public class Application{

public static void main(String[] args) {

new SpringApplicationBuilder(Application.class)

.web(true).run(args);

}
}

application.properties配置如下:

spring.application.name=eureka-server

server.port=1001

eureka.instance.hostname=localhost

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

啟動工程后,訪問:http://localhost:1001/

2.創(chuàng)建服務(wù)提供方:
下面我們創(chuàng)建服務(wù)得客戶端,并向服務(wù)注冊中心注冊自己;
首先創(chuàng)建一個springboot應(yīng)用,命名為eureka-client,引入pom文件
<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>

其次,實現(xiàn)/dc請求處理接口,通過DiscoveryClient對象,在日志中打印出服務(wù)實例的相關(guān)內(nèi)容。

@RestController

public class DcController {

@Autowired

DiscoveryClient discoveryClient;

@GetMapping("/dc")

public String dc() {

String services = "Services: " + discoveryClient.getServices();

System.out.println(services);

return services;

}
}

最后在應(yīng)用主類中通過加上@EnableDiscoveryClient注解,該注解能激活Eureka中的DiscoveryClient實現(xiàn),這樣才能實現(xiàn)Controller中對服務(wù)信息的輸出。

@EnableDiscoveryClient

@SpringBootApplication

public class Application {

public static void main(String[] args) {

new SpringApplicationBuilder(

ComputeServiceApplication.class)

.web(true).run(args);

}

}

我們在完成了服務(wù)內(nèi)容的實現(xiàn)之后,再繼續(xù)對application.properties做一些配置工作,具體如下:

spring.application.name=eureka-client

server.port=2001

eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

通過spring.application.name屬性,我們可以指定微服務(wù)的名稱后續(xù)在調(diào)用的時候只需要使用該名稱就可以進行服務(wù)的訪問。eureka.client.serviceUrl.defaultZone屬性對應(yīng)服務(wù)注冊中心的配置內(nèi)容,指定服務(wù)注冊中心的位置。為了在本機上測試區(qū)分服務(wù)提供方和服務(wù)注冊中心,使用server.port屬性設(shè)置不同的端口。

啟動該工程后,再次訪問:http://localhost:1001/。可以如下圖內(nèi)容,我們定義的服務(wù)被成功注冊了。

image

當然,我們也可以通過直接訪問eureka-client服務(wù)提供的/dc接口來獲取當前的服務(wù)清單,只需要訪問:http://localhost:2001/dc,我們可以得到如下輸出返回:Services: [eureka-client]

其中,方括號中的eureka-client就是通過Spring Cloud定義的DiscoveryClient接口在eureka的實現(xiàn)中獲取到的所有服務(wù)清單。由于Spring Cloud在服務(wù)發(fā)現(xiàn)這一層做了非常好的抽象,所以,對于上面的程序,我們可以無縫的從eureka的服務(wù)治理體系切換到consul的服務(wù)治理體系中區(qū)。

?著作權(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)容