Spring Cloud:服務註冊與發現(Eureka)【Dalston版】

Spring Cloud簡介

Spring Cloud是一個基於Spring Boot實現的雲應用開發工具,它爲基於JVM的雲應用開發中涉及的配置管理、服務發現、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分佈式會話和集羣狀態管理等操做提供了一種簡單的開發方式。web

Spring Cloud包含了多個子項目(針對分佈式系統中涉及的多個不一樣開源產品),好比:Spring Cloud Config、Spring Cloud Netflix、Spring Cloud0 CloudFoundry、Spring Cloud AWS、Spring Cloud Security、Spring Cloud Commons、Spring Cloud Zookeeper、Spring Cloud CLI等項目。spring

建立「服務註冊中心」

建立一個基礎的Spring Boot工程,命名爲eureka-server,並在pom.xml中引入須要的依賴內容:apache

<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>SpringCloud</groupId>
  <artifactId>eurekaService</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/>
</parent>

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

<dependencyManagement>
    <dependencies>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Dalston.SR1</version>
           <type>pom</type>
           <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  
  
  
<profiles>
    <profile>
        <id>jdk-1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
  </profiles>
</project>

經過@EnableEurekaServer註解啓動一個服務註冊中心提供給其餘應用進行對話springboot

@EnableEurekaServer
@SpringBootApplication
public class ApplicationServer {
    public static void main(String[] args) {
        //SpringApplication.run(source, args);
        new SpringApplicationBuilder(ApplicationServer.class)
        .web(true).run(args);
    }
}app

在默認設置下,該服務註冊中心也會將本身做爲客戶端來嘗試註冊它本身,因此咱們須要禁用它的客戶端註冊行爲,只須要在application.properties配置文件中增長以下信息:maven

spring.application.name=eureka-server
server.port=1001
# 實例的主機名稱
eureka.instance.hostname=localhost
## 不要向註冊中心註冊本身
eureka.client.register-with-eureka=false
## 表示不去檢索其餘的服務,由於服務註冊中心自己的職責就是維護服務實例,它也不須要去檢索其餘服務
eureka.client.fetch-registry=false
 #指定服務註冊中心地址 這裏直接指向了本服務
#eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/分佈式

啓動項目,就能夠訪問:http://localhost:1001/看看是否能發現服務spring-boot

建立服務提供方工具

首先,建立一個基本的Spring Boot應用。命名爲eureka-client,在pom.xml中,加入以下配置:開發工具

<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>springboot</groupId>
  <artifactId>eureka-client</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  
  <parent> 
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

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

<dependencyManagement>
    <dependencies>
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-dependencies</artifactId>
           <version>Dalston.SR1</version>
           <type>pom</type>
           <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<profiles>
    <profile>
        <id>jdk-1.8</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <jdk>1.8</jdk>
        </activation>
        <properties>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
        </properties>
    </profile>
  </profiles>
</project>

其次,實現/dc請求處理接口,經過DiscoveryClient對象,在日誌中打印出服務實例的相關內容

package com.client.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class DcController {
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/dc")
    public String dc() {
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return services;
    }
}
最後在應用主類中經過加上@EnableDiscoveryClient註解,該註解能激活Eureka中的DiscoveryClient實現,這樣才能實現Controller中對服務信息的輸出。

package com.client;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

//注意這裏也可以使用@EnableEurekaClient
//但因爲springcloud是靈活的,註冊中心支持eureka、consul、zookeeper等
//若寫了具體的註冊中心註解,則當替換成其餘註冊中心時,又須要替換成對應的註解了。
//因此 直接使用@EnableDiscoveryClient 啓動發現。
//這樣在替換註冊中心時,只須要替換相關依賴便可
@EnableDiscoveryClient
@SpringBootApplication
public class ApplicationClient {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ApplicationClient.class).web(true).run(args);
    }
}
application.properties作一些配置工做,具體以下:

spring.application.name=eureka-client
server.port=2001
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
啓動該工程後,再次訪問:http://localhost:1001/,咱們定義的服務被成功註冊了。

們也能夠經過直接訪問eureka-client服務提供的/dc接口來獲取當前的服務清單,只須要訪問:http://localhost:2001/dc,咱們能夠獲得以下輸出返回 Services: [eureka-client]

相關文章
相關標籤/搜索