SpringBoot整合eureka简记

这篇具有很好参考价值的文章主要介绍了SpringBoot整合eureka简记。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

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

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

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

springboot集成eureka,微服务,eureka,spring boot,java

 文章来源地址https://www.toymoban.com/news/detail-568601.html

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服务的展示页面,可以看到,这时候是没有服务注册进来的。

springboot集成eureka,微服务,eureka,spring boot,java

 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创建另一个微服务,配置同理

启动后

springboot集成eureka,微服务,eureka,spring boot,java

 可以看到,两个服务均已经成功注册到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文库

 

到了这里,关于SpringBoot整合eureka简记的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 二十、微服务之-Spring Cloud使用、打包、启动与整合springboot

    根据 Spring Cloud 的官方网站,Spring Cloud 为开发人员提供了一些快速构建分布式系统常见模式的工具(例如 配置管理、服务发现、断路器、智能路由、领导选举、分布式会话、集群状态 )。 安装 IntelliJ IDEA:从 JetBrains 官方网站下载适用于您操作系统的 IntelliJ IDEA 版本,并按照

    2024年04月29日
    浏览(34)
  • springboot整合eureka、config搭建注册中心和配置中心

    这篇文章详细介绍怎么通过eureka和config分别搭建一个注册中心和配置中心的服务。 目录 一 、springboot整合eureka搭建注册中心 二、springboot整合config搭建配置中心 三、从配置中心拉取配置 1、在IntelliJ IDEA中创建一个springboot项目,并命名为eureka 2、修改pom.xml,添加eureka-server的依

    2024年02月16日
    浏览(33)
  • Springboot3.X整合Dubbo3.XSpringCloudAlibaba微服务 2022.0 + Springboot3.X 集成 Dubbo实现对外调用http内部调用RPC

    近期自己新开了一套SpringCloud Alibaba微服务项目,接口使用了对外HTTP,内部RPC的设计,具体点说就是外部用户或客户端通过Nginx访问到Gateway网关再分发到各个服务,内部各个服务之间统一使用Dubbo RPC进行通信。下面是Springboot3.x集成Dubbo的分享: 1. 需要的关键依赖 2. 启动程序入

    2024年02月15日
    浏览(26)
  • 13、MongoDB--通过 SpringBoot 整合 Spring Data MongoDB(【连接多个 MongoDB 服务器】、【高级定制 MongoDB 客户端】的简单介绍)

    放弃 Spring Boot 为 MongeDB 提供的自动配置,接下来同样要干如下事情: 手动配置多组 ReactiveMongoDatabaseFactory 和 ReactiveMongoTemplate,要连几个 MongoDB 服务器就配置几组。 同步 API 则使用 MongoDatabaseFactory 和 MongoTemplate。 针对不同 MongoDB 服务器,分别开发相应的 DAO 组件类,建议将它

    2024年03月19日
    浏览(48)
  • 云原生微服务 第五章 Spring Cloud Netflix Eureka集成负载均衡组件Ribbon

    第一章 Java线程池技术应用 第二章 CountDownLatch和Semaphone的应用 第三章 Spring Cloud 简介 第四章 Spring Cloud Netflix 之 Eureka 第五章 Spring Cloud Netflix 之 Ribbon Spring Cloud Ribbon 是一套基于 Netflix Ribbon 实现的客户端负载均衡和服务调用工具,其主要功能是提供客户端的负载均衡算法和服务

    2024年02月08日
    浏览(33)
  • Springboot搭建微服务案例之Eureka注册中心

    放一些pojo类 (1)整合mybatis dao层 创建 dao 接口的映射文件还有 mybatis 的核心配置文件 配置 MyBatis 的类型别名,简化 MyBatis 映射文件中的配置  (2)Service 使用 RestTemplate 这个 Java 客户端组件来实现服务的远程调用。所以我们需要将 RestTemplate 使用 Java 配置类进行注入: 这里

    2024年02月05日
    浏览(35)
  • spring security - 快速整合 springboot

    2024年02月10日
    浏览(35)
  • DevOps系列文章 之 SpringBoot整合GitLab-CI实现持续集成

    在企业开发过程中,我们开发的功能或者是修复的BUG都需要部署到服务器上去,而这部分部署操作又是重复且繁琐的工作,GitLab-CI 持续集成为我们解决了这一痛点,将重复部署的工作自动化,大大的节省了程序员们的宝贵时间。本文详细讲述了 GitLab-CI 持续集成的安装、部署

    2024年02月13日
    浏览(35)
  • SpringBoot整合Spring Security实现权限控制

    要对Web资源进行保护,最好的办法莫过于Filter 要想对方法调用进行保护,最好的办法莫过于AOP。 Spring Security进行认证和鉴权的时候,就是利用的一系列的Filter来进行拦截的。 如图所示,一个请求想要访问到API就会从左到右经过蓝线框里的过滤器,其中 绿色部分是负责认证的

    2024年02月15日
    浏览(30)
  • 【RabbitMQ】4 Spring/SpringBoot整合RabbitMQ

    spring-amqp 是对AMQP的一些概念的一些抽象, spring-rabbit 是对RabbitMQ操作的封装实现。 主要有几个核心类 RabbitAdmin 、 RabbitTemplate 、 SimpleMessageListenerContainer 等。 RabbitAdmin 类完成对Exchange,Queue,Binding的操作,在容器中管理了 RabbitAdmin 类的时候,可以对Exchange,Queue,Binding进行自

    2024年01月22日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包