1. 程式人生 > >基於spring-cloud實現eureka註冊服務小案例

基於spring-cloud實現eureka註冊服務小案例

首先建立兩個專案,eureka-service和eureka-client。
eureka-server作為eureka的服務端,提供註冊服務,eureka-client作為eureka的客戶端,屬於一個應用,註冊到eureka註冊中心。

eureka-service的配置檔案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> <groupId>com.example</groupId> <artifactId>eureka-service</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging
>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <!--配合spring cloud版本--> <relativePath/> <!-- lookup parent from repository -->
</parent> <properties> <!--設定字元編碼及java版本--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--增加eureka-server的依賴--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <!--用於測試的,本例可省略--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <!--依賴管理,用於管理spring-cloud的依賴--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Brixton.SR3</version> <!--官網為Angel.SR4版本,但是我使用的時候總是報錯--> <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>

eureka-service專案中建立包,自定,本例為:com.spring
在該包下建立類用於啟動服務EurekaServiceApplication.java

package com.spring;

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

@EnableEurekaServer         //開啟eureka服務
@SpringBootApplication      //springBoot註解,spring在springBoot基礎之上來構建專案
public class EurekaServiceApplication {

    //spirng boot的標準入口
    public static void main(String[] args) {
        SpringApplication.run(EurekaServiceApplication.class, args);
    }
}

關於eureka-service的配置檔案 applicaiton.yml 或者使用application.properties,兩者格式不同。
application.yml

server:
  port: 8761     #指定服務埠
eureka:
  client:
    registerWithEureka: false  #是否將eureka自身作為應用註冊到eureka註冊中心
    fetchRegistry: false       #為true時,可以啟動,但報異常:Cannot execute request on any known server

簡單的eureka-service就寫好了,執行下EurekaServiceApplication.java
訪問:http://localhost:8761/

關於eureks-client 的pom.xml檔案如下,因為不提供其他服務,這裡基本與eureka-service一樣:

<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>
  <groupId>com.huawei.it</groupId>
  <artifactId>eureka-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath/>
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Brixton.SR3</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
      <!--此處是差異所在-->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-eureka</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
      </dependency>
  </dependencies>

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

</project>

同樣建立入口類EurekaClientApplication.java

package com.spring;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Administrator
 *
 */
@EnableDiscoveryClient            //通過該註解,實現服務發現,註冊
@SpringBootApplication
public class EurekaClientApplication {

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

}

@RestController
class ServiceInstanceRestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping("/service-instances/{applicationName}")
    public List<ServiceInstance> serviceInstancesByApplicationName(
            @PathVariable String applicationName) {
        return this.discoveryClient.getInstances(applicationName);
    }

    @RequestMapping("/")
    public String sayhello() {
        return "hello";
    }
}

關於eureka-client的配置檔案bootstrap.yml和application.yml,
bootstrap.yml先於application.yml載入,一般不變的東西解除安裝bootstrap裡,使用一樣:
application.yml

server:
  port: 7070
spring:
  application:
    name: cloud-client    #為你的應用起個名字,該名字將註冊到eureka註冊中心

好了,現在啟動eureka-client,再次訪問http://localhost:8761,可能需要等一會
eureka註冊中心

這裡因為兩個都部署在同一臺機器上,所以可以直接發現服務,並註冊,如果在不同的機器上不部署,application.yml配置中需要加上eureka的地址如,多個eureka服務地址,可以用,號隔開:

eureka:
  client:
    serviceUrl:
      defaultZone: http://${eureka-serviceIP}:8761/eureka/