美文网首页
MAVEN多模块项目Spring'Cloud'Eureka

MAVEN多模块项目Spring'Cloud'Eureka

作者: 苗義 | 来源:发表于2019-08-03 23:53 被阅读0次

    使用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文件如下:

    <?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、创建子项目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文件如下:

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

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

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

    在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文件如下:

    <?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的启动类如下:

    @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

          本文链接:https://www.haomeiwen.com/subject/cjxedctx.html