MAVEN多模塊項(xiàng)目Spring'Cloud'Eureka

使用IDEA新建MAVEN的多模塊項(xiàng)目

Spring Cloud的環(huán)境配置:

JDK/SDK:1.8.0_212

Maven version: 3.5.0

spring-boot.version:2.1.6.RELEASE

spring-cloud.version:Greenwich.RELEASE

[TOC]

一、Spring Cloud和Spring Boot的版本

注意: 截至到今天(2019/08/02),各個(gè)版本的對(duì)應(yīng)情況:

Release Train Boot Version
Greenwich 2.1.x
Finchley 2.0.x
Edgware 1.5.x
Dalston 1.5.x

請(qǐng)注意Spring Cloud的overview里的這段話:

Finchley builds and works with Spring Boot 2.0.x, and is not expected to work with Spring Boot 1.5.x.

Note: The Dalston release train will reach end-of-life in December 2018. Edgware will follow the end-of-life cycle of Spring Boot 1.5.x.

The Dalston and Edgware release trains build on Spring Boot 1.5.x, and are not expected to work with Spring Boot 2.0.x.

以上內(nèi)容摘自:https://spring.io/projects/spring-cloud#overview

二、創(chuàng)建服務(wù)注冊(cè)和發(fā)現(xiàn)中心

Spring Cloud Netflix項(xiàng)目提供的集成有:

服務(wù)發(fā)現(xiàn)(Eureka)

斷路器(Hystrix)

智能路由(Zuul)

客戶端負(fù)載平衡(Ribbon)

2.1、創(chuàng)建服務(wù)的父級(jí)工程

我使用的是Eureka,之前我也用過Consul,先說一下Eureka吧。

[圖片上傳失敗...(image-2c4ac-1564847573158)]

[圖片上傳失敗...(image-a1cb02-1564847573159)]

[圖片上傳失敗...(image-d9458a-1564847573159)]

注意Type的時(shí)候選擇Maven POM,然后一路next就可以了。修改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.ac</groupId>
    <artifactId>echo-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>echo-parent</name>
    <description>Demo project for Spring Boot</description>

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

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

2.2、創(chuàng)建子項(xiàng)目Eureka Service和Eureka Client

在echo-parent的項(xiàng)目上右鍵-->>New-->>Module,選擇Spring Initializr,選擇合適的GAV屬性標(biāo)簽,這里的Type選擇Maven Project,然后添加eureka對(duì)應(yīng)的起步依賴即可。

[圖片上傳失敗...(image-efee19-1564847573159)]

[圖片上傳失敗...(image-7ccc7e-1564847573159)]

[圖片上傳失敗...(image-7cbb7-1564847573159)]

修改eureka-service的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>com.ac</groupId>
        <artifactId>echo-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.ac</groupId>
    <artifactId>eureka-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-service</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

在父級(jí)pom中添加eureka的module如下:

<modules>
    <module>eureka-service</module>
</modules>

在spring boot的啟動(dòng)類EurekaServiceApplication上添加eureka的注釋@EnableEurekaServer:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServiceApplication {

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

}

配置文件采用yml格式:

spring:
  application:
    name: eurka-server

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

在yaml文件中的registerWithEureka和fetchRegistry設(shè)置成false,表明當(dāng)前是eureka的服務(wù)器端。在瀏覽器的地址欄輸入http://localhost:8761即可以看到eureka的界面了:

[圖片上傳失敗...(image-6a3b62-1564847573159)]

接下來創(chuàng)建eureka的client項(xiàng)目,創(chuàng)建過程同eureka server差不多,選擇的starter是客戶端:

[圖片上傳失敗...(image-1191fa-1564847573159)]

修改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>com.ac</groupId>
        <artifactId>echo-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <groupId>com.ac</groupId>
    <artifactId>eureka-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>eureka-client</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml文件如下:

spring:
  application:
    name: eureka-client

server:
  port: 8762

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

eureka-client的啟動(dòng)類如下:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

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

}

啟動(dòng)eureka-service和eureka-client,打開http://localhost:8761/就可以看到實(shí)例已經(jīng)注冊(cè)上去了,服務(wù)名稱是eureka-client,端口號(hào)是8762:

[圖片上傳失敗...(image-a621d7-1564847573159)]

打開http://localhost:8762/,可以看到我們的hello world。

注意:現(xiàn)在在eureka-client可以不用輸入注釋@EnableEurekaClient就可以開啟eureka的客戶端支持?。?!以下是spring cloud官方提供的說明,請(qǐng)注意:只要在classpath中引用了spring-cloud-starter-netflix-eureka-client的GAV就可以開始eureka的支持了,至于原理需要看一下源碼才能分析出來。

The following example shows a minimal Eureka client application:

@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello world";
    }

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

Note that the preceding example shows a normal Spring Boot application. By having spring-cloud-starter-netflix-eureka-client on the classpath, your application automatically registers with the Eureka Server. Configuration is required to locate the Eureka server, as shown in the following example: ... ...

摘自:https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_service_discovery_eureka_clients

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容