1. 程式人生 > >springcloud微服務架構之搭建註冊中心Eureka與註冊中心的互相註冊

springcloud微服務架構之搭建註冊中心Eureka與註冊中心的互相註冊

前言
    springboot作為當下最流行的微服務框架,並且提供了程式碼的執行環境。然而springcloud是一套微服務管理框架,提供了服務的註冊與發現,負載均衡等元件。這兩個框架結合起來會很容易開發出一套微服務系統。
    註冊中心的互相發現可以有效的解決當一個註冊中心掛掉以後整個服務就丟失服務的狀態,當一個註冊中心掛掉後,還會有一個相同的服務繼續工作。

    這篇部落格主要說一下如何自己搭建一個註冊中心與互相註冊。

前期準備

    IDEA編輯器

    JDK1.8

搭建過程
    1.首先在開發工具中建立一個maven根專案,File-New-Project-Maven,選擇Project SDK 中的1.8,然後點選下一步。


    2.輸入包名GroupId、專案唯一標識ArtifactId、以及版本號Version,點選下一步。    3.輸入專案名,一般和ArtifactId保持一致,然後選擇專案路徑,點選下一步,建立成功。    4.然後在pom檔案中新增父檔案的依賴:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
    5.然後點選專案右鍵,New-Module,重複剛才的步驟,建立一個Eureka子專案,來作為註冊中心。在新建立專案的pom檔案中新增依賴:
    <!--依賴管理-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!--資源庫管理-->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <!--依賴管理中心-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--構建中心-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- defined in spring-cloud-starter-parent pom (as documentation hint),
                    but needs to be repeated here -->
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-core</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-client</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>
    6.在專案中建立包架構,然後建立啟動類Application.java。需要在啟動類上加上@SpringBootApplication(springboot的啟動類)和@EnableEurekaServer(對外提供註冊與發現服務)
    7.在resources中建立一個配置檔案application.yml、application-8000.yml、application-8001.yml,用來配置註冊中心的地址與埠號
application.yml:
#配置檔案管理
spring:
  application:
    #配置專案名,會在註冊中心application中顯示
    name: eureka
  profiles:
    #配置使用的配置檔案
    active: 8000
application-8000.yml:
#server 埠號設定
server:
  port: 8000

#註冊中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
application-8001.yml:
#server 埠號設定
server:
  port: 8001

#註冊中心地址
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8000/eureka/

    8.這裡用兩個配置檔案分別寫不同的埠號,然後註冊到另一個埠號的註冊中心裡,實現了註冊中心的互相註冊。使用application.yml控制使用的配置檔案,減少更改配置檔案次數。
    9.分別使用兩個配置檔案啟動兩次服務,然後訪問其中的一個埠號,比如:http://localhost:8000/,若看到如下頁面則說明註冊中心搭建成功,在Status中看到同一個機器的兩個埠號。