1. 程式人生 > 其它 >Spring Cloud(一)Eureka註冊中心

Spring Cloud(一)Eureka註冊中心

一、引言

  寫這篇文章時心情還是比較激動的,以前用習慣了Dubbo,一直覺得Spring Cloud這一套元件相當的多,學起來有點慌...

  隨著技術潮流的遞進,Spring Cloud生態已經逐漸成為主流的分散式系統解決方案,包括我們最近的專案終於開始用Spring Cloud了,那麼學習勢在必行,公司的實現程式碼不方便直接貼出來,所以這裡我從0開始搭建一整套Spring Cloud的基礎架構,來記錄一下我的學習過程。

  PS:Spring Cloud相關的概念我就不一一解釋了,網上一搜一大堆。其實能看到Spring Cloud的小夥伴應該對分散式系統都會有自己的理解了,所以我這裡廢話不多說,直接上實現程式碼~

二、Eureka註冊中心

  註冊中心主要是在分散式系統中扮演著服務治理與發現等角色,相關的實現有很多,比如我們之前說過的Zookeeper呀,Nacos,Eureka等等,這篇文章我們來學習Eureka,因為其AP的特性【可用性、分割槽容錯性】以及CS的模式【客戶端-服務端】,也是Spring Cloud官方推薦的,所以我們學就完事了~

  搭建Eureka註冊中心之前首先要建立一個Spring Boot工程,步驟就不放出來了,結構如下:

pom.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns
="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 <groupId>com.zhbf</groupId> 6 <artifactId
>euraka</artifactId> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>euraka</name> 9 <description>註冊中心【有夢想的肥宅】</description> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>1.5.6.RELEASE</version> 15 <relativePath/> 16 </parent> 17 18 <properties> 19 <java.version>1.8</java.version> 20 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 21 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 22 <spring-boot.version>1.5.6.RELEASE</spring-boot.version><!--Spring Boot版本--> 23 <spring-cloud.version>1.4.6.RELEASE</spring-cloud.version><!--Spring Cloud版本--> 24 </properties> 25 26 <dependencyManagement> 27 <dependencies> 28 <!--指定下載源和使用Spring Boot的版本--> 29 <dependency> 30 <groupId>org.springframework.boot</groupId> 31 <artifactId>spring-boot-dependencies</artifactId> 32 <version>${spring-boot.version}</version> 33 <type>pom</type> 34 <scope>import</scope> 35 </dependency> 36 <!--指定下載源和使用Spring Cloud的版本--> 37 <dependency> 38 <groupId>org.springframework.cloud</groupId> 39 <artifactId>spring-cloud-dependencies</artifactId> 40 <version>Edgware.SR5</version> 41 <type>pom</type> 42 <scope>import</scope> 43 </dependency> 44 </dependencies> 45 </dependencyManagement> 46 47 48 <dependencies> 49 <!--引入Spring Boot依賴--> 50 <dependency> 51 <groupId>org.springframework.boot</groupId> 52 <artifactId>spring-boot-starter</artifactId> 53 </dependency> 54 <!--引入lombok--> 55 <dependency> 56 <groupId>org.projectlombok</groupId> 57 <artifactId>lombok</artifactId> 58 <optional>true</optional> 59 </dependency> 60 <!--引入Spring Cloud的Eureka依賴--> 61 <dependency> 62 <groupId>org.springframework.cloud</groupId> 63 <artifactId>spring-cloud-starter-eureka-server</artifactId> 64 <version>${spring-cloud.version}</version> 65 </dependency> 66 </dependencies> 67 68 69 </project>
View Code

application.yml

#應用名稱
spring:
    application:
        name: EUREKA

#服務埠
server:
  port: 9090

#Eureka配置
eureka:
    client:
        service-url:
            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #eureka服務端配置
        register-with-eureka: false #不向eureka註冊自己
        fetch-registry: false #不向eurek獲取服務列表
    instance:
        hostname: 127.0.0.1

啟動Eureka