第一篇: 服务的注册与发现(Eureka)

本文介绍了Eureka作为服务发现组件的配置与使用方法,包括如何将应用配置为Eureka服务器及客户端,以及如何通过@EnableEurekaServer和@EnableDiscoveryClient启用服务发现功能。
**eureka是一个高可用的组件,它没有后端缓存,每一个实例注册之后需要向注册中心发送心跳(因此可以在内存中完成),在默认情况下erureka server也是一个eureka client ,必须要指定一个 server。eureka server的配置文件appication.yml:
server:
port: 8761

eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/


[size=medium][color=red][b]通过eureka.client.registerWithEureka:false和fetchRegistry:false来表明自己是一个eureka server.[/b][/color][/size]

在EurekaServer类中我们加入如下代码:
 
 @SpringBootApplication
 @EnableEurekaServer
  public class EurekaServer {

    public static void main(String[] args) {

    SpringApplication.run(EurekaServer.class, args);

  }

  }

可以看到只需要使用@EnableEurekaServer注解就可以让应用变为Eureka服务器,这是因为spring boot封装了Eureka Server,让你可以嵌入到应用中直接使用。至于真正的EurekaServer是Netflix公司的开源项目,也是可以单独下载使用的。

  在application.properties配置文件中使用如下配置:
server.port=8761
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

其中server.port配置eureka服务器端口号。Eureka的配置属性都在开源项目spring-cloud-netflix-master中定义(spring boot连文档都没有,只能看源码了),在这个项目中有两个类EurekaInstanceConfigBean 和EurekaClientConfigBean,分别含有eureka.instance和eureka.client相关属性的解释和定义。从中可以看到
[list]
[*]registerWithEureka
表示是否注册自身到eureka服务器,因为当前这个应用就是eureka服务器,没必要注册自身,所以这里是false
[*]fetchRegistry
表示是否从eureka服务器获取注册信息,同上,这里不需要
[*]defaultZone
就比较重要了,是设置eureka服务器所在的地址,查询服务和注册服务都需要依赖这个地址
[/list]

2)让服务使用eureka服务器

  让服务使用eureka服务器,只需添加@EnableDiscoveryClient注解就可以了。回到我们在上篇文章中实现的cloud-simple-service微服务应用。在main方法所在的Application类中,添加@EnableDiscoveryClient注解。然后在配置文件中添加:

  eureka.client.serviceUrl.defaultZone=http\://localhost\:8761/eureka/

  spring.application.name=cloud-simple-service

  其中defaultZone是指定eureka服务器的地址,无论是注册还是发现服务都需要这个地址。application.name是指定进行服务注册时该服务的名称。这个名称就是后面调用服务时的服务标识符(这是服务发现的功能,我们在后面章节具体介绍)。当然,pom文件也需要增加:

  <dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-eureka</artifactId>

  </dependency>

[url]http://blog.youkuaiyun.com/forezp/article/details/69696915[/url]

[size=x-large][color=black][b]Spring Cloud依赖库[/b][/color][/size]
这里我会罗列下常用的一些依赖包,为了更好理解每个依赖所负责的区域,我还是用一张和之前类似的图来展示各块的功能:
[img]https://img-blog.youkuaiyun.com/20160726161105167?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center[/img]
[color=red][b]常用依赖包如下:[/b][/color]
spring-cloud-starter-parent 具备spring-boot-starter-parent同样功能并附加Spring Cloud的依赖  
spring-cloud-starter-config 默认的配置服务依赖,快速自动引入服务的方式,端口8888
spring-cloud-config-server/client 用户自定义配置服务的服务端/客户端依赖
spring-cloud-starter-eureka-server 服务发现的Eureka Server依赖
spring-cloud-starter-eureka 服务发现的Eureka客户端依赖
spring-cloud-starter-hystrix/zuul/feign/ribbon 断路器(Hystrix),智能路有(Zuul),客户端负载均衡(Ribbon)的依赖
angular-ui-router 页面分发路由依赖



springcloud eureka server 官方文档
[url]http://projects.spring.io/spring-cloud/spring-cloud.html#spring-cloud-eureka-server[/url]

springcloud eureka client 官方文档
[url]http://projects.spring.io/spring-cloud/spring-cloud.html#_service_discovery_eureka_clients[/url]

spring cloud 源代码学习
[url]https://github.com/forezp/SpringCloudLearning[/url]

Spring cloud实现服务注册及发现
[url]http://www.cnblogs.com/skyblog/p/5133752.html[/url]
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值