SpringCloud Eureka服務註冊中心(一)

目錄直通車

一、簡介

二、配置與Eureka的使用

1、創建Eureka的Maven模塊

2、創建Eureka的Server(服務端)

3、創建Eureka的Client端並接入Server端

三、細節配置優化

1、修改該端口的微服務名稱

2、設置訪問信息有IP的提示

3、顯示該服務的一些描述信息

四、服務的發現

1、 新建一個Controller

2、 添加服務發現的註解

五、Eureka的集羣配置

1、修改啓動類名稱

2、POM

3、YML


一、簡介

1、在微服務架構中,註冊中心是核心的基礎服務之一。(有兩種方案實現註冊中心:第一是Dubbo+Zookeeper,第二是Spring Cloud+Eureka)

2、Eureka是一個服務註冊中心,在項目中的微服務需要註冊中心裏面註冊,用戶通過訪問註冊中心使用該微服務。

3、Euraka是CS架構的設計模式,它有兩大組件 Client 與 Server。流程如下:

二、配置與Eureka的使用

此處環境:JDK 1.8、Spring Boot 2.0.0.RELEASE、Spring Cloud Finchley.M9、Maven 5.4

1、創建Eureka的Maven模塊

2、創建Eureka的Server(服務端)

① POM添加下方三條依賴

     <!-- Eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>

        <!-- Eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
        </dependency>
        <!-- Eureka標配hystrix 熔斷器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

② yml配置

server:
  port: 7001
eureka:
  instance:
    #eureka服務端的實例名稱
    hostname: localhost
  client:
    #false表示不向註冊中心註冊自己。
    register-with-eureka: false
    #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址(單機)。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

③ 標註Server服務啓動類

最核心的是添加這個註解 @EnableEurekaServer

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

/**
 * created by DJun on 2019/6/22
 */
@SpringBootApplication
// 標註這是EurekaServer服務啓動類,接收其它微服務註冊進來
@EnableEurekaServer
public class EurekaServer7001_App {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServer7001_App.class,args);
    }
}

3、創建Eureka的Client端並接入Server端

① POM

eureka-client 是標註的版本的,本人測試的時候不標註版本,maven不會自動下載這個依賴包。

 <!-- 將微服務provider側註冊進eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>2.0.0.BUILD-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
            <version>2.0.0.M8</version>
        </dependency>
        <!-- spring-cloud-config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

② yml配置Eureka的客戶端

在yml的配置文件中添加下方的配置信息連接上端口爲7001的eureka服務端,這裏注意先啓動7001在啓動這裏的客戶端。

eureka:
  client:
    #客戶端註冊進eureka服務列表內
    service-url:
      defaultZone: http://localhost:7001/eureka/

③ 配置啓動類

在啓動類上新加一條@EnableEurekaClient的註解。

綜上,通過以上配置能夠實現如下效果,表示客戶端已經連接上了服務端了。

等了大概90秒之後,在一定時間內沒有接收到某個微服務實例的心跳,Eureka Server將會註銷該實例(默認90秒)Eureka會啓動自我保護機制,使用自我保護模式,可以讓Eureka集羣更加的健壯、穩定。情況如下:

在Spring Cloud中,可以使用 eureka.server.enable-self-preservation = false 禁用自我保護模式(不建議)。關於Eureka的保護的機制:推薦閱讀:http://www.itmuch.com/spring-cloud-sum/understanding-eureka-self-preservation/#more

注意:在某一時刻某一微服務若是不能使用了,eureka不會立即清理,依舊會對該微服務的信息進行保存。

 

三、細節配置優化

這裏完成三個功能,完整配置Eureka的POM內容如下:

1、修改該端口的微服務名稱

2、設置訪問信息有IP的提示

3、顯示該服務的一些描述信息

eureka:
  client:
    #客戶端註冊進eureka服務列表內
    service-url:
      defaultZone: http://localhost:7001/eureka/

     # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  instance:
    # 服務名稱修改
    instance-id: demo-provider-8001
    # 設置訪問信息有IP 信息提示
    prefer-ip-address: true   

# info是當前微服務的信息
info:
  app.name: microservice-demo
  company.name: com.djun.demo
  build.artifactId: $project.artifactId$
  build.version: $project.version$

在項目的父工程中的POM添加如下內容:

   <build>
        <finalName>micro-service-cloud</finalName>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <delimiters>
                        <!-- 以下方這個符號開頭與結尾中間的字符串爲信息 -->
                        <delimit>$</delimit>
                    </delimiters>
                </configuration>
            </plugin>
        </plugins>
    </build>

四、服務的發現

1、 新建一個Controller


import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * created by DJun on 2019/6/23
 */
@RestController
public class EurekaDiscoveryService {
    // 此處導包易錯,選擇最長的那個包
    @Resource
    private DiscoveryClient client;

    @GetMapping("/discovery")
    public Object discovery(){
        List<String> eurekaServiceList = client.getServices();
        System.out.println("eurekaServiceList: "+eurekaServiceList);

        List<ServiceInstance> srvList = client.getInstances("MICROSERVICE-DEMO");
        for (ServiceInstance element : srvList){
            System.out.println("ServiceId:"+element.getServiceId()+",HOST:"+element.getHost()
                    +",PORT:"+element.getPort()+",URI:"+element.getUri());
        }
        return this.client;
    }

}

2、 添加服務發現的註解

在啓動類上添加 @EnableDiscoveryClient

五、Eureka的集羣配置

所謂集羣就是避免一臺服務器宕機了以後,服務無法正常運行。我這裏就配置三臺,也比較具有代表性。

在第五章以上講述的是單機版的配置,下面就需要重新修改一下 POM 和 YML 文件。在進行以下操作之前,先添加以下內容在Windows下面的   C:\Windows\System32\drivers\etc 的hosts文件:

127.0.0.1      eureka7001.com
127.0.0.1      eureka7002.com
127.0.0.1      eureka7003.com

1、修改啓動類名稱

2、POM

三個Eureka的POM依賴均一致。

 <dependencies>
        <!-- Eureka-server -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- Eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-server</artifactId>
            <!--<version>2.2.0.BUILD-SNAPSHOT</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <!-- 修改後立即生效,熱部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

3、YML

在這裏面,如7001的機子要連接7002和7003,7002的機子要連接7001和7003.......以此類推。這裏也就配置一臺機子的YML,其餘只用修改一些端口號和連接的機子就可以了。

server:
  port: 7001
eureka:
  instance:
    # eureka服務端的實例名稱
    # hostname: localhost
    # 完成集羣配置,在hosts修改映射文件
    hostname: eureka7001.com

  client:
    #false表示不向註冊中心註冊自己。
    register-with-eureka: false
    #false表示自己端就是註冊中心,我的職責就是維護服務實例,並不需要去檢索服務
    fetch-registry: false
    service-url:
      #設置與Eureka Server交互的地址查詢服務和註冊服務都需要依賴這個地址(單機版)。
      #defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

      # 7001裏面配置7002 和 7003
     defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

配置成功的效果如下:

最後,感謝您的閱讀  :)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章