SpringCloud——Eureka服务注册和发现
2017-12-20 14:27
573 查看
一、SpringCloud和Dubbo
SpringCloud整合了一套较为完整的微服务解决方案框架,而Dubbo只是解决了微服务的几个方面的问题。content | Dubbo | SpringCloud |
---|---|---|
服务注册中心 | zookeeper | Spring Cloud Netflix Eureka |
服务调用方式 | RPC | REST API |
服务网关 | 无 | Spring Cloud Netflix Zuul |
断路器 | 不完善 | Spring Cloud Netflix Hystrix |
分布式配置 | 无 | Spring Cloud Config |
服务跟踪 | 无 | Spring Cloud Sleuth |
消息总线 | 无 | Spring Cloud Bus |
数据流 | 无 | Spring Cloud Stream |
批量任务 | 无 | Spring Cloud Task |
二、Demo
今天介绍的服务发现是在SpringCloud的子项目Netflix中,除此之外,他还提供了熔断器、负载均衡、智能路由等,之后会介绍到。和往常一样,我们先来实现这个实例,然后再分析。这里,我们需要一个服务注册中心(即下面例子中的eureka-server)和一个服务的提供方(eureka-provider)。
1、服务注册中心
1)、引入pom文件
<!--基于Springboot--> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath/> </parent> <properties> <!--设置字符编码及java版本--> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!--增加eureka-server的依赖--> <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> <!--依赖管理,用于管理spring-cloud的依赖,其中Camden.SR3是版本号--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2)、配置
新建application.properties,注意名称只能是这个,不然不会被识别。server.port=8761 #注册中心默认端口就是8761,也可通过下面的方式定义其他端口 #eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/ eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false1
2
3
4
5
3)、启动类
@EnableEurekaServer //启动一个服务注册中心提供给其他应用进行对话 @SpringBootApplication public class ServerApplication { public static void main(String[] args) { //下面两行代码都可以用来启动 SpringApplication.run(ServerApplication.class, args); //new SpringApplicationBuilder(Application.class).web(true).run(args); } }
b62b
1
2
3
4
5
6
7
8
9
4)、测试
在浏览器中输入http://localhost:8761 就会显示:2、服务提供者
1)、pom文件跟注册中心类似,只有eureka有区别:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>1
2
3
4
2)、配置文件
#应用(服务)名称 spring.application.name=compute-service server.port=8762 #注册中心地址 eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/1
2
3
4
5
6
7
3)、Java Code
Controller中,通过DiscoveryClient发现服务。启动类
@EnableDiscoveryClient //激活eureka中的DiscoveryClient实现 @SpringBootApplication public class ComputeServiceApplication { public static void main(String[] args) { new SpringApplicationBuilder(ComputeServiceApplication.class).web(true).run(args); } }1
2
3
4
5
6
7
4)、测试
在浏览器中输入http://localhost:8762/hello?name=Sherry或http://yfywyangsx.hiersun.com:hello-service:8762/hello?name=Sherry
##
三、FAQ
1、Eureka的高可用方案 上面的例子中,Eureka只有一个8761的注册中心,那么如何避免单点问题呢?
我们可以采用集群的方式来解决。 比如现在有三台机器:Server1、Server2和Server3.在高可用方案中,三台机器要两两注册。比如S1要向S2、S3分别进行注册,目前他无法实现注册的传递性。 这样一来,如果Server1宕机,我们还可以继续从Server2和3中获取服务。
2、为什么不用zookeeper做注册中心 在使用dubbo时,一般都结合zk(作为注册中心)来使用。那为什么SpringCloud中使用Eureka,而不是zk呢?
我们来比较一下,在CAP理论中,zk更看重C和P,即一致性和分区容错性。但Eureka更在意的是A和P,A为高可用。zk中有master和follower区别,当进入选举模式时,就无法正常对外提供服务。但Eureka中,集群是对等的,地位是相同的,虽不能保证一致性,但至少可以提供注册服务。 根据不同的业务场景,各有取舍吧。
相关文章推荐
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- 微服务架构 SpringCloud(二)Eureka(服务注册和服务发现基础篇)
- SpringCloud : 服务的注册与发现(Eureka)
- SpringCloud(3)---Eureka服务注册与发现
- SpringCloud入门教学|第一篇:服务的注册和发现(Eureka)
- SpringCloud服务注册与发现(Eureka 注册中心)
- 【微服务架构】SpringCloud之Eureka(服务注册和服务发现基础篇)(二)
- 【Spring Cloud】--SpringCloud的服务注册与发现【Eureka】
- 《Spring Cloud Netflix》--服务注册和服务发现-Eureka的深入了解
- SpringCloud之注册与发现服务(Eureka)|第一章-yellowcong
- SpringCloud 学习记录(一):服务注册与发现(eureka+feign)
- 服务注册发现Eureka之一:Spring Cloud Eureka的服务注册与发现
- Spring Cloud Eureka 服务注册与发现
- Spring cloud Eureka服务注册及发现(一)创建Eurake服务器
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
- Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】
- springcloud(第一篇)springcloud eureka 服务注册与发现
- SpringCloud--Eureka服务注册和发现
- Spring Cloud eureka 注册服务发现,访问管理页面是xml问题解决
- SpringCloud——服务注册与发现Eureka以及注册源码解析