MAVEN多模块项目Spring'Cloud'Eureka

使用IDEA新建MAVEN的多模块项目

Spring Cloud的环境配置:

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),各个版本的对应情况:

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

请注意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.

以上内容摘自:https://spring.io/projects/spring-cloud#overview

二、创建服务注册和发现中心

Spring Cloud Netflix项目提供的集成有:

服务发现(Eureka)

断路器(Hystrix)

智能路由(Zuul)

客户端负载平衡(Ribbon)

2.1、创建服务的父级工程

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

[图片上传失败...(image-2c4ac-1564847573158)]

[图片上传失败...(image-a1cb02-1564847573159)]

[图片上传失败...(image-d9458a-1564847573159)]

注意Type的时候选择Maven POM,然后一路next就可以了。修改pom文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.6.RELEASE
         
    
    com.ac
    echo-parent
    0.0.1-SNAPSHOT
    pom

    echo-parent
    Demo project for Spring Boot

    
        UTF-8
        UTF-8
        1.8
        Greenwich.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


2.2、创建子项目Eureka Service和Eureka Client

在echo-parent的项目上右键-->>New-->>Module,选择Spring Initializr,选择合适的GAV属性标签,这里的Type选择Maven Project,然后添加eureka对应的起步依赖即可。

[图片上传失败...(image-efee19-1564847573159)]

[图片上传失败...(image-7ccc7e-1564847573159)]

[图片上传失败...(image-7cbb7-1564847573159)]

修改eureka-service的pom文件如下:



    4.0.0
    
        com.ac
        echo-parent
        0.0.1-SNAPSHOT
    

    com.ac
    eureka-service
    0.0.1-SNAPSHOT
    jar

    eureka-service
    Demo project for Spring Boot

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


在父级pom中添加eureka的module如下:


    eureka-service

在spring boot的启动类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设置成false,表明当前是eureka的服务器端。在浏览器的地址栏输入http://localhost:8761即可以看到eureka的界面了:

[图片上传失败...(image-6a3b62-1564847573159)]

接下来创建eureka的client项目,创建过程同eureka server差不多,选择的starter是客户端:

[图片上传失败...(image-1191fa-1564847573159)]

修改pom文件如下:



    4.0.0
    
        com.ac
        echo-parent
        0.0.1-SNAPSHOT
    
    com.ac
    eureka-client
    0.0.1-SNAPSHOT
    jar

    eureka-client
    Demo project for Spring Boot

    
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


application.yml文件如下:

spring:
  application:
    name: eureka-client

server:
  port: 8762

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

eureka-client的启动类如下:

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

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

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

}

启动eureka-service和eureka-client,打开http://localhost:8761/就可以看到实例已经注册上去了,服务名称是eureka-client,端口号是8762:

[图片上传失败...(image-a621d7-1564847573159)]

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

注意:现在在eureka-client可以不用输入注释@EnableEurekaClient就可以开启eureka的客户端支持!!!以下是spring cloud官方提供的说明,请注意:只要在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

你可能感兴趣的:(MAVEN多模块项目Spring'Cloud'Eureka)