Java知网

  • 首页
  • Spring Boot
  • 面试精选
  • 程序人生
  • 资源
  • 友链
  • 关于我
  1. 首页
  2. Spring Cloud
  3. 正文

Spring Cloud 注册中心 Eureka

2021年4月16日 251点热度 0人点赞 0条评论

一、Eureka的简介

1.1 什么是服务治理?

在传统的rpc远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理起来比较复杂,所以需要服务治理,管理服务与服务之间的调用关系,可以实现服务调用、负载均衡、容错等,实现服务注册与发现。

1.2 什么是服务注册与发现

Eureka Server是一个注册中心系统,服务提供者可以将自己的服务注册到注册中心,消费者可以在注册中心发现服务。

二、组成以及原理

三、搭建Eureka单机版

3.1 注册中心

3.1.1 Maven

<dependencies>
    <!-- eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--监控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

3.1.2 application.yml

eureka:
  instance:
    hostname: localhost
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false

3.1.3 Application

添加@EnableEurekaServer注解即可

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

3.1.4 测试

访问 http://localhost:8080

3.2 服务提供者-provider

3.2.1 Maven

 <!--eureka client-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.2.2 applicaition.yml

spring:
  application:
    name: cloud-eureka-provide-userinfo-service
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #单机版 这里填写注册中心的路径 server:port/eureka/
      defaultZone: http://localhost:8080/eureka/
  instance:
    prefer-ip-address: true

3.2.3 Application

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

3.2.4 测试

3.3 消费者-consumer

3.3.1 Maven

 <!--eureka client-->
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

3.3.2 application.yml

spring:
  application:
    name: cloud-eureka-consumer-useradmin 
eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8080/eureka/

3.3.3 Application

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

3.3.4 配置RestTemplate

如果需要使用注册中心服务提供者的名字来调用接口的话,需要添加 @LoadBalanced 开启负载均衡才能正常使用。不然会报出UnknownHostException 的调用异常,一定要@loadBalance注解修饰的restTemplate才能实现服务名的调用,没有修饰的restTemplate是没有该功能的。

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced  // 没有加这个的话没有办法使用 服务名 来调用接口
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }    
}

3.3.5 调用服务

因为统一使用了Eureka作为注册中心,所以我们可以直接使用服务名来调用相关的服务,所以不需要知道服务部署在什么地方,只需要固定好 服务名 即可。

@Resource
RestTemplate restTemplate;

String URL_PREFIX = "http://CLOUD-EUREKA-PROVIDE-USERINFO-SERVICE"; 

@GetMapping("/getUser/{userId}")
public BaseResponse getUser(@PathVariable String userId){
  ResponseEntity<BaseResponse> forEntity = restTemplate.getForEntity(URL_PREFIX+"/user/getUserById/" + userId, BaseResponse.class);
  return forEntity.getBody() ;
} 

四、搭建Eureka集群

要想实现服务的高可用,避免不了要使用集群,注册中心可以集群,服务也可以集群

4.1 注册中心集群

4.1.1 配置集群

相互注册,相互守望。

要实现注册中心集群,则需要彼此相互注册,用到的理念是相互注册,相互守望。实现的方式也是非常简单的,只是需要修改配置文件即可:

例如目前我搭建了两个注册中心,分别是localhost:8080和localhost:8090,则我们需要再8080上注册8090,同理,需要在8090上注册8080。

  • 8080
server:
  port: 8080 
eureka:
  instance:
    hostname: localhost8080   # 为了更好的区分,这里修改了hostname
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    # 下面需要注册需要守望的注册中心,多个用逗号隔开即可
    service-url:
      defaultZone: http://localhost:8090/eureka/
  • 8090
server:
  port: 8090 
eureka:
  instance:
    hostname: localhost8090 # 为了更好的区分,这里修改了hostname
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要检索服务
    fetch-registry: false
    # 下面需要注册需要守望的注册中心,多个用逗号隔开即可
    service-url:
      defaultZone: http://localhost:8080/eureka/

直接启动即可,启动之后,访问任何一个注册中心的页面就可以看到DS Replicas上可以看到守望的注册中心了,因为我这里测试全部部署在本机,所以不太方便看出来,如果在不同的域下就容易看出来了。

4.1.2 将服务注册到集群

将服务注册到集群中,只需要修改eureka.client.service-url.defaultZone即可,单机版的defaultZone只有一个注册中心的url,集群版的只需要用逗号隔开多个注册中心url即可。

例如:

eureka:
  client:
    #表示是否将自己注册进EurekaServer默认为true
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册消息,默认为true,单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetch-registry: true
    service-url:
      #集群版,需要用逗号将注册中心分开
      defaultZone: http://localhost:8080/eureka/,http://localhost:8090/eureka/
  instance:
    prefer-ip-address: true

4.2 服务提供者集群

要实现服务提供者的集群也是非常的简单,只需要保证应用(服务)名称一致,然后注册到同样的注册中心即可。

五、Eureka的自我保护

某时刻某一个微服务不可用了,Eureka不会立刻清理,依旧会对该微服务的信息进行保存,这是属于CAP里面的AP分支。

5.1 配置

  • 注册中心关闭自我保护
eureka.server.enable-self-preservation = false
  • 生产者客户端配置心跳
# 下面单位是秒
# 发送心跳的时间间隔
eureka.instance.lease-renewal-interval-in-seconds=30
# 注册中心等待时间上限
eureka.instance.lease-expiration-duration-in-seconds=90

六、Eureka停更进维

官方说明

作者:Martain
来源:https://www.jianshu.com/p/8f3e20e91860

相关文章:

  1. Spring Cloud Stream 消息驱动
  2. Spring Cloud Bus 实时刷新配置
标签: Spring Cloud
最后更新:2021年9月4日

javatip

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

取消回复
搜一搜
扫一扫
    关注公众号
  • 技术干货推送
  • 免费资料领取
  • 定时福利发放
分类
  • Java / 110篇
  • Mysql / 17篇
  • Redis / 10篇
  • Spring Boot / 29篇
  • Spring Cloud / 16篇
  • 消息队列 / 14篇
  • 程序人生 / 21篇
  • 资源 / 3篇
  • 面试 / 23篇
归档
  • 2022年4月 / 1篇
  • 2022年1月 / 1篇
  • 2021年12月 / 9篇
  • 2021年11月 / 2篇
  • 2021年9月 / 10篇
  • 2021年8月 / 4篇
  • 2021年7月 / 2篇
  • 2021年6月 / 10篇
  • 2021年5月 / 18篇
  • 2021年4月 / 75篇
  • 2021年3月 / 78篇

COPYRIGHT © 2021 javatip.cn. ALL RIGHTS RESERVED.

陇ICP备19004310号-2

甘公网安备 62010202003150号