SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版

一、spring cloud簡介
鑑於《史上最簡單的Spring Cloud教程》很受讀者歡迎再次我特意升級了一下版本目前支持的版本爲Spring Boot版本2.0.3.RELEASE,Spring Cloud版本爲Finchley.RELEASE。

Finchley版本的官方文檔如下
http://cloud.spring.io/spring-cloud-static/Finchley.RELEASE/single/spring-cloud.html

spring cloud 爲開發人員提供了快速構建分佈式系統的一些工具包括配置管理、服務發現、斷路器、路由、微代理、事件總線、全局鎖、決策競選、分佈式會話等等。它運行環境簡單可以在開發人員的電腦上跑。另外說明spring cloud是基於springboot的所以需要開發中對springboot有一定的瞭解如果不瞭解的話可以看這篇文章2小時學會springboot。另外對於“微服務架構” 不瞭解的話可以通過搜索引擎搜索“微服務架構”瞭解下。

二、創建服務註冊中心
在這裏我還是採用Eureka作爲服務註冊與發現的組件至於Consul 之後會出文章詳細介紹。

2.1 首先創建一個maven主工程。

首先創建一個主Maven工程在其pom文件引入依賴spring Boot版本爲2.0.3.RELEASESpring Cloud版本爲Finchley.RELEASE。瞭解springcloud架構可以加求求三五三六二四七二五九這個pom文件作爲父pom文件起到依賴版本控制的作用其他module工程繼承該pom。這一系列文章全部採用這種模式其他文章的pom跟這個pom一樣。再次說明一下以後不再重複引入。代碼如下

<?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.forezp</groupId>
    <artifactId>sc-f-chapter1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>sc-f-chapter1</name>
    <description>Demo project for Spring Boot</description>

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

    <modules>
        <module>eureka-server</module>
        <module>service-hi</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
    </properties>

    <dependencies>
        <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>

2.2 然後創建2個model工程:**一個model工程作爲服務註冊中心即Eureka Server,另一個作爲Eureka Client。

下面以創建server爲例子詳細說明創建過程

右鍵工程->創建model-> 選擇spring initialir 如下圖SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版
下一步->選擇cloud discovery->eureka server ,然後一直下一步就行了。
SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版
創建完後的工程其pom.xml繼承了父pom文件並引入spring-cloud-starter-netflix-eureka-server的依賴代碼如下

<?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.forezp</groupId>
    <artifactId>eureka-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

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

</project>

2.3 啓動一個服務註冊中心只需要一個註解@EnableEurekaServer這個註解需要在springboot工程的啓動application類上加

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

2.4 eureka是一個高可用的組件它沒有後端緩存每一個實例註冊之後需要向註冊中心發送心跳因此可以在內存中完成在默認情況下erureka server也是一個eureka client ,必須要指定一個 server。eureka server的配置文件appication.yml

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: eurka-server

通過eureka.client.registerWithEurekafalse和fetchRegistryfalse來表明自己是一個eureka server.

2.5 eureka server 是有界面的啓動工程,打開瀏覽器訪問
http://localhost:8761 ,界面如下
SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版
No application available 沒有服務被發現 ……_
因爲沒有註冊服務當然不可能有服務被發現了。

三、創建一個服務提供者 (eureka client)
當client向server註冊時它會提供一些元數據例如主機和端口URL主頁等。Eureka server 從每個client實例接收心跳消息。 如果心跳超時則通常將該實例從註冊server中刪除。

創建過程同server類似,創建完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.forezp</groupId>
    <artifactId>service-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>service-hi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.forezp</groupId>
        <artifactId>sc-f-chapter1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <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>
    </dependencies>

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

</project>

通過註解@EnableEurekaClient 表明自己是一個eurekaclient.
@SpringBootApplication
@EnableEurekaClient
@RestController
public class ServiceHiApplication {

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

    @Value("${server.port}")
    String port;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "forezp") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }

}

僅僅@EnableEurekaClient是不夠的還需要在配置文件中註明自己的服務註冊中心的地址application.yml配置文件如下

server:
  port: 8762

spring:
  application:
    name: service-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

需要指明spring.application.name,這個很重要這在以後的服務與服務之間相互調用一般都是根據這個name 。
啓動工程打開http://localhost:8761 即eureka server 的網址
SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版
SpringCloud分佈式微服務雲架構 第一篇: 服務的註冊與發現Eureka(Finchley版
你會發現一個服務已經註冊在服務中了服務名爲SERVICE-HI ,端口爲7862

這時打開 http://localhost:8762/hi?name=forezp 你會在瀏覽器上看到 :

hi forezp,i am from port:8762

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