本文主要是介绍Spring Cloud版本的若依(2)--Eureka,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Eureka 采用了 C-S 的设计架构。Eureka Server 作为服务注册功能的服务器,它是服务注册中心。
而系统中的其他微服务,使用 Eureka 的客户端连接到 Eureka Server并维持心跳连接。这样系统的维护人员就可以通过 Eureka Server 来监控系统中各个微服务是否正常运行。SpringCloud 的一些其他模块(比如Zuul)就可以通过 Eureka Server 来发现系统中的其他微服务,并执行相关的逻辑。
Eureka包含两个组件:Eureka Server和Eureka Client Eureka Server提供服务注册服务 各个节点启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到.
EurekaClient是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)
加入依赖
<?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"><parent><artifactId>cloud-zero</artifactId><groupId>com.wind</groupId><version>1.0.0</version></parent><modelVersion>4.0.0</modelVersion><artifactId>cloud-zero-eureka</artifactId><dependencies><!--服务中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies></project>
新建启动器
package com.wind.cloud.zero;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication
@EnableEurekaServer // EurekaServer服务器端启动类,接受其它微服务注册进来
public class EurekaApp
{public static void main(String[] args){SpringApplication.run(EurekaApp.class);}
}
新建application.yml
server: port: 7001eureka: instance:hostname: eureka7001.com #eureka服务端的实例名称client: register-with-eureka: false #false表示不向注册中心注册自己。fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务service-url: #单机 #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
绑定host,绑定是为方便后续调用或者更改,部署也方便很多
127.0.0.1 eureka7001.com
#集群需要
127.0.0.1 eureka7002.com
127.0.0.1 eureka7003.com
provider-user连接注册中心,
pom.xml
<!--eureka 客户端 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>
application.yml添加配置
eureka:client: # 客户端注册进eureka服务列表内service-url:defaultZone: http://localhost:7001/eureka#defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/instance:instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}prefer-ip-address: true # 访问路径可以显示IP地址
在启动类上添加注解@EnableDiscoveryClient 或@EnableEurekaClient
eureka:client: # 客户端注册进eureka服务列表内service-url:defaultZone: http://localhost:7001/eureka#defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/instance:instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}prefer-ip-address: true # 访问路径可以显示IP地址
从Spring Cloud Edgware开始,@EnableDiscoveryClient 或@EnableEurekaClient 可省略。只需加上相关依赖,并进行相应配置,即可将微服务注册到服务发现组件上。
@EnableDiscoveryClient和@EnableEurekaClient共同点就是:都是能够让注册中心能够发现,扫描到改服务。
不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
这篇关于Spring Cloud版本的若依(2)--Eureka的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!