SpringBoot整合eureka简记

2023-10-30

Eureka是一个服务治理组件,它主要包括服务注册和服务发现,主要用来搭建服务注册中心。

Eureka 是一个基于 REST 的服务,用来定位服务,进行中间层服务器的负载均衡和故障转移;

Eureka是Netflix 公司开发的,Spring Cloud发现eureka很好使,因此将eureka封装到自己的模块中。

 

1、要使用eureka,首先要创建一个服务,eureka本身也是一个微服务

引入springcloud和eureka-server

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.4.4.RELEASE</version>
    </dependency>

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

<dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>

</dependencyManagement>

这个地方有一个坑,我折腾了半天才启动成功。springboot和springcloud是有对应关系的

SpringCloud版本 SpringBoot版本
2021.0.1-SNAPSHOT Spring Boot >=2.6.4-SNAPSHOT and <2.7.0-M1
2021.0.0 Spring Boot >=2.6.1 and <2.6.4-SNAPSHOT
2021.0.0-RC1 Spring Boot >=2.6.0-RC1 and <2.6.1
2021.0.0-M3 Spring Boot >=2.6.0-M3 and <2.6.0-RC1
2021.0.0-M1 Spring Boot >=2.6.0-M1 and <2.6.0-M3
2020.0.5 Spring Boot >=2.4.0.M1 and <2.6.0-M1
Hoxton.SR12 Spring Boot >=2.2.0.RELEASE and <2.4.0.M1
Hoxton.BUILD-SNAPSHOT Spring Boot >=2.2.0.BUILD-SNAPSHOT
Hoxton.M2 Spring Boot >=2.2.0.M4 and <=2.2.0.M5
Greenwich.BUILD-SNAPSHO Spring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4
Greenwich.SR2 Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT
Greenwich.M1 Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE
Finchley.BUILD-SNAPSHOT Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3
Finchley.SR4 Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT
Finchley.RC2 Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE
 Finchley.RC1  Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE
Finchley.M9 Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE
Finchley.M7 Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2
Finchley.M6 Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1
Finchley.M5 Spring Boot >=2.0.0.M7 and <=2.0.0.M7
Finchley.M4 Spring Boot >=2.0.0.M6 and <=2.0.0.M6
Finchley.M3 Spring Boot >=2.0.0.M5 and <=2.0.0.M5
Finchley.M2 Spring Boot >=2.0.0.M3 and <2.0.0.M5
Edgware.SR5 1.5.20.RELEASE
Edgware.SR5 1.5.16.RELEASE
Edgware.RELEASE 1.5.9.RELEASE
Dalston.RC1 1.5.2.RELEASE

为eureka服务增加注解说明

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {

        SpringApplication.run(EurekaApplication.class, args);
    }
}

为eureka服务增加配置

server:

  port: 9000
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka

 

这里我使用9000端口作为eureka的服务端口 

启动后可以看到eureka服务的展示页面,可以看到,这时候是没有服务注册进来的。

 2、创建微服务注册到eureka服务

pom文件,简化版,其他springcloud和springboot的引用和eureka相同

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

服务启动类

@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {


    public static void main(String[] args) {

        SpringApplication.run(OrderApplication.class, args);
    }
}

配置文件

server:

  port: 1001
eureka:
  instance:
    prefer-ip-address: true

    hostname: localhost
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:9000/eureka/
spring:
  application:
    name: orderservice

这里我使用1001作为第一个微服务 端口号,下边使用1002创建另一个微服务,配置同理

启动后

 可以看到,两个服务均已经成功注册到eureka服务。

3、创建另一个微服务去调用已经注册进去的服务,配置相同,这里只截图调用相关代码

@Autowired
RestTemplate restTemplate;

@RequestMapping("indexClient.do")

public @ResponseBody String indexClient() {

     return restTemplate.getForEntity("http://orderservice/index.do", String.class).getBody();

}

使用的是RestTemplate,RestTemplate简化了发起 HTTP 请求以及处理响应的过程,并且支持 REST,类似于自己创建的http请求类。

这里有个小坑,如果clean服务后配置文件application.yml有可能会被从target中删除,这里需要手动复制到target中,不然会启动失败,而且配置的server端口不生效,谨记。

为了不至于学习资料丢失,代码已经上传至以下地址:

(3条消息) springboot整合eureka源代码,学习资料-Java文档类资源-CSDN文库

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot整合eureka简记 的相关文章

随机推荐

  • RGB与YCbCr

    1 RGB RGB色彩模式是工业界的一种颜色标准 是通过对红 R 绿 G 蓝 B 三个颜色通道的变化以及它们相互之间的叠加来得到各式各样的颜色的 RGB即是代表红 绿 蓝三个通道的颜色 这个标准几乎包括了人类视力所能感知的所有颜色 是目前运
  • 成功解决Array Index Out Of Bounds Exception问题

    成功解决Array Index Out Of Bounds Exception问题 目录 解决问题 解决方法 解决问题 Array Index Out Of Bounds Exception 解决方法 数组索引越界异常 即数组下标号超出数组
  • Spring Cache --- @Cacheable/@CachePut/@CacheEvict注解的原理深度剖析和使用

    这里只是部分 需要深入了解可以看原著 原文链接 https blog csdn net f641385712 article details 94570960 关于Spring的缓存注解 一共有如下5个 1 Cacheable 缓存 sin
  • 实战:10 种实现延迟任务的方法,附代码!

    捡田螺的小男孩 2022 08 04 07 45 发表于广东 以下文章来源于Java中文社群 作者磊哥 Java中文社群 Java实用文章聚集地 程序员田螺 专注分享后端面试题 包括计算机网络 MySql数据库 Redis缓存 操作系统 J
  • 新Kubeflow,新征程 (一):简化部署体验

    综述 2017年底 奥斯汀的Kubecon上 Google项目的产品经理 David Aronchick 和首席工程师 Jeremy Lewi将Kubeflow这个项目带到了大家的视野里 经过一年的发展 在2018年的西雅图的Kubecon
  • Python中selenium实现文件上传所有方法整理总结

    pyAutoGUI官方文档 https pyautogui readthedocs io en latest 文件上传是所有UI自动化测试都要面对的一个头疼问题 今天博主在这里给大家分享下自己处理文件上传的经验 希望能够帮助到广大被文件上传
  • ​stp文件转ply

    什么是一 stp 文件 STP 文件是用于在 CAD 和 CAM 应用程序之间交换产品数据的 3D CAD 文件 它包含有关 3D 对象的信息 并以类似于STEP文件格式的方式保存 STP 文件根据STEP应用程序协议 ISO 10303
  • Qt设置label的文字自动换行、高度随内容调整、上下可滚动

    Qt初学者 想要用label控件实现一个用于显示文字信息的文本框 要求文字自动换行 label高度随内容调整 上下可滚动 为了实现滚动 使用ScrollArea控件 将要显示文字的label放到这个ScrollArea里 然后设置Scrol
  • 控制台报错:failed to load resource: net:err_cert_date_invalid的解决办法

    原因 浏览器端屏蔽了不安全的连接 解决办法 1 打开检查页面里 Network 网络 2 双击name 名称 下红色报错的地方 会出现以下界面 3 点击高级 下面会出现提示 点击继续前往 不安全 4 回到之前的页面刷新 问题就解决了
  • Exception of type 'System.OutOfMemoryException' was thrown.

    OutOfMemoryException Exception of type System OutOfMemoryException was thrown 在装有4G物理内存的32位OS机器上内存不足 原因是 The common lang
  • array type has an incomplete element type

    一 错误详情 array type has an incomplete element type 二 错误分析 1 错误代码 int readInfo int B int n int findMax int B int n int m 2
  • 英伟达Tesla T4 显卡编解码能力测试

    显卡基本参数 Timestamp Fri Aug 27 10 04 12 2021 Driver Version 460 32 03 CUDA Version 11 2 Attached GPUs 8 FB Memory Total 151
  • 计算机网络【IP数据包首部的各个字段详解】

    文章目录 一 网络层简述 二 网络层首部 版本 首部长度 区分服务 总长度 三 网络层首部 标识 标志 片偏移 四 网络层首部 生存时间 协议 首部检验和 五 可选字段 填充字段 源IP 目标IP 六 总结 一 网络层简述 一个IP数据包的
  • Windows计划任务(手动执行脚本正常,定时执行不生效)

    ps 本公司是一家游戏公司 目前有一台win服务器上面搭建MySQL数据库 现在需要做MySQL数据库的备份然后上传到Linux服务器上 bat 脚本写好以后手动双击测试多遍正常上传备份文件 可每当计划任务去执行的时候往往出错 不执行上传L
  • 系统开发设计基础(二)

    删除
  • 信号的傅里叶分析之傅里叶级数

    1 为什么要进行傅里叶分析 信号分析方法主流方法有 1 时域分析 以冲激信号为基本信号 任意输入信号可分解为一系列冲激信号 2 频域分析 以正弦信号和虚指数信号为基本信号 将任意输入信号分解为一系列不同频率的正弦信号或者虚指数信号之和 独立
  • c语言入门-程序运行的过程

    目录 程序运行的过程 1 编译 预编译 编译 汇编 2 链接 1 段表的合并 2 符号表的合并和重定位 3 运行 预处理 define 宏实现计算 define和typedef define的替换规则 和 将参数插入代码中 带有副作用的宏
  • Qt信号槽connect用法整理

    本文整理了一些当前项目中使用的Qt5 6版本信号 槽connect新旧写法的比较 需要注意的问题 一 connect string based和functor based写法比较 1 1 概述 自Qt 5 0以来 Qt提供了两种C 信号槽c
  • Python读取csv文件

    导入pandas包并设置别名为pd import pandas as pd 读取csv格式文件并把格式设置为DataFrame格式 值1是路径可以用绝对路径 cd盘内读取 也可以用相对路径 此项目内读取 这里用的是相对路径 作用 把乱码格式
  • SpringBoot整合eureka简记

    Eureka是一个服务治理组件 它主要包括服务注册和服务发现 主要用来搭建服务注册中心 Eureka 是一个基于 REST 的服务 用来定位服务 进行中间层服务器的负载均衡和故障转移 Eureka是Netflix 公司开发的 Spring