Eureka
Netflix在设计Eureka时遵守的就是AP原则 CAP原则又称CAP定理,指的是在一个分布式系统中,Consistency(一致性)、 Availability(可用性)、Partition tolerance(分区容错性),三者不可兼得Eureka基本架构
Spring Cloud 封装了 Netflix 公司开发的 Eureka 模块来实现服务注册和发现(请对比Zookeeper)。 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秒)三大结构
1、Eureka server提供服务注册与发现 2、service provide将自身服务注册到Eureka,从而使服务消费方能够找到 3、service consumer从Eureka获取注册服务列表,从而能够消费服务项目结构
总父工程 通用模块API 服务提供者Provider 服务消费者Consumer eureka服务注册中心modulePOM
主要包含一个spring-cloud-starter-eureka-server
<?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</artifactId> <groupId>com.lee</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-eureka-7001</artifactId> <dependencies> <!--eureka-server服务端 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <!-- 修改后立即生效,热部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies> </project>注意:
基本上我们要引入cloud的一个新的技术组件,会有两步 1、新增一个相关的maven坐标 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> 2、在主启动类上标注启动该新组件的相关注解标签 @EnableEurekaServer @EnableZuulProxy @EnableXXXXX ...APPLICATION.YML
主要暴露eureka端口和对外服务地址
server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: register-with-eureka: false #false表示不向注册中心注册自己。 fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址。主启动类
package com.lee.cloud; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; //EurekaServer服务器端启动类,接受其它微服务注册进来 @EnableEurekaServer @SpringBootApplication public class EurekaServer7001_App { public static void main(String[] args) { SpringApplication.run(EurekaServer7001_App.class,args); } }测试
http://localhost:7001修改cloud-provider-dept-8001
POM - 新增 eureka客户端 config配置中心
<!-- 将微服务provider侧注册进eureka --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!--注意:--> <!-- 如果eureka下载不下来的话 改成这个 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.6.RELEASE</version> </dependency> -->YML - 新增 eureka服务地址
eureka: client: #客户端注册进eureka服务列表内 service-url: defaultZone: http://localhost:7001/eureka主启动类 - 新增 将本服务启动后自动注册进eureka服务中
@EnableEurekaClient //本服务启动后会自动注册进eureka服务中测试:
1、启动 cloud-eureka-7001 2、启动 cloud-provider-dept-8001 3、http://localhost:7001 注意:暴露在eureka中的微服务名称,即provider的yml中spring:application:name的名称 如果找不到服务的话,大概率是应为spring-cloud-starter-eureka和 <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR1</version> 版本不一致导致的Eureka服务中的显示
ApplicationAMIsAvailability ZonesStatusCLOUD-DEPTn/a (1)(1)UP (1) - windows10.microdone.cn:cloud-dept:8001目标:修改 windows10.microdone.cn:cloud-dept:8001的显示
cloud-provider-dept-8001修改:
YML
eureka: instance: instance-id: cloud-dept8001结果:
ApplicationAMIsAvailability ZonesStatusCLOUD-DEPTn/a (1)(1)UP (1) - cloud-dept8001鼠标移动到上侧超链接时,浏览器左下角的链接显示
cloud-provider-dept-8001修改:
YML
eureka: instance: prefer-ip-address: true #访问路径可以显示IP地址cloud-dept8001超了解点击后内容的构建
cloud-provider-dept-8001修改:
POM
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>YML
info: #内容以info开头下边随便写 app.name: cloud author.name: lee app.function: 部门服务提供者 build.artifactId: $project.artifactId$ build.version: $project.version$CLOUD父工程POM
<build><!--构建--> <finalName>microservicecloud</finalName> <resources> <!--可以访问src/main/resources下的文件--> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <configuration> <delimiters> <delimit>$</delimit> </delimiters> </configuration> </plugin> </plugins> </build>结果:
访问:http://192.168.101.39:8001/info 显示: {"app": { "name":"cloud", "function":"部门服务提供者" }, "author":{"name":"lee"}, "build":{ "artifactId":"$project.artifactId$", "version":"$project.version$" } }(1)、仿照cloud-eureka-7001创建cloud-eureka-7002和cloud-eureka-7003
(2)、修改 映射
c:\Windows\System32\drivers\etc下的host文件 添加如下: 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com(3)、三台eureka服务的yml配置
eureka: instance: hostname: eureka7001.com ##eureka服务端的实例名称,知识localhost换了个名而已 eureka: client: defaultZone: http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka ##也可以是http://localhost:7002/eureka,http://localhost:7003/eureka(4)、cloud-provider-dept-8001 YML服务地址修改
eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka测试:
http://eureka7001.com:7001 http://eureka7002.com:7002 http://eureka7003.com:7003拓展知识
#服务发现 @AutoWire private DiscoveryClient client; List<String> serviceList = client.getServices();//列出Eureka中所有的服务 List<ServiceInstance> instanceList = client.getInstances("CLOUD-DEPT");//查找服务实例 instance.getServiceId();//实例ID instance.getHost();//实例主机 instance.getPort();//实例端口 instance.getUri();//实例Uri @EnableDiscoveryClient //主启动类上+服务发现注解