全部spring cloud全部框架示例参考:http://www.weiqinxue.cn/blogs/index.php/User/articleview/ArticleID/U2A226
简介:
Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。
为什么要使用Eureka,因为在一个完整的系统架构中,任何单点的服务都不能保证不会中断,因此我们需要服务发现机制,在某个节点中断后,其它的节点能够继续提供服务,从而保证整个系统是高可用的。
服务发现有两种模式:一种是客户端发现模式,一种是服务端发现模式。Erueka采用的是客户端发现模式。
Eureka Server会提供服务注册服务,各个服务节点启动后,会在Eureka Server中进行注册,这样Eureka Server中就有了所有服务节点的信息,并且Eureka有监控页面,可以在页面中直观的看到所有注册的服务的情况。同时Eureka有心跳机制,当某个节点服务在规定时间内没有发送心跳信号时,Eureka会从服务注册表中把这个服务节点移除。Eureka还提供了客户端缓存的机制,即使所有的Eureka Server都挂掉,客户端仍可以利用缓存中的信息调用服务节点的服务。Eureka一般配合Ribbon进行使用,Ribbon提供了客户端负载均衡的功能,Ribbon利用从Eureka中读取到的服务信息,在调用服务节点提供的服务时,会合理的进行负载。
Eureka通过心跳检测、健康检查、客户端缓存等机制,保证了系统具有高可用和灵活性。
总结一下:
eureka是服务和服务之间的中间层,加了这层后就可以添加负载均衡、服务集群切换等使系统更稳定的处理
好了上代码:
server EurekaApplication.java
package com.ITclub.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * Created by damon on 2017/3/3. */ @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args)throws Exception{ SpringApplication.run(EurekaApplication.class,args); } }
server 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> <groupId>spring_cloud_erueka_server</groupId> <artifactId>spring_cloud_erueka_server</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR3</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
server applicatiom.yml:
server: port: 10001 eureka: instance: hostname: localhost client: register-with-eureka: false #当前服务不需要在Eureka server注册 fetch-registry: false #是否从eureka服务器获取注册信息 serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址

Client EurekaClientApplication.java :
package com.ITclub.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; /** * Created by damon on 2017/3/4. */ @SpringBootApplication @EnableDiscoveryClient public class EurekaClientApplication { public static void main(String[] args)throws Exception{ SpringApplication.run(EurekaClientApplication.class,args); } }
client bootstrap.yml:
spring: application: name: eurekaClient #服务名称 server: port: 10002 eureka: instance: non-secure-port: ${server.port:10002} #3 metadata-map: instanceId: ${spring.application.name}:${random.value} # 当前服务在eureka server 的ID client: register-with-eureka: true #当前服务需要在Eureka server注册 fetch-registry: false #是否eureka服务器获取注册信息 service-url: defaultZone: http://${eureka.host:localhost}:${eureka.port:10001}/eureka/ #Eureka server 的地址
client 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> <groupId>spring_cloud_erueka_service_provider</groupId> <artifactId>spring_cloud_erueka_service_provider</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-parent</artifactId> <version>Angel.SR3</version> <relativePath/> </parent> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
源码地址:EurekaServer