1. 程式人生 > >第三篇:服務治理:Spring Cloud Eureka

第三篇:服務治理:Spring Cloud Eureka

1、服務治理是微服務框架中的核心與基礎,主要用來實現各個微服務例項的自動化註冊和發現。

           分為服務註冊和服務發現。

2、引入相應依賴模組

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<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.SR5</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

3、使用@EnableEurekaServer註解註冊一個註冊中心

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class DemoApplication {

	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}
}

在預設設定下,該服務中心也會將自己作為客戶端嘗試註冊自己,我們可以禁用他的客戶端註冊行為,在application.properties檔案中增加配置屬性

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

其中 Instances currently registered with Eureka為空,說明該註冊中心還沒有註冊任何服務