SpringCloud入門學習(服務註冊中心與服務篇)
一、服務註冊中心的創建
- 首先創建一個空的maven工程,刪掉src目錄,具體過程我就不演示了,相信大家都會。如下:
- 前面也講過,SpringCloud是基於SpringBoot的。所以,接下來創建一個SpringBoot模塊:
然後選擇Spring Initializr
,接着依賴選擇:
- 注意修改SpringCloud版本,本文用的是
Finchley.RELEASE
,請和我保持版本一致,避免因爲版本問題而導致項目無法啓動。以下貼出我的registrycentry(服務註冊中心)
模塊的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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>registrycentry</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>registrycentry</name>
<description>Demo project for Spring Boot</description>
<properties>
<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-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
-
如果是第一次創建該工程,依賴加載會有點費時,耐心等待就行
創建並啓動一個服務註冊中心非常簡單,只需按照以下步驟操作即可:
4.1 啓動類添加註解@EnableEurekaServer
package com.example.registrycentry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableEurekaServer //表示我是一個服務註冊中心,任何服務都需要到服務註冊中心進行註冊 public class RegistrycentryApplication { public static void main(String[] args) { SpringApplication.run(RegistrycentryApplication.class, args); } }
4.2 配置文件進行如下配置:
server port:8761 #指定服務端口號 eureka: client: register-with-eureka: false #是否需要將自己註冊到註冊中心,因爲該工程自己就是服務註冊中心,所以無需註冊。如果是多個服務註冊中心集羣模式,則另當別論 fetch-registry: false #是否向註冊中心定時更新自己狀態
ok,啓動項目,然後訪問
localhost:8761
可以看到,此時並沒有服務進行註冊。所以,接着,讓我們創建幾個服務吧!
二、服務創建並註冊
-
同樣的
new -> module -> Spring Initializr -> 依賴選擇
如下:
再次強調,記得修改pom文件<spring-cloud.version>
修改前:<properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR1</spring-cloud.version> </properties>
修改後:
<properties> <java.version>1.8</java.version> <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> </properties>
之後的每個服務也要記得修改!!!
-
ok,創建一個服務,並將服務註冊到服務註冊中心,如下步驟操作即可:
2.1 啓動類添加註解@EnableEurekaClient
package com.example.microservice01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableEurekaClient //表示我是一個服務,任何服務都是服務註冊者也是服務調用者,都需要註冊到服務註冊中心 public class Microservice01Application { public static void main(String[] args) { SpringApplication.run(Microservice01Application.class, args); } }
2.2 配置文件進行如下配置:
server: port: 8081 #指定服務端口號 registrycentery: 8761 #服務註冊中心端口號 spring: application: name: MicroService01 #服務名,服務名是唯一的,不同的服務名字不能重複 eureka: client: register-with-eureka: true #是否需要將自己註冊到註冊中心 fetch-registry: true #是否向註冊中心定時更新自己狀態 service-url: default-zone: http://localhost:${server.registrycentery}}/eureka/ #指定去哪個服務註冊中心進行註冊
啓動項目,再去訪問
localhost:8761
就會發現此時,已經有一個服務註冊上去了,服務名爲MICROSERVICE01
, 默認會將名字顯示爲全部大寫。 -
接着,繼續創建幾個服務,步驟和上面是一樣的,我就不再重複演示了。
並在此基礎上,分別寫一個簡單的接口調用。啓動服務,再次打開註冊中心localhost:8761
可以看到我啓動了3個服務:
SpringCloud服務註冊中心與服務篇就到這裏就結束啦 -> 本篇源碼
下一篇我將介紹 -> SpringCloud整合Feign
實現服務之間的調用