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

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

1.1服务发现组件的功能
  • 服务注册表:
    服务注册表是一个记录当前可用服务实例的数据库,是服务发现机制的核心。服务注册表提供查询API和管理API,通过查询Api可以查看当前可用的服务实例,通过管理Api可以注册或注册实例。
  • 服务注册:
    就是服务在启动时,将服务实例的网络地址注册到服务注册表中
  • 健康检查:
    服务注册表会通过某些机制(如心跳监测)定期检查已注册的服务,如果发现某个服务不可用,就将其从注册表中移除。
  • 服务注册组件:
    不同人的称谓可能不同,不过说的都是一个组件,有的称为注册中心,服务注册,服务发现,在这里统一称为服务注册发现组件(如Dubbo使用Zookeeper,Edas使用阿里自己定制的AliTomcat,我们这里使用Netfix提供的Eureka,当然springcloud也支持其它的注册发现组件)。
1.2服务注册发现方式

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

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

今天介绍的内容均可以查看这里的中文文档:https://springcloud.cc/spring-cloud-dalston.html#_spring_cloud_netflix

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
@EnableEurekaServer
public class MicroserviceDiscoveryEurekaApplication {
    public static void main(String[] args) {
    SpringApplication.run(MicroserviceDiscoveryEurekaApplication.class, args);
    }
}

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

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

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

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
@EnableEurekaClient
public class MicroserviceProviderUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroserviceProviderUserApplication.class, args);
    }
}

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

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

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

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: qwe123
server:
  port: 8761
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://admin:qwe123@localhost:8761/eureka

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

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: 7901
user:
  userServicePath: http://localhost:7900/simple/
spring:
  application:
    name: microservice-consumer-movie            #配置项目名
eureka:
  client:             #配置要访问的注册中心地址,即eureka所在的地址
    healthcheck:
      enabled: true
    service-url:
      defaultZone: http://admin:qwe123@localhost:8761/eureka
  instance:           #配置是否显示ip
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.cloud.client.ipAddress}:${spring.application.instance_id:${server.port}}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值