Spring Cloud 2.x系列之 eureka註冊中心單機spring
1、簡介apache
Spring Cloud Eureka是Spring Cloud Netflix項目下的服務治理模塊。而Spring Cloud Netflix項目是Spring Cloud的子項目之一,主要內容是對Netflix公司一系列開源產品的包裝,它爲Spring Boot應用提供了自配置的Netflix OSS整合。經過一些簡單的註解,開發者就能夠快速的在應用中配置一下經常使用模塊並構建龐大的分佈式系統。它主要提供的模塊包括:服務發現(Eureka),斷路器(Hystrix),智能路由(Zuul),客戶端負載均衡(Ribbon)等。bootstrap
一、 新建一個maven項目:sc-eureka-server,其pom.xml配置以下:app
<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">maven
<modelVersion>4.0.0</modelVersion>分佈式
<groupId>spring-cloud</groupId>spring-boot
<artifactId>sc-eureka-server</artifactId>ui
<version>0.0.1-SNAPSHOT</version>url
<packaging>jar</packaging>
<name>sc-eureka-server</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.RELEASE</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
</dependencies>
</project>
備註:
主要引入eureka server所需的starter
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
Spring Cloud 1.x以前的eureka server的starter爲
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
在http://mvnrepository.com中央倉庫spring-cloud-starter-eureka-server已經被標誌爲過時,推薦使用spring-cloud-starter-netflix-eureka-server
二、 添加配置文件bootstrap.yml或者application.yml
spring:
application:
name: sc-eureka-server
server:
port: 5001
eureka:
instance:
hostname: 127.0.0.1
client:
#因爲該應用爲註冊中心,因此設置爲false,表明不向註冊中心註冊本身
registerWithEureka: false
#因爲註冊中心的職責就是維護服務實例,它並不須要去檢索服務,因此也設置爲false
frechRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
備註:也可使用application-dev.yml配置文件,可是添加以下配置:-Dspring.profiles.active=dev
三、 編寫啓動程序
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
四、 啓動程序,並驗證啓動成功
方式一:
方式二: