天天看點

SpringCloud(2):注冊中心Eureka的搭建

SpringCloud(2):注冊中心Eureka的搭建

Eureka

Eureka:服務的注冊與發現

前面說了很多微服務的概念,其實最大的思想就是實作了項目/業務拆分

很多熟悉Dubbo的朋友應該已經對SOA的思想非常的清晰了,這裡我也就不再贅述。

說起Eueka肯定很多人就會第一時間想起Zookeeper,确實在Dubbo和SpringCloud這兩種SOA架構中的都是擔任注冊中心這樣的角色,可以說實作的能是大相徑庭,現在我們來快速上手搭建Eureka中心。

項目中注冊中心的搭建

IDEA項目搭建圖解

SpringCloud(2):注冊中心Eureka的搭建

建立SpringBoot項目

SpringCloud(2):注冊中心Eureka的搭建

核心坐标

核心坐标:

<!--eureka server 注冊中心-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
           

項目分析:

分布式的項目肯定是每個項目有都會用到一些相同的坐标 而我們是采用Maven來管理項目的

<parent>
        <groupId>**.**</groupId>
        <artifactId>****</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath> <!-- 引入父級pom坐标 -->
    </parent>
           

我們再來看看父級pom.xml

<modules>
    <module>eureka-server</module>
    <module>eureka-producer</module>
    <module>eureka-consumer-feign</module>
    <module>commons</module>
    <module>monitor</module>
</modules>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<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.SR1</spring-cloud.version>
</properties>

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

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--springboot-data-redis-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!--fastJson-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.15</version>
    </dependency>

    <!--long3-->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.4</version>
    </dependency>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>

</dependencies>
           

Eureka的配置檔案 application.properties

#端口号
server.port=10000

#注冊中心服務ID
spring.application.name=eureka-server

#eureka.client.registerWithEureka :表示是否将自己注冊到Eureka Server,預設為true
# 由于目前這個應用就是Eureka Server,故而設為false
eureka.client.register-with-eureka=false

#表示是否從Eureka Server擷取注冊資訊,預設為true。因為這是一個單點的Eureka Server
# 不需要同步其他的Eureka Server節點的資料,故而設為false。
eureka.client.fetch-registry=false

# Eureka Server互動的位址,查詢服務和注冊服務都需要依賴這個位址
# 注冊中心叢集 雙注冊中心 peer1  peer2   10000  10001 10002
eureka.client.service-url.defaultZone=http://localhost:10000/eureka/
           
peer1 :application-peer1.properties
server.port=10001

spring.application.name=eureka-server
# hosts 檔案中添加host名稱
eureka.instance.hostname=peer1
#開啟
eureka.client.register-with-eureka=true
#同步節點資料 開啟
eureka.client.fetch-registry=true

# 注冊中心叢集 雙注冊中心 peer1  peer2   10000  10001 10002
# 節點互相指向
eureka.client.service-url.defaultZone=http://peer2:10002/eureka/
           
peer2 :application-peer2.properties
server.port=10002

spring.application.name=eureka-server
# hosts 檔案中添加host名稱
eureka.instance.hostname=peer2
#開啟
eureka.client.register-with-eureka=true
#同步節點資料 開啟
eureka.client.fetch-registry=true

# 注冊中心叢集 雙注冊中心 peer1  peer2   10000  10001 10002
# 節點互相指向
eureka.client.service-url.defaultZone=http://peer1:10001/eureka/
           
open-cluster 開啟注冊中心叢集(雙注冊中心)

條件:

hosts 檔案中添加:peer1 peer2

SpringCloud(2):注冊中心Eureka的搭建

hosts

# 打包
clean package
# 使用peer1  peer2  啟動jar
args: --spring.profiles.active=peer1
--spring.profiles.active=peer2
           

啟動類添加注解 @EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

啟動一下通路看看

SpringCloud(2):注冊中心Eureka的搭建

Eureka注冊中心

我們的注冊中心已經ok了