什么是eureka
服务注册中心, 服务治理的一个框架组件,用来实现各个服务实例的自动化注册和发现
服务注册
在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,包括服务的主机与端口号、服务版本号、通讯协议等一些附加信息。注册中心按照服务名分类组织服务清单,同时还需要以心跳检测的方式去监测清单中的服务是否可用,若不可用需要从服务清单中剔除,以达到排除故障服务的效果。
服务发现
在服务治理框架下,服务间的调用不再通过指定具体的实例地址来实现,而是通过服务名发起请求调用实现。服务调用方通过服务名从服务注册中心的服务清单中获取服务实例的列表清单,通过指定的负载均衡策略取出一个服务实例位置来进行服务调用。
Eureka服务端
创建springBoot项目eureka-server
引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
完整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 https://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.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hc</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.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<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>
注意我这里的springCloud版本<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
配置文件application.yml
server:
port: 8761
eureka:
# instance:
# prefer-ip-address: true #显示IP地址
# instance-id: eureka注册中心1:${server.port}
# hostname: centos101 #ip映射地址 windows电闹在hosts文件中配置127.0.0.1 centos101
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/ #http://${eureka.instance.hostname}:${server.port}/eureka/
# 默认情况下,eureka server同时也是eureka client,用于相互注册形成高可用eureka服务。
# 单点时,如果registerWithEureka配置为true,则eureka server会报错Cannot execute request on any known server
# 是否注册到eureka服务,默认为true,当前已为eureka server,且单点eureka,故配置为false
registerWithEureka: false
#是否在本地缓存注册表信息,默认为true,当前为单点eureka server,不需要从其他eureka除获取注册表信息,更谈不上缓存,故配置为false
fetchRegistry: false
server:
#在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间,单位为毫秒,默认为1000 * 60 * 5
wait-time-in-ms-when-sync-empty: 0
#自我保护模式, 暂时关闭
enable-self-preservation: false
客户端,需要注册的服务,也可以说成服务提供者,生产者
添加eureka客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
完整依赖
<?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 https://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.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hc</groupId>
<artifactId>eureka-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-client</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
</properties>
<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-web</artifactId>
</dependency>
<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>
yml配置文件
server:
port: 8080
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
#指定服务名称
name: eureka-client
启动两个项目访问http://localhost:8761/eureka/apps
返回如下xml
<applications>
<versions__delta>1</versions__delta>
<apps__hashcode>UP_1_</apps__hashcode>
<application>
<name>EUREKA-CLIENT</name>
<instance> <!--实例信息-->
<!--实例id yml配置中我把instanceId给注掉了,这里有个默认的id就是主机名+服务名+端口号-->
<instanceId>DESKTOP-C4NAI72:eureka-client:8080</instanceId>
<!--因为我没有配置hostName,默认就是主机名, 看我下面的我的电脑的属性, 就是我计算机的名字-->
<hostName>DESKTOP-C4NAI72</hostName>
<app>EUREKA-CLIENT</app> <!--引用名称-->
<ipAddr>192.168.0.6</ipAddr><!--ip地址-->
<status>UP</status> <!--服务状态,up启动-->
<overriddenstatus>UNKNOWN</overriddenstatus>
<port enabled="true">8080</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>1570852135332</registrationTimestamp>
<lastRenewalTimestamp>1570862065643</lastRenewalTimestamp>
<evictionTimestamp>0</evictionTimestamp>
<serviceUpTimestamp>1570852134349</serviceUpTimestamp>
</leaseInfo>
<metadata>
<management.port>8080</management.port>
<jmx.port>50083</jmx.port>
</metadata><!--元数据-->
<homePageUrl>http://DESKTOP-C4NAI72:8080/</homePageUrl>
<statusPageUrl>http://DESKTOP-C4NAI72:8080/actuator/info</statusPageUrl>
<healthCheckUrl>http://DESKTOP-C4NAI72:8080/actuator/health</healthCheckUrl>
<vipAddress>eureka-client</vipAddress>
<secureVipAddress>eureka-client</secureVipAddress>
<isCoordinatingDiscoveryServer>false</isCoordinatingDiscoveryServer>
<lastUpdatedTimestamp>1570852135332</lastUpdatedTimestamp>
<lastDirtyTimestamp>1570852133160</lastDirtyTimestamp>
<actionType>ADDED</actionType>
</instance>
</application>
</applications>
单机至此完成
Eureka简单集群—伪集群
这里我只有一台机器, 虚拟机的话可以弄,但是太麻烦, 就做个伪集群, 这个会了,真集群也就是分分钟的事情
更改eureka-server服务的配置文件
server:
port: 8761
eureka:
#注册实例
instance:
prefer-ip-address: true #显示IP地址
instance-id: eureka注册中心1:${server.port}
# hostname: centos101 #ip映射地址 windows电闹在hosts文件中配置127.0.0.1 centos101 我这里就不配置了
client:
serviceUrl:
defaultZone: http://localhost:8762/eureka/
# 默认情况下,eureka server同时也是eureka client,用于相互注册形成高可用eureka服务。
# 单点时,如果registerWithEureka配置为true,则eureka server会报错Cannot execute request on any known server
# 是否注册到eureka服务,默认为true,当前已为eureka server,且单点eureka,故配置为false
registerWithEureka: true
#是否在本地缓存注册表信息,默认为true,当前为单点eureka server,不需要从其他eureka处获取注册表信息,更谈不上缓存,故配置为false
fetchRegistry: true
server:
#在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间,单位为毫秒,默认为1000 * 60 * 5
wait-time-in-ms-when-sync-empty: 0
#自我保护模式, 暂时关闭
enable-self-preservation: true
spring:
application:
name: eureka-server1 #服务名称
profiles: peer1 #指定前缀
---
server:
port: 8762
eureka:
instance:
prefer-ip-address: true #显示IP地址
instance-id: :eureka注册中心2:${server.port}
# hostname: centos102
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
# 默认情况下,eureka server同时也是eureka client,用于相互注册形成高可用eureka服务。
# 单点时,如果registerWithEureka配置为true,则eureka server会报错Cannot execute request on any known server
# 是否注册到eureka服务,默认为true,当前已为eureka server,且单点eureka,故配置为false
registerWithEureka: true
#是否在本地缓存注册表信息,默认为true,当前为单点eureka server,不需要从其他eureka处获取注册表信息,更谈不上缓存,故配置为false
fetchRegistry: true
server:
#在Eureka服务器获取不到集群里对等服务器上的实例时,需要等待的时间,单位为毫秒,默认为1000 * 60 * 5
wait-time-in-ms-when-sync-empty: 0
#自我保护模式, 暂时关闭
enable-self-preservation: true
spring:
application:
name: eureka-server2
profiles: peer2
使用指定配置文件启动server
–spring.profiles.active=peer1
–spring.profiles.active=peer2
查看结果 已注册的服务
分别访问http://localhost:8761/eureka/apps, http://localhost:8762/eureka/apps
因为我这里是在一台机子上运行的,又懒得配置hosts文件,所以你看到的集群节点就只有localhost
eureka控制台详解