1. 程式人生 > >Sprng Cloud入門之Eureka(一)

Sprng Cloud入門之Eureka(一)

目錄

1.前言

2.介紹

3.搭建註冊中心

3.1 建立Spring Boot專案

3.2匯入依賴

3.3 application.yml 配置引數

3.4開啟註冊中心功能

4.實戰演練

4.1 user-api 專案部分程式碼(服務提供)

4.1.1新增依賴

4.1.2配置引數

4.1.3服務介面

4.1.4開啟服務註冊功能

4.2user-web 專案部分程式碼(服務消費)

4.2.1新增依賴

4.2.2配置引數

4.2.3客戶端

4.2.4開啟服務發現功能

原始碼地址


1.前言

Spring Cloud 是一系列框架的有序集合。它利用 Spring Boot 的開發便利性巧妙地簡化了分散式系統基礎設施的開發,如服務發現註冊、配置中心、訊息匯流排、負載均衡、斷路器、資料監控等,都可以用 Spring Boot 的開發風格做到一鍵啟動和部署。

2.介紹

Eureka 是 Netflix 的子模組,它是一個基於 REST 的服務,用於定位服務,以實現雲端中間層服務發現和故障轉移。

服務註冊和發現對於微服務架構而言,是非常重要的。有了服務發現和註冊,只需要使用服務的識別符號就可以訪問到服務,而不需要修改服務呼叫的配置檔案。該功能類似於 Dubbo 的註冊中心,比如 Zookeeper。

Eureka 採用了 CS 的設計架構。Eureka Server 作為服務註冊功能的服務端,它是服務註冊中心。而系統中其他微服務則使用 Eureka 的客戶端連線到 Eureka Server 並維持心跳連線。

其執行原理如下圖:

由圖可知,Eureka 的執行原理和 Dubbo 大同小異, Eureka 包含兩個元件: Eureka Server 和 Eureka Client。

Eureka Server 提供服務的註冊服務。各個服務節點啟動後會在 Eureka Server 中註冊服務,Eureka Server 中的服務登錄檔會儲存所有可用的服務節點資訊。

Eureka Client 是一個 Java 客戶端,用於簡化 Eureka Server 的互動,客戶端同時也具備一個內建的、使用輪詢負載演算法的負載均衡器。在應用啟動後,向 Eureka Server 傳送心跳(預設週期 30 秒)。如果 Eureka Server 在多個心跳週期內沒有接收到某個節點的心跳,Eureka Server 會從服務登錄檔中將該服務節點資訊移除。

3.搭建註冊中心

3.1 建立Spring Boot專案

3.2匯入依賴

(注意Spring Boot 與 SpringCloud 有版本相容關係,如果引用版本不對應,專案啟動會報錯)



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


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-parent</artifactId>
                <version>1.5.9.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
   

3.3 application.yml 配置引數

server:
  port: 9000

eureka:
  instance:
    hostname: localhost   # eureka 例項名稱
  client:
    register-with-eureka: false # 不向註冊中心註冊自己
    fetch-registry: false       # 是否檢索服務
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  # 註冊中心訪問地址

3.4開啟註冊中心功能

在啟動類上新增 @EnableEurekaServer 註解。

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

啟動專案,訪問http://localhost:9000/ ,可看到 Eureka 服務監控介面,如下:

 

4.實戰演練

瞭解 Eureka 的環境搭建後,我們需要進行實戰直觀的感受 Eureka 的真正作用,這樣才能清楚掌握和學習 Eureka 。我們再建立兩個 Spring Boot 專案,一個名為 user-api ,用於提供介面服務,另一個名為 user-web,用於呼叫 user-api 介面獲取資料與瀏覽器互動。

服務例項 描述
eureka 9000 註冊中心(Eureka 服務端)
user-api 8081 服務提供者(Eureka 客戶端)
user-web 80 服務消費者,與瀏覽器端互動(Eureka 客戶端)

4.1 user-api 專案部分程式碼(服務提供)

4.1.1新增依賴

同樣主要版本問題

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

        <!-- eureka 客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

4.1.2配置引數

server:
  port: 8081

spring:
  application:
    name: user-api

eureka:
  instance:
    instance-id: user-api-8081
    prefer-ip-address: true # 訪問路徑可以顯示 IP
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/  # 註冊中心訪問地址

注意:http://localhost:9000/eureka/ 就是註冊中心的地址。

4.1.3服務介面

public interface UserService {
    String getUserName();
}
@Service
public class UserServiceImpl implements UserService {

    @Override
    public String getUserName() {
        return "張三";
    }
}
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getname")
    public String get() {
        return this.userService.getUserName();
    }
}

注意:該 controller 是給 user-web 使用的(內部服務),不是給瀏覽器端呼叫的。

4.1.4開啟服務註冊功能

在啟動類上新增 @EnableEurekaClient 註解。

@EnableEurekaClient
@SpringBootApplication
public class UserApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(UserApiApplication.class, args);
    }

}

啟動專案完成後,瀏覽器訪問 http://localhost:9000 檢視 Eureka 服務監控介面 ,如下圖:

 

從圖可知,user 相關服務資訊已經註冊到 Eureka 服務中了。

補充:在上圖中,我們還看到一串紅色的字型,那是因為 Eureka 啟動了自我保護的機制。當 EurekaServer 在短時間內丟失過多客戶端時(可能發生了網路故障),EurekaServer 將進入自我保護模式。進入該模式後,EurekaServer 會保護服務登錄檔中的資訊不被刪除。當網路故障恢復後,EurekaServer 會自動退出自我保護模式。

 

4.2user-web 專案部分程式碼(服務消費)

4.2.1新增依賴

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

        <!-- eureka 客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.3.1.RELEASE</version>
        </dependency>

4.2.2配置引數

server:
  port: 80

spring:
  application:
    name: user-web

eureka:
  client:
    register-with-eureka: false # 不向註冊中心註冊自己
    fetch-registry: true        # 是否檢索服務
    service-url:
      defaultZone: http://localhost:9000/eureka/  # 註冊中心訪問地址

4.2.3客戶端

@Configuration
public class RestConfiguration {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient client;

    @RequestMapping("/getname")
    public User get() throws Exception {
        //getForObject() 傳送一個HTTP GET請求,返回的請求體將對映為一個物件
        //user-api 為呼叫的uri(服務提供者(Eureka 客戶端)地址)在eureka上註冊的application.name
        return restTemplate.getForObject( "http://user-api/user/getname",User.class);

    }

}

4.2.4開啟服務發現功能

在啟動類上新增 @EnableDiscoveryClient 註解。

@EnableDiscoveryClient
@SpringBootApplication
public class UserWebApplication {

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

}

啟動專案後,使用瀏覽器訪問 user-web 專案介面,執行結果如下:

資料來源:

user-api 8081 服務提供者(Eureka 客戶端)

原始碼地址

https://github.com/Uncle-LiuY/eureka-server

https://github.com/Uncle-LiuY/user-api

https://github.com/Uncle-LiuY/user-web

 

以上為本人實際測試寫下的,如有不足地方請指出。