SpringCloud实战---第六篇:Eureka注册中心之服务注册

前言

说起来容易做起来难,一步一步都干完!!!

学习一定要自己动手搞一搞,不能只眼会。

学习笔记是跟着尚硅谷的视频学的:https://www.bilibili.com/video/BV18E411x7eT?p=1


场景大纲

我们以这样一个场景来学习、构建我们的微服务
在这里插入图片描述

服务注册中心Eureka

1. 概念

Eureka是cloud提供的服务治理框架,实现服务的统一调用和管理,当微服务接口数量上来之后使用传统的RestTemplate的方式调用会十分不便,也不便于我们统一的管理和监控,引入Eureka框架可以实现服务统一的发布和管理,Eureka包含Server和Client两个组件。
EurekaServer就是注册中心,服务向注册中心注册后,可以被其他使用方发现调用。EurekaClient就是服务提供方,服务可以将自己注册到注册中心EurekaServer。
在这里插入图片描述

  • EurekaServer提供注册服务

各个微服务节点通过配置启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中会存储所有可用服务节点的信息,可以通过web页面看到。

  • EurekaClient向注册中心注册自己提供的服务信息
    是一个Java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的、使用轮询负载均衡算法的负载均衡器,在应用启动后,会向EurekaServer发送心跳(默认30秒),如果server多个周期没有接收到某个节点的心跳,Server会从注册表中会把这个服务节点移除(默认90秒)。

单机版Eureka构建

注册中心EurekaServer端口7001,服务调用者端口80,服务提供者端口8001
在这里插入图片描述

1. 创建EurekaServer模块7001

  1. 创建模块,模块名
cloud-eureka-server7001
  1. 修改POM,添加依赖(注意:等待下载完依赖)
   <dependencies>
        <!--eureka-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!--boot web actuator-->
        <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>
        <!--一般通用配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
        </dependency>
    </dependencies>
  1. 添加application.yml配置文件,注意,由于7001是注册中心,所以不需要自己向自己注册
server:
  port: 7001
eureka:
  instance:
    hostname: localhost #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
    #集群指向其它eureka
      #defaultZone: http://eureka7002.com:7002/eureka/
    #单机就是7001自己
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  1. 在java下建立包
com.atguigu.springcloud

在这里插入图片描述

  1. 编写启动类,@EnableEurekaServer注解表示该启动类类是EurekaServer的注册中心服务(自己不用向自己注册)
package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @Author: Daisen.Z
 * @Date: 2021/12/17 18:00
 * @Version: 1.0
 * @Description:
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {

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

}
  1. 创建完成
    在这里插入图片描述
  2. 启动测试
    在这里插入图片描述
    访问 localhost:7001,能看到Eureka的web页面,创建完成
    在这里插入图片描述

2. 改造EurekaClient服务提供方8001

  1. 添加EurekaClient的依赖。
<!--eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在这里插入图片描述

  1. 添加服务名称配置
    向Eureka注册自身服务,需要告知注册中心本服务的名称, 这个名称在application.yml里面配置,之前我们添加配置时已经添加该项。
    在这里插入图片描述
  2. 添加EurekaClient配置
eureka:
  client:
    register-with-eureka: true     #false表示不向注册中心注册自己。
    fetch-registry: true     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #表示Eureka的地址
      defaultZone: http://localhost:7001/eureka/

在这里插入图片描述

  1. 修改启动类,改为添加EurekaClient标识注解
@EnableEurekaClient

在这里插入图片描述

  1. 启动测试

在这里插入图片描述
访问页面

http://localhost:7001/

cloud-payment-service已经注册进来,其中服务名字是我们配置文件中配的,转成了大写显示。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3. 改造EurekaClient服务提供方80

  1. 添加EurekaClient的依赖。
<!--eureka-client-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

在这里插入图片描述

  1. 修改80的配置文件,添加如下配置,注意首行缩进要保持一直
spring:
  application:
    name: cloud-order-service # 本服务的名称

eureka:
  client:
    register-with-eureka: true     #false表示不向注册中心注册自己。
    fetch-registry: true     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
      #表示Eureka的地址
      defaultZone: http://localhost:7001/eureka/

在这里插入图片描述

  1. 修改启动类,改为添加EurekaClient标识注解

在这里插入图片描述
order服务也注册进来了
在这里插入图片描述

  1. 只调用服务可以不将自己注册到注册中心上,通过修改配置文件可以更正,修改register-with-eureka配置,ctrl+s让热部署自动重启。

在这里插入图片描述
刷新页面,order服务消失啦
在这里插入图片描述


小结

  • Eureka包含Server和Client两个组件。

  • EurekaServer就是注册中心,服务向注册中心注册后,可以被其他使用方发现调用。

  • EurekaClient就是服务提供方和服务调用方,服务可以将自己注册到注册中心。

  • 改register-with-eureka配置控制是否将自身注册到Eureka。

  • EurelkaServer端要给启动类添加@EnableEurekaServer注解。

  • EurekaClient端要给启动类添加@EnableEurekaClient注解。

  • 1
    点赞
  • 1
    收藏
  • 打赏
    打赏
  • 0
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:博客之星2021 设计师:Hiro_C 返回首页
评论

打赏作者

老司机张师傅

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

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值