# SpringCloud 服务注册发现(Eureka)


SpringCloud 是一个基于SpringBoot实现的云应用开发工具,对项目中涉及的配置管理、服务发现、断路由、智能路由、微代理、控制总线、等操作进行一种简单的开发方式。

  • 微服务架构的特点:各个模块服务能能独立部署,可以做到高内聚低耦合。方便模块独自开发部署。

服务注册

每个注册单元向注册中心登记自己提供的服务,将自己的信息(主机、端口号)等信息登记到注册中心。比如有个A模块需要的服务有198.168.111.1:8001 198.168.111.2:8002B模块需要的服务有198.168.111.3:8003 198.168.111.4:8004那么注册中心将会登记如下的信息。

服务名位置
A模块的服务198.168.111.1:8001 198.168.111.2:8002
B模块的服务198.168.111.3:8003 198.168.111.4:8004

服务发现

服务间的调用不在通过地址去调用,使用服务名进行请求调用。服务调用方在调用服务提供接口时,是不知道具体的服务实例位置。因此调用方需要向注册中心咨询,并获取所有实例的清单,以实现对具体服务实例的访问。

Spring Cloud Eureka

Spring Cloud Eureka来实现服务治理。它为Spring Boot应用提供了自配置的Netflix OSS整合。通过一些简单的注解,开发者就可以快速的在应用中配置一下常用模块并构建庞大的分布式系统。它主要提供的模块包括:服务发现(Eureka),断路器(Hystrix),智能路由(Zuul),客户端负载均衡(Ribbon)等。

服务端 EureKa Server
  • 在项目中EureKa Server模块添加EureKa的依赖
   <!-- EureKa依赖包 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-eureka-server</artifactId>
    <version>1.4.7.RELEASE</version>
</dependency>
  • SprinbgBoot启动类上加上注解@EnableEurekaServer。提供给其他应用进行会话。
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class);
    }
}

  • EureKa配置,禁止EureKa客户端自己注册自己,配置内容如下。
#    端口号
server:
  port: 8001
spring:
  application:
    name: eureka-provider
eureka:
  instance:
    hostname: localhost # Eureka访问地址
  client:
    register-with-eureka: false # 禁止自己注册自己
    fetch-registry: false
    service-url:
     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 浏览器访问localhost:8001/ 可以看到我们定义的服务注册成功了。
    C:\Users\MarLonBrando\AppData\Roaming\Typora\typora-user-images
模块作为Eureka的客户端进行注册
  • 引入相关依赖
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>2.0.2.RELEASE</version>
    </dependency>
</dependencies>
  • 模块启动类加上注解、也可以不加
@SpringBootApplication
@EnableEurekaClient
public class ClientOneApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientOneApplication.class,args);
    }
}
  • 模块配置,配置Eureka 注册地址信息
server:
  port: 5002
spring:
  application:
    name: springboot-clientone
eureka:
  client:
    service-url:
      defaultZone: http://localhost:5001/eureka/
  instance:
    prefer-ip-address: true
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值