龙空技术网

SpringCloud将服务者和提供者注册到注册中心(Eureka)

顶呱呱啊顶呱呱 775

前言:

目前各位老铁们对“netfix 注册”大体比较注意,咱们都想要了解一些“netfix 注册”的相关资讯。那么小编在网摘上网罗了一些有关“netfix 注册””的相关文章,希望各位老铁们能喜欢,同学们快快来了解一下吧!

1.服务注册和发现的一些术语解释

1.1服务发现组件的功能

服务注册表: 服务注册表是一个记录当前可用服务实例的数据库,是服务发现机制的核心。服务注册表提供查询API和管理API,通过查询Api可以查看当前可用的服务实例,通过管理Api可以注册或注册实例。

服务注册: 就是服务在启动时,将服务实例的网络地址注册到服务注册表中

健康检查: 服务注册表会通过某些机制(如心跳监测)定期检查已注册的服务,如果发现某个服务不可用,就将其从注册表中移除。

服务注册组件: 不同人的称谓可能不同,不过说的都是一个组件,有的称为注册中心,服务注册,服务发现,在这里统一称为服务注册发现组件(如Dubbo使用Zookeeper,Edas使用阿里自己定制的AliTomcat,我们这里使用Netfix提供的Eureka,当然springcloud也支持其它的注册发现组件)。

1.2服务注册发现方式

不同的框架用的组件可能不能,不过总的来说有客户端发现和服务端发现,Eureka和Zookeeper使用客户端发现方式,Consul属于服务端发现方式。

2.将服务端和客户端注册到Eureka注册组件中

今天介绍的内容均可以查看这里的中文文档: 。

2.1创建Eureka服务注册组件项目

这里的注册中心跟zookeeper不同,zookeeper启动之后再启动服务即可注册实例,我们这里需要自己开发注册中心项目。使用IDEA创建名为microservice-discovery-eureka的项目,pom中引入 spring-cloud-starter-eureka-server的jar包。

 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <version>1.3.4.RELEASE</version> </dependency>

在MicroserviceDiscoveryEurekaApplication方法中添加注解@EnableEurekaServer,表明这是一个服务注册组件的服务端

@SpringBootApplication@EnableEurekaServerpublic class MicroserviceDiscoveryEurekaApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args); }}

将application.porperties注册文件改名为application.yml,之后进行配置。

server: port: 8761eureka: client: fetch-registry: false register-with-eureka: false service-url: defaultZone: 

Eureka的默认端口就是8761,其中“defaultZone”是一个魔术字符串后备值,为任何不表示首选项的客户端提供服务URL(即defaultZone是有用的默认值)。现在就可以启动这个Main方法,之后再浏览器中输入 ,打开页面如下:

2.2将服务者注册到Eureka中

首先引入jar包spring-cloud-starter-eureka,这里引的跟上面引的jar不一样,上面的服务端,这里的是客户端。

 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> <version>1.3.4.RELEASE</version> </dependency>

在之前的main方法中添加@EnableEurekaClient注解,表明它是客户端。

@SpringBootApplication@EnableEurekaClientpublic class MicroserviceProviderUserApplication { public static void main(String[] args) { SpringApplication.run(MicroserviceProviderUserApplication.class, args); }}

修改application.yml配置文件,配置应用实例名称,应用访问方式等。

server: port: 7900spring: jpa: generate-ddl: false show-sql: true hibernate: ddl-auto: none datasource: platform: h2 schema: classpath:schema.sql data: classpath:data.sql application: name: microservice-provider-user #应用名称eureka: client: #配置eureka客户端 service-url: defaultZone:  instance: prefer-ip-address: true #配置在eureka中显示ip地址 instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}} #配置应用访问方式。应用名:IP:端口号

现在启动服务者项目即可将服务注册到Eureka中,刷新eureka的页面可看到注册的服务实例。

2.3将调用者注册到Eureka中

调用者和服务者注册到Eureka的方式一样,首先引入spring-cloud-starter-eureka的jar包,之后给main方法配置@EnableEurekaClient注解,再修改application.yml的配置文件,不再赘余。

server: port: 7901spring: application: name: microservice-consumer-movie #配置项目名eureka: client: #配置要访问的注册中心地址,即eureka所在的地址 service-url: defaultZone:  instance: #配置是否显示ip prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

至此服务者和调用者均已经注册到服务注册组件Eureka中了,刷新 即可查看,至于如何调用,下一节介绍

3.补充知识

补充身份认证和健康检查。

3.1身份认证

在Eureka的项目中引入springboot提供的安全认证jar包,服务者和调用者不用引入。

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>1.3.4.RELEASE</version> </dependency>

在application.yml中配置要访问的用户名和密码,注册中心,服务者,消费者均要修改访问路径跟Eureka中defaultZone的一致。

security: basic: enabled: true user: name: admin password: qwe123server: port: 8761eureka: client: fetch-registry: false register-with-eureka: false service-url: defaultZone: 

这样在访问 的时候就需要输入用户名和密码了。

3.2健康检查

微服务中要想保证服务可用就必须要保证每个服务均是可用的,Eureka中提供了健康检查机制,检测到某个服务不可用会将其从注册表中移除,下面介绍如何进行健康检查:

在每一个需要注册到注册中心的服务项目中都引入spring-boot-starter-actuator的jar包(Eureka项目中不引入)

 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>1.3.4.RELEASE</version> </dependency>

在每个要注册到注册中心的项目的application.yml中都加入 healthcheck:enabled: true的配置(Eureka的配置文件不加)。示例如下:

server: port: 7901user: userServicePath: : application: name: microservice-consumer-movie #配置项目名eureka: client: #配置要访问的注册中心地址,即eureka所在的地址 healthcheck: enabled: true service-url: defaultZone:  instance: #配置是否显示ip prefer-ip-address: true instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

这样在某个服务不可用是就会从注册表中剔除。

标签: #netfix 注册