AdSense

網頁

2019/6/21

Spring Cloud 建立 Spring Cloud Netflix Eureka Client

本篇介紹如何使用Spring Boot建立Spring Cloud NetFlix Eureka Client

請先參考Spring Cloud Netflix Eureka簡介

本範例使用的IDE為Eclipse版本為2019-03

Spring Cloud版本為Greenwich.SR1 (Service Release1)


首先用Eclipse STS工具建立一個Spring Boot Starter專案



Project Dependencies選擇Eureka Discovery ClientSpring (Boot) Web Starter

如果沒加入Spring Web Starter,則就只是一個普通的Spring Boot應用程式而已,啟動執行完就會自動關閉,所以必須加上Spring Web Starter的dependency。



建立好的專案目錄結構如下。



如果專案的pom.xml出現未知的錯誤,請參考Eclipse Maven pom.xml Maven Configuration Problem: Unknown來解決。


專案的pom.xml內容如下。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.abc</groupId>
    <artifactId>spring-cloud-eureka-client-demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-client-demo</name>
    <description>Eureka Client Demo</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


開啟SpringBootApplcation類別,也就是本範例的SpringCloudEurekaClientDemoApplication,然後在類別前加上@EnableDiscoveryClient

SpringCloudEurekaClientDemoApplication

package com.abc.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class SpringCloudEurekaClientDemoApplication {

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

}

@EnableDiscoveryClient的作用是啟用服務發現及註冊,也就是將此應用程式註冊到Eureka Server(discovery-server)中。


然後在專案的預設配置檔application.properties要設定以下。
本範例改以yml格式來設定,也就是application.yml

application.yml

# Spring properties
spring:
  application:
     name: eureka-client-service #服務名稱
# Discovery Server Access
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka/ #Eureka Server的位址
# HTTP Server
server:
  port: 2222   # HTTP (Tomcat) port #服務的埠號

spring.application.name設定服務的名稱。
eureka.client.service-url.default-zone指定Eureka Server的位址。
server.port設定服務的port號。


到此便完成了一個Eureka Client服務的設定。

在啟動Eureka Client前,先建立 Spring Cloud Netflix Eureka Server並啟動Eureka Server,然後再啟動Eureka Client。

如果本篇有幫助到您,幫忙點一下廣告支持,感恩。

如果Eureka Server沒有啟動或位址設定錯誤,會發生如下的連線錯誤訊息,也就是找不到指定的Eureka Server位址。

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.8.jar:1.9.8]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.8.jar:1.9.8]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) ~[eureka-client-1.9.8.jar:1.9.8]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.8.jar:1.9.8]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) ~[eureka-client-1.9.8.jar:1.9.8]


沒有留言:

AdSense