SpringCloud微服务(01):Eureka组件,管理服务注册与发现

Easter79
• 阅读 514

一、Eureka基本架构

1、Eureka角色结构图

SpringCloud微服务(01):Eureka组件,管理服务注册与发现

角色职责如下:

1)、Register:服务注册中心,它是一个Eureka Server ,提供服务注册和发现功能。

2)、Provider:服务提供者,它是一个Eureka Client ,提供服务。

3)、Consumer:服务消费者,它是一个Eureka Cient ,消费服务。

2、Eureka中几个核心概念

1)、Registe服务注册

当Client向Server 注册时,Client 提供自身的元数据,比如IP 地址、端口、运行状况指标的Uri 、主页地址等信息。

2)、Renew服务续约

Client 在默认的情况下会每隔30 秒发送一次心跳来进行服务续约。通过服务续约来告知Server该Client仍然可用。正常情况下,如果Server在90 秒内没有收到Client 的心跳,Server会将Client 实例从注册列表中删除。官网建议不要更改服务续约的间隔时间。

3)、Fetch Registries获取服务注册列表信息

Client 从Server 获取服务注册表信息,井将其缓存在本地。Client 会使用服务注册列表信息查找其他服务的信息,从而进行远程调用。该注册列表信息定时(每30 秒) 更新一次。

4)Cancel服务下线

Client 在程序关闭时可以向Eureka Server 发送下线请求。发送请求后,该客户端的实例信息将从Server 的服务注册列表中删除。该下线请求不会自动完成,需要在程序关闭时调用以下代码:

DiscoveryManager.getinstance().shutdownComponent();

5) Eviction服务下线

在默认情况下,当Client 连续90 秒没有向Server 发送服务续约(即心跳〉时,Server 会将该服务实例从服务注册列表删除,即服务下线。

二、Eureka案例代码

1、项目基本结构图

主要包括两个注册中心(集群)
node01-eureka-7001
node01-eureka-7002
一个服务提供方
node01-provider-8001
一个服务消费方
node01-consume-8002

2、配置本机的Host文件

# cloud host
127.0.0.1 registry01.com
127.0.0.1 registry02.com
127.0.0.1 provider-8001.com

3、注册中心代码分解

1)Eureka注册中心依赖

<dependencies>
    <!--eureka-server服务端 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
    </dependency>
</dependencies>

2)核心配置文件

server:
  port: 7001
spring:
  application:
    name: node01-eureka-7001
eureka:
  instance:
    hostname: registry01.com
    prefer-ip-address: true
  client:
    # false表示不向注册中心注册自己
    register-with-eureka: false
    # false表示该端就是注册中心,维护服务实例,不去检索服务
    fetch-registry: false
    service-url:
      # 单点注册中心
      # defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
      # 集群注册中心
      defaultZone: http://registry02.com:7002/eureka/

这里采用集群的配置,如果有多个集群,逗号分隔,如下写法就好:

defaultZone: 
http://registry02.com:7002/eureka/,
http://registry02.com:7002/eureka/

3)启动类注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer // 注册中心注解
public class Application_7001 {
    public static void main(String[] args) {
        SpringApplication.run(Application_7001.class,args) ;
    }
}

这样注册中心代码完成。

4)启动项目,如图

SpringCloud微服务(01):Eureka组件,管理服务注册与发现

暂时没有服务注册进来。

4、服务提供方代码分解

1)核心依赖

<dependencies>
    <!-- 将微服务provider侧注册进eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>
</dependencies>

2)配置文件如下

server:
  port: 8003
spring:
  application:
    name: node01-provider-8001
eureka:
  instance:
    hostname: provider-8001
    prefer-ip-address: true
  client:
    service-url:
      # 集群注册中心
      defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

3)提供一个接口服务

@RestController
public class ProviderController {
    @RequestMapping("/getInfo")
    public Map<String,String> getInfo (){
        Map<String,String> infoMap = new HashMap<>() ;
        infoMap.put("作者:","知了一笑") ;
        infoMap.put("时间:","2019-05-18") ;
        infoMap.put("主题:","SpringCloud微服务框架") ;
        return infoMap ;
    }
}

4)启动类上需要注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient    // 本服务启动后会自动注册进eureka服务中
@EnableDiscoveryClient
public class Application_8001 {
    public static void main(String[] args) {
        SpringApplication.run(Application_8001.class,args) ;
    }
}

5、服务消费方代码分解

服务消费方和提供方的代码逻辑基本一致。 1)配置文件 这里不需要注册自己,只是单纯从注册中心获取服务提供方的消息。

server:
  port: 8002
spring:
  application:
    name: node01-consume-8002
eureka:
  client:
    register-with-eureka: false
    service-url:
      # 集群注册中心
      defaultZone: http://registry01.com:7001/eureka/,http://registry02.com:7002/eureka/

2)消费服务代码 注意这里的【server_name】就服务提供方的

spring:
  application:
    name: node01-provider-8001

使用RestTemplate调用服务。

@RestController
public class ConsumeController {
    @Autowired
    private RestTemplate restTemplate ;
    String server_name = "node01-provider-8001" ;
    @RequestMapping("/showInfo")
    public Map<String,String> showInfo (){
        return restTemplate.getForObject("http://"+server_name+":8001/getInfo",Map.class) ;
    }
}

到这里,一个基于Eureka的服务注册与发现就完成了。 3)四个服务全部启动

SpringCloud微服务(01):Eureka组件,管理服务注册与发现

4)访问如下地址

http://localhost:8002/showInfo

获取服务接口结果

{
    "主题:": "SpringCloud微服务框架",
    "作者:": "知了一笑",
    "时间:": "2019-05-18"
}

三、源代码案例

GitHub地址:知了一笑
https://github.com/cicadasmile
码云地址:知了一笑
https://gitee.com/cicadasmile

SpringCloud微服务(01):Eureka组件,管理服务注册与发现 SpringCloud微服务(01):Eureka组件,管理服务注册与发现

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Wesley13 Wesley13
2年前
Spring Cloud Eureka源代码解析(1)Eureka启动,原生启动与SpringCloudEureka启动异同
Eureka作为服务注册中心对整个微服务架构起着最核心的整合作用,因此对Eureka还是有很大的必要进行深入研究。Eureka1.x版本是纯基于servlet的应用。为了与springcloud结合使用,除了本身eureka代码,还有个粘合模块springcloudnetflixeurekaserver。在我们启动EurekaServer实例
Easter79 Easter79
2年前
SpringCloud常用组件
springcloud中有五大核心组件Eureka、Ribbon、Feign、Hystrix、Zuul,简单记录如下。Eureka是微服务架构中的注册中心,专门负责服务的注册与发现。EurekaClient组件专门负责将服务的信息注册到EurekaServer中,而EurekaServer是一个注册中心,里面有一个注册表,保存了各服务所在
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
2年前
Golang注册Eureka的工具包goeureka发布
1.简介提供Go微服务客户端注册到Eureka中心。点击:github地址(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fgithub.com%2FSimonWang00%2Fgoeureka),欢迎各位多多star!(已通过测试验证,用于正式生产部署)2.原理
Easter79 Easter79
2年前
SpringCloud Eureka服务治理机制
一、基础架构!(https://oscimg.oschina.net/oscnet/c088a917c16ee8be06202e47bd73e50a7a0.png)构建Eureka服务治理有三个核心角色:服务注册中心、服务提供者和服务消费者。上图就是这三个角色之间的通信工作架构图。服务注册中心(Eureka 
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k