目录
1.创建maven项目
2.idea打开maven项目
3.pom.xml文件如下
<?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>
<!--spring boot版本依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.lizz</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.RC2</spring-cloud.version>
</properties>
<dependencies>
<!--web项目 默认使用内置tomcat-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--eureka server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--test 相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--spring cloud版本依赖-->
<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>
<!--导出文件名-->
<finalName>../eureka-server</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<!--maven远程仓库-->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
4.创建yml配置文件-单点
在src/main/resources目录下创建application.yml文件(使用application.properties也可以)
spring:
application:
name: eureka-peer #服务名
server:
port: 9991 #服务访问端口
servlet:
context-path: / #访问路径
eureka:
instance:
appname: eureka-server #eureka中注册Application名称,默认使用spring.application.name
client:
fetch-registry: false #是否获取注册信息
register-with-eureka: true #是否再eureka上进行注册,正式环境设置为false
serviceUrl:
#eureka server接口,/eureka/为内部固定服务地址,集群逗号分隔 http://eureka-peer2:9991/eureka/,http://eureka-peer3:9991/eureka/
defaultZone: http://localhost:9991/eureka/
5.开启Eureka Server
在启动文件EurekaServerApplication.java中增加@EnableEurekaServer标签,执行mian方法,启动服务器。
package com.lizz.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
//启动eureka server服务
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
... INFO 57393 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Got 1 instances from neighboring DS node INFO 57393 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Renew threshold is: 1 INFO 57393 --- [ Thread-13] c.n.e.r.PeerAwareInstanceRegistryImpl : Changing status to UP INFO 57393 --- [ Thread-13] e.s.EurekaServerInitializerConfiguration : Started Eureka Server
6.访问Eureka控制台
地址http://localhost:9991
7.高可用集群配置
伪集群:port 修改端口号,defaultZone url中端口号。
正常集群:修改defaultZone url中IP地址,也可以使用hosts映射。
server:
port: 9991 #服务访问端口
servlet:
context-path: / #访问路径
eureka:
instance:
appname: eureka-peer #eureka中注册Application名称,默认使用spring.application.name
client:
fetch-registry: false #是否获取注册信息 true false
register-with-eureka: true #是否再eureka上进行注册,正式环境设置为false,true false
serviceUrl:
#eureka server接口,http://ip:port/eureka/为内部固定服务地址
#集群配置,使用逗号分隔,只需配置同步服务。
defaultZone: http://192.168.180.15:9992/eureka/,http://192.168.180.15:9993/eureka/
8.启动所有集群服务
启动时会同步集群节点报错,同步服务全部启动后正常。
2019-01-15 19:40:12.302 ERROR 7383 --- [92.168.180.15-3] c.n.e.cluster.ReplicationTaskProcessor : Network level connection to peer 192.168.180.15; retrying after delay
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar!/:1.19.1]
at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.9.8.jar!/:1.9.8]
at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.8.jar!/:1.9.8]
at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar!/:1.19.1]
at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.9.8.jar!/:1.9.8]
at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:80) ~[eureka-core-1.9.8.jar!/:1.9.8]
at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:193) [eureka-core-1.9.8.jar!/:1.9.8]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_131]
9.访问Eureka集群控制台
DS Replicas: 集群服务,显示集群服务器地址,伪集群都再同一台服务器上,只显示了1个。
Status: 在线的3个eureka生产者服务。
registered-replicas:注册的eureka其他节点服务地址。
unavailable-replicas:不可用的eureka其他节点服务地址。
available-replicas:可用的eureka其他节点服务地址。
10.使用服务注册
生产者注册到eureka服务后,集群会自动进行同步。