《重新定义Spring Cloud实战》——2.2 Spring Cloud Eureka入门案例
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>
图2-2 eureka-server管理界面
- 点赞
- 收藏
- 关注作者
评论(0)