《重新定义Spring Cloud实战》——2.2 Spring Cloud Eureka入门案例

举报
华章计算机 发表于 2019/06/04 13:24:05 2019/06/04
【摘要】 本书摘自《重新定义Spring Cloud实战》——书中第2章,第2.2节,作者是许进、叶志远、钟尊发、蔡波斯、方志朋、郭芳碧、朱德明。

2.2 Spring Cloud Eureka入门案例

下面来让我们体验一下Eureka的Hello World工程, 这里需要用到的组件是Spring Cloud Netflix Eureka。

1.创建Maven父级pom工程

在父工程里面配置好工程需要的父级依赖,目的是为了方便管理与简化配置,如代码清单2-1所示。

代码清单2-1 ch2-1\pom.xml

<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-web</artifactId>

    </dependency>


    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>

</dependencies>

2.创建Eureka Server工程

为避免赘述,在本章中,Eureka组件配置只展示一次,后续实战演练配置中心皆基于此。

配置Eureka Server工程的pom.xml文件,只需要添加spring-cloud-starter-netflix-eureka-server即可,注意F版和之前的版本有些变化,如代码清单2-2所示。

代码清单2-2 ch2-1\ch2-1-eureka-server\pom.xml

<dependencies>

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>

    </dependency>

</dependencies>

对于Eureka的启动主类,这里添加相应注解,作为程序的入口,如代码清单2-3所示。

代码清单2-3 ch2-1\ch2-1-eureka-server\src\main\java\cn\springcloud\book\Ch21Eureka- ServerApplication.java

@SpringBootApplication

@EnableEurekaServer

public class Ch21EurekaServerApplication {


    public static void main(String[] args) {

        SpringApplication.run(Ch21EurekaServerApplication.class, args);

    }

}

Eureka Server需要的配置文件,如代码清单2-4所示。

代码清单2-4 ch2-1\ch2-1-eureka-server\src\main\resources\application-standalone.yml

server:

    port: 8761


eureka:

    instance:

        hostname: localhost

    client:

        registerWithEureka: false

        fetchRegistry: false

        serviceUrl:

            defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

    server:

            waitTimeInMsWhenSyncEmpty: 0

            enableSelfPreservation: false

这里的单机版配置,仅仅是为了演示,切勿用于生产。

3.创建Eureka Client组件工程

配置Eureka Client工程的pom.xml文件,只需要引入spring-cloud-starter-netflix-eureka-client即可,注意也与之前版本不一样,如代码清单2-5所示。

代码清单2-5 ch2-1\ch2-1-eureka-client \pom.xml

<dependencies>

    <dependency>

        <groupId>org.springframework.cloud</groupId>

        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

    </dependency>

</dependencies>

添加Eureka Client的启动主类,如代码清单2-6所示。

代码清单2-6 ch2-1\ch2-1-eureka-client\src\main\java\cn\springcloud\book \Ch21Eureka-ClientApplication.java

@SpringBootApplication

@EnableDiscoveryClient

public class Ch21EurekaClientApplication {


    public static void main(String[] args) {

        SpringApplication.run(Ch21EurekaClientApplication.class, args);

    }

}

Eureka Client的配置文件,如代码清单2-7所示。

代码清单2-7 ch2-1\ch2-1-eureka-client\src\main\resources\application-demo.yml

server:

    port: 8081


spring:

    application:

        name: demo-client1


eureka:

    client:

        serviceUrl:

            defaultZone: http://localhost:8761/eureka/

这里需要说明一下,需要指定spring.application.name,不然会在Eureka Server界面显示为UNKNOWN。

4.效果展示

分别启动eureka-server及eureka-client,然后访问http://localhost:8761,结果如图2-2所示。

通过访问Eureka Server的rest api接口,比如http://localhost:8761/eureka/apps,返回的结果如下:

<applications>

    <versions__delta>1</versions__delta>

    <apps__hashcode>UP_1_</apps__hashcode>

    <application>

        <name>DEMO-CLIENT1</name>

        <instance>

            <instanceId>10.2.238.223:demo-client1:8081</instanceId>

            <hostName>10.2.238.223</hostName>

            <app>DEMO-CLIENT1</app>

            <ipAddr>10.2.238.223</ipAddr>

            <status>UP</status>

            <overriddenstatus>UNKNOWN</overriddenstatus>

            <port enabled="true">8081</port>

            <securePort enabled="false">443</securePort>

            <countryId>1</countryId>

            <dataCenterInfo class="com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo">

                <name>MyOwn</name>

            </dataCenterInfo>

            <leaseInfo> 

                <renewalIntervalInSecs>30</renewalIntervalInSecs>

                <durationInSecs>90</durationInSecs>  

                <registrationTimestamp>1529377552795</registrationTimestamp>

                <lastRenewalTimestamp>1529386376662</lastRenewalTimestamp>

                    <evictionTimestamp>0</evictionTimestamp>

                    <serviceUpTimestamp>1529377552795</serviceUpTimestamp>

                </leaseInfo>

                <metadata>

                    <management.port>8081</management.port>

            </metadata>

            <homePageUrl>http://10.2.238.223:8081/</homePageUrl>

            <statusPageUrl>http://10.2.238.223:8081/actuator/info</statusPageUrl>

            <healthCheckUrl>http://10.2.238.223:8081/actuator/health</healthCheckUrl>

            <vipAddress>demo-client1</vipAddress>

            <secureVipAddress>demo-client1</secureVipAddress>

            <isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>

            <lastUpdatedTimestamp>1529377552795</lastUpdatedTimestamp>

            <lastDirtyTimestamp>1529377552665</lastDirtyTimestamp>

            <actionType>ADDED</actionType>

        </instance>

    </application>

</applications>

image.png

图2-2 eureka-server管理界面


【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

举报
请填写举报理由
0/200