1.创建父工程:
1.1添加pom依赖
<properties>
<web-version>2.7.1</web-version>
<test-version>2.7.1</test-version>
<spring-Cloud-version>Hoxton.SR12</spring-Cloud-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${web-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>${test-version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<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>
2.在工程下创建两个子工程server和client
3.在server项目下的创建启动类
3.1 导入父类依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
3.2在resource下创建yml文件
server:
port: 8080
spring:
application:
name: yn
eureka:
client:
service-url:
defaultZone: http://location:8080/eureka/
fetch-registry: false #是否拉取服务列表
server:
eviction-interval-timer-in-ms: 30000 #清除无效节点频率-(定期清理)
enable-self-preservation: false #server的自我保护机制,避免因为网络因素误删,生产环境建议为打开
renewal-percent-threshold: 0.85 #如果85%的client没有续约,则会认为是网络原因,保留服务实例,不会删除
instance:
hostname: yn1104
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port} #实例id
prefer-ip-address: true #服务列表以id形式
lease-expiration-duration-in-seconds: 90 #client给server的上次心跳之后,如果在该时间内没收到下一次心跳频率,则移除改实例
lease-renewal-interval-in-seconds: 30 #client给server发送的心跳频率
3.3运行启动类
注:红色是因为未开启自我保护机制
3.1.0 Client的pom配置
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
yml配置信息
server:
port: 8761 #如果不是该端口,就会报错,但是不要紧张
spring:
application:
name: yn #夹带私货不要介意哈
eureka:
client:
service-url:
defaultZone: http://location:8080/eureka
register-with-eureka: true #注册自己
fetch-registry: true #拉取服务列表
registry-fetch-interval-seconds: 5 #表示client间隔多久被拉取服务注册 信息
instance:
hostname: yn1104
instance-id: ${eureka.instance.hostname}:${spring.application.name}:${server.port} #实例id
prefer-ip-address: true #服务列表以id形式
lease-expiration-duration-in-seconds: 90 #client给server的上次心跳之后,如果在该时间内没收到下一次心跳频率,则移除改实例
lease-renewal-interval-in-seconds: 30 #client给server发送的心跳频率