您的位置:首页 > 编程语言 > Java开发

SpringCloud-Eureka服务注册于发现中心

2019-09-27 18:25 337 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/qq_42547321/article/details/101551411

Eureka分为三端

  1. Eureka Server : Eureka的服务注册与发现中心端(核心),它有服务注册和发现服务的能力,也就是注册中心
  2. Eureka Provider : 服务提供者,用于简化与Euraka Server 的交互
  3. Eureka Consumer : 服务调用者也可以说是服务消费者,也同样是用于简化与Eureka Server 的交互

Eureka Provider与Eureka Consumer都是微服务中的服务端和消费端(客户端),例如:我们项目有两个模块,一个是订单管理,一个是会员管理,这两个模块都分别向Eureka Server中注册自己,订单管理需要获取到会员管理中用户的信息,此时,会员管理是服务体提供者(服务端),订单管理就是服务消费者(消费端、客户端),那么由此而来,我们每一个模块,都有可能是客户端也同样可能是服务端。

那么,微服务启动时,Eureka Client都会向Eureka Server中注册自己,那么Eureka Server就会存储每个注册进来的服务的信息。

Eureka的特性以及相关配置

1.服务注册: 当我们的Eureka Client启动的之后,会通过rest请求的方式将自己注册到Eureka Server服务发现注册中心
eureka.client.register-with-eureka=true 是否向注册中心注册自己 默认 true
eureka.client.fetch-registry=true 是否去检索注册中心的服务 默认 true
2.服务续约:当我们的服务提供者和服务消费者(Eureka Client)在我们的Eureka Server服务者注册中心注册之后,会周期性的向Eureka Server发送心跳包,进行续约,每30秒,防止Eureka Server 的“剔除任务”将该服务排除出去
eureka.instance.lease-renewal-interval-in-seconds=30 定义服务续约任务的调用间隔时间 默认30s
3.服务续期(剔除任务):Eureka Server在启动之后,会每间隔60秒,去执行一次服务实效检测功能,有些时候我们的服务不一定是正常下线,有可能是因为网络的原因不能正常运作,为了从服务列表中将这些无法提供服务的实例剔除,Eureka Server在启动的时候会创建一个定时任务,默认每隔一段时间(默认为60秒)将当前清单中超时(默认为90秒)没有续约的服务剔除出去 4.服务识别:Eureka Client会缓存Eureka Server中的服务信息,即使所有的Eureka Server都宕掉,服务消费者仍然可以从缓存的信息中找到服务提供者

接下来创建我们的Eureka Server

pom.xml

<dependencies>
<!--eureka服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

我们在创建新的项目时,如果选择了相关SpringCloud的依赖,则会自动在pom.xml配置文件内添加SpringCloud最新稳定版本依赖配置。
spring-cloud-dependencies这个依赖是SpringCloud内所需要依赖的版本维护,在maven项目内如果被内修饰的,子项目或者本项目在使用时可以不用设置版本号,默认使用下内设置的版本信息。
正因如此,这也是为什么我们添加了spring-cloud-dependencies依赖后,在使用相关SpringCloud插件时可以不用添加version标签设置导入指定版本的依赖。本文章中的version都设置了,其实不用引

application.yml

server:
port: 50001  #端口号

spring:
application:
name: sjh-eureka01  #微服务名称

eureka:
server:
enable-self-preservation: false           #自我保护机制 (红字提醒)
eviction-interval-timer-in-ms: 60000     #eureka清理无效节点的时间间隔,默认60000毫秒,即60秒 其实可以不写
client:
fetch-registry: false   #本身就是服务注册中心不需要去注册中心1拉取服务注册表  默认为true
register-with-eureka: false  #是否注册进Eureka服务注册中心 默认为true
service-url:
defaultZone: http://localhost:50002/eureka/   #将本eureka注册进9002eureka服务注册中心(通常我们是建立多个Eureka Server实现服务的高可用)
instance:
hostname: eurekaserver01  #当前实例的主机名称
prefer-ip-address: true   #使用ip注册到eureka Server上
instance-id: eureka01
lease-renewal-interval-in-seconds: 30 #心跳时间,即服务续约间隔时间( 默认为30s) 其实可以不写
lease-expiration-duration-in-seconds: 90 #触发剔除任务的时间,即服务续约到期时间(默认为90s) 其实可以不写

启动类EurekaApplication @EnableEurekaServer

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* 服务注册发现中心
*/
@SpringBootApplication
@EnableEurekaServer //这是EurekaServer端
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(Eureka01Application.class,args);
}
}

创建Eureka Client

pom.xml

<dependencies>
<!--eureka client端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<version>2.1.1.RELEASE</version>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

application.yml

server:
port: 60001   #端口号

spring:
application:
name: sjh-client01

eureka:
client:
register-with-eureka: true  #向eurekaServer注册自己  默认为true 其实可以不写
fetch-registry: true  #因为是服务消费者,所以需要向注册中心拉去服务表 默认为true 其实可以不写
service-url:
defaultZone: http://localhost:50001/eureka/,http://localhost:50002/eureka/   #向两个eureka注册中心注册自己,实现高可用
instance:
prefer-ip-address: true
instance-id: client01

logging:
level:
com.sjh: debug

启动类ClientApplication @EnableEurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
* 业务端,eurekaClient端
*/
@SpringBootApplication
@EnableEurekaClient //表示一个EurekaClient端
public class Client01Application {

public static void main(String[] args) {
SpringApplication.run(Client01Application.class,args);
}

}

这样,先启动Eureka Server端,因为Eureka Client需要向Eureka Server 注册自己,访问Eureka Server http://localhost:50001 就可以看到我们注册在Eureka Server中的Eureka Cilent了,那么像之前讲的那样,如果有多个Eureka Client 之间需要互相联系,这就是 Spring Cloud Feign 远程调用的事情了,到这里一个基于Eureka的服务注册与发现就完成了

Spring Cloud Feign 请看后续文章。。。。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: