最近發現SpringCloud構建微服務架構中,網上不少只是用到了SpringBoot2.x以前的版本,顯然使用SpringBoot2.x以後構建,網上的資料會給初學者帶來不少不方便,並且沒有多大的參考價值,因此,這裏將使用SpringBoot2.0.0版本,構建SpringCloud Eureka服務治理。 java
服務治理分了兩部分:註冊中心和服務提供者web
工具環境:IntelliJ IDEAspring
1、搭建註冊中心架構
一、打開IDEA,File->new->Project->maven...app
如上圖所示,這一步很重要,由於建立maven項目能夠有不少種方式,若是構建簡單的項目,能夠選擇快速maven,可是SpringCloud Eureka確定必需要選擇那個webapp項目,否則的話,結果出來會沒法正常訪問Spring Eureka頁面。建立項目的後續操做這裏不詳細述說,相信不少玩過maven項目的都會。webapp
二、配置pom文件,導入相關包maven
<!-- 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>
上面的信息是主要的依賴導入,這裏特別須要指出幾個注意點:spring-boot
①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
三、配置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/
四、配置啓動類
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端口管理。
2、搭建服務提供者
步驟跟搭建註冊中心如出一轍,能夠直接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的註冊中心和服務提供者的操做步驟與注意點,但願對於各位讀者有幫助。