【微服務】之二:從零開始,輕松搞定SpringCloud微服務系列--注冊中心(一)


微服務體系,有效解決項目龐大、互相依賴的問題。目前SpringCloud體系有強大的一整套針對微服務的解決方案。本文中,重點對微服務體系中的服務發現注冊中心進行詳細說明。本篇中的注冊中心,采用Netflix 公司的Eureka。

本系列教程列表:
【微服務】之一:從零開始,輕松搞定SpringCloud微服務系列--開山篇(spring boot 小demo)
【微服務】之二:從零開始,輕松搞定SpringCloud微服務系列--注冊中心(一)

注冊中心簡介

Netflix Eureka:雲端負載均衡,一個基於 REST 的服務,用於定位服務,以實現雲端的負載均衡和中間層服務器的故障轉移。他包含很多功能,本文重點講解它的服務注冊中心。

官方解釋
Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier. The client also has a built-in load balancer that does basic round-robin load balancing.

Eureka支持服務動態擴容、縮容、失效剔除。

Eureka提供了完整的Service Registry和Service Discovery實現,與現有Spring Cloud完美融合。

注冊中心服務原理

enter image description here

由上圖可以看出,藍色部分為注冊中心,黃色部分為一個生產者、消費者。所有應用都被同一個注冊中心納入管理。通常有Register(服務注冊)、Renew(服務續約)、Cancel(服務下線)等操作。

環境清單

JDK: 1.8
Maven:3.x+
IDE:idea

開始起飛

為了后續博文的開展,我們約定所有子系統都放置在一個父類項目下,采用Idea的模塊開發機制,對所有子系統進行統一倉庫管理,方便交流學習。

創建父類項目

使用idea創建一個maven項目(創建之前設置好maven、jdk版本)。
然后在pom.xml文件中加入以下超級父類依賴。

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

創建子模塊

模塊開發

通過上面箭頭提示的Module創建模塊項目,流程與新建項目類似。
創建完成以后在子項目pom文件中加入以下依賴。

配置pom文件

<!--依賴管理-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <!--資源庫管理-->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/libs-snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>
    <!--依賴管理中心-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--構建中心-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- defined in spring-cloud-starter-parent pom (as documentation hint),
                    but needs to be repeated here -->
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-core</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-client</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>
        </plugins>
    </build>

設置完成以后,開始主體設置,創建包>>創建主方法>>配置yml文件
項目目錄結構
文件目錄

新建Application.java文件


/**
 * @Description : Eureka 服務發現server 啟動類
 * @Author hanyahong
 * @Date 2017/12/4- 16:14
 */
@SpringBootApplication
@EnableEurekaServer
public class Application {

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

}

說明:
@EnableEurekaServer :服務發現服務端注解,設置以后將作為服務注冊中心。

@SpringBootApplication:springboot啟動主程序注解。

配置application.yml文件

在位於resources的文件夾下面創建該文件,該文件作為服務的配置文件,可以配置相關子項目參數。

#server 端口號設置
server:
  port: 8081

#注冊中心設置,server本身不被發現
eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0

至此,一個完整的服務注冊中心基本搭建完畢。可以進行啟動測試。

啟動程序

驗證是否成功

我們可以在Terminal看到以下信息,說明啟動成功。


 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.9.RELEASE)
...
2017-12-04 18:04:30.780  INFO 32476 --- [      Thread-11] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
2017-12-04 18:04:30.823  INFO 32476 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8081 (http)
2017-12-04 18:04:30.825  INFO 32476 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8081
2017-12-04 18:04:30.830  INFO 32476 --- [           main] com.hyh.Application                      : Started Application in 14.784 seconds (JVM running for 15.647)
2017-12-04 18:05:30.759  INFO 32476 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms


通過瀏覽器訪問 http://localhost:8081/ 可以看到已經啟動正常。
image.png

使用Maven 插件進行打包

提醒:在打包之前需要本地已經安裝好Git/Maven並配置好環境變量。博主因為不喜歡使用Idea自帶的JDK/MAVEN/GIT,都是單獨安裝的。這個看個人喜好吧。
另外,博主喜歡使用命令進行驅動打包,因此對Idea的Terminal進行了重新配置,將Win下面CMD替換成了Git.bash,如果有同學感興趣可以自行體驗。

IDEA 默認Terminal 修改

打開 File>>Settings,找到修改配置選項
Settings
然后將默認的Shell地址改成Git.
配置默認Shell

設置完成以后進行保存。回到我們界面可以通過Alt + F12 快速打開shell窗口。
shell窗口

正式開始打包

首先使用pwd 查看所在的目錄,應該是父類項目的根目錄,ls 命令或者dir 查看,該目錄下面的文件與文件夾,我們需要跳轉到子目錄下面,進行子項目(服務注冊中心)打包。所以使用 cd 命令跳轉子目錄
使用命令mvn clean package 就可以實現maven打包。可能會先下載一些依賴,完了就會開始打包操作。

跳轉打包目錄

Maven打包

mvn clean package

執行命令后出發maven構建,如下。

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building cloud-hyh-discovery-eureka V1.0.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.6.1:clean (default-clean) @ cloud-hyh-discovery-eureka ---
[INFO] Deleting E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ cloud-hyh-discovery-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ cloud-hyh-discovery-eureka ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ cloud-hyh-discovery-eureka ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ cloud-hyh-discovery-eureka ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.18.1:test (default-test) @ cloud-hyh-discovery-eureka ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ cloud-hyh-discovery-eureka ---
[INFO] Building jar: E:\workplace\spring-cloud-microservice\cloud-hyh-discovery-eureka\target\cloud-hyh-discovery-eureka-V1.0.0.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.9.RELEASE:repackage (default) @ cloud-hyh-discovery-eureka ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.984 s
[INFO] Finished at: 2017-12-04T19:24:35+08:00
[INFO] Final Memory: 37M/326M
[INFO] ------------------------------------------------------------------------

至此,一個完整的服務注冊中心,我們就開發並打包完畢。可以通過**target ** 文件夾找到剛剛生成的jar文件。進行獨立啟動了。
image.png

使用命令可以啟動獨立的jar保程序。

java -jar XXX.jar

Linux環境下,后台啟動命令

nohup java -jar xxx.jar &

備注: Linux查看端口對於的啟動程序命令,XXXX代表端口號

netstat -anp|grep XXXX

通過以上命令查到程序對於的信息(包括PID)。如果想停止,可以通過PID(進程ID)殺死相關進程。

kill -9 PID

博客源碼地址

Github地址:https://github.com/hanyahong/spring-cloud-microservice

碼雲地址:https://gitee.com/hyhvpn/spring-cloud-microservice


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM