1. 程式人生 > >SpringBoot2.0+SpringCloud Eureka構建服務治理

SpringBoot2.0+SpringCloud Eureka構建服務治理

actor -s 功能 pac 很多 bin -c util 出現

最近發現SpringCloud構建微服務架構中,網上很多只是用到了SpringBoot2.x之前的版本,顯然使用SpringBoot2.x之後構建,網上的資料會給初學者帶來很多不方便,而且沒有多大的參考價值,所以,這裏將使用SpringBoot2.0.0版本,構建SpringCloud Eureka服務治理。

服務治理分了兩部分:註冊中心和服務提供者

工具環境:IntelliJ IDEA

一、搭建註冊中心

1、打開IDEA,File->new->Project->maven...

技術分享圖片

如上圖所示,這一步很重要,因為創建maven項目可以有很多種方式,如果構建簡單的項目,可以選擇快速maven,但是SpringCloud Eureka肯定必須要選擇那個webapp項目,不然的話,結果出來會無法正常訪問Spring Eureka頁面。創建項目的後續操作這裏不詳細述說,相信很多玩過maven項目的都會。

2、配置pom文件,導入相關包

<!-- SpringBoot 2.0.0 依賴 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/>
</parent>

<!-- JUnit測試依賴 -->
<dependencies
> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <!-- 添加spring-boot-starter-web模塊依賴 --> <dependency> <groupId>org.springframework.boot</groupId
> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- spring cloud 配置依賴, 這個可以先不導入 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- SpringCloud Eureka依賴 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <!-- Spring Cloud 管理依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

上面的信息是主要的依賴導入,這裏特別需要指出幾個註意點:

①eureka-server包

技術分享圖片

對於SpringBoot2.0.0,是使用spring-cloud-starter-netflix-eureka-server;對於一些低版本的SpringBoot,是使用spring-cloud-starter-eureka-server;如果使用版本不匹配,就會無法導入@EnableEurekaServer。

②SpringCloud集中管理版本

技術分享圖片

對於SpringBoot2.x版本,SpringCloud應該使用Finchley版本,SpringCloud的版本命名是根據英國街道名字,具體可以百科一下,下面給出不同SpringBoot版本對應的SpringCloud版本代號:

技術分享圖片

詳細可以參考:https://blog.csdn.net/54powerman/article/details/79163440

3、配置application.properties文件(有些人喜歡用yaml,這裏使用.properties)

完整application.properties配置如下:

server.port=1111
eureka.instance.hostname=localhost
spring.application.name=hello-service1
#由於該應用是註冊中心,false:代表不向註冊中心註冊自己;true:代表註冊自己
eureka.client.register-with-eureka=false
#是否啟動檢測服務,由於註冊中心的職責是維護服務實例,所以它不需要檢服務
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

4、配置啟動類

package com.cjs;

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

/**
* Hello world!
*
*/
@SpringBootApplication
@EnableEurekaServer
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

@EnableEurekaServer表示,此項目作為Eureka服務中心

最後,啟動項目,在網頁上輸入:http://127.0.0.1:1111,就會跳到Spring Eureka的界面,如下圖:

技術分享圖片

技術分享圖片

出現上面圖片,證明搭建註冊中心成功。註意紅色框,顯示沒有“實例”(“服務”)可用,當然了,首先,properties文件已經關掉服務中心自己註冊自己的功能,其次,沒有其他服務提供者使用1111端口管理。

二、搭建服務提供者

步驟跟搭建註冊中心一模一樣,可以直接copy過去,需要修改幾點位置:

①properties文件

#設置服務提供者名字
spring.application.name=hello-server-index

eureka.instance.hostname=localhost

eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:1111/eureka/

②啟動類文件

package com.cjs;

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

@SpringBootApplication
@EnableDiscoveryClient
public class App {
    public static void main(String[] args){
        System.out.println("啟動————提供服務者");
        SpringApplication.run(App.class, args);
    }
}

在註冊中心是使用@EnableEurekaServer,而在服務提供者這裏,要使用@EnableDiscoveryClient。

最後,在啟動註冊中心的基礎上,再將此項目也啟動一下,再次訪問http:127.0.0.1:1111,這是就會發現:

技術分享圖片

這裏可以看到剛才創建的服務提供者的名字。

接下來,再服務提供者項目裏創建一個簡單的Controller,用來測試,

package com.cjs.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.logging.Logger;

@Controller
public class HelloController {
    @Autowired
    private DiscoveryClient client;

    @RequestMapping("/index")
    @ResponseBody
    public String index() {
        return "hello World";
    }
}    

訪問http://127.0.0.1:8080/index(註意:這裏端口為8080),會出現下面效果:

技術分享圖片

以上就是簡單搭建Eureka的註冊中心和服務提供者的操作步驟與註意點,希望對於各位讀者有幫助。

SpringBoot2.0+SpringCloud Eureka構建服務治理