SpringBoot整合eureka简记

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-SNAPSHOTSpring Boot >=2.6.4-SNAPSHOT and <2.7.0-M1
2021.0.0Spring Boot >=2.6.1 and <2.6.4-SNAPSHOT
2021.0.0-RC1Spring Boot >=2.6.0-RC1 and <2.6.1
2021.0.0-M3Spring Boot >=2.6.0-M3 and <2.6.0-RC1
2021.0.0-M1Spring Boot >=2.6.0-M1 and <2.6.0-M3
2020.0.5Spring Boot >=2.4.0.M1 and <2.6.0-M1
Hoxton.SR12Spring Boot >=2.2.0.RELEASE and <2.4.0.M1
Hoxton.BUILD-SNAPSHOTSpring Boot >=2.2.0.BUILD-SNAPSHOT
Hoxton.M2Spring Boot >=2.2.0.M4 and <=2.2.0.M5
Greenwich.BUILD-SNAPSHOSpring Boot >=2.1.9.BUILD-SNAPSHOT and <2.2.0.M4
Greenwich.SR2Spring Boot >=2.1.0.RELEASE and <2.1.9.BUILD-SNAPSHOT
Greenwich.M1Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE
Finchley.BUILD-SNAPSHOTSpring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3
Finchley.SR4Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT
Finchley.RC2Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE
 Finchley.RC1 Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE
Finchley.M9Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE
Finchley.M7Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2
Finchley.M6Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1
Finchley.M5Spring Boot >=2.0.0.M7 and <=2.0.0.M7
Finchley.M4Spring Boot >=2.0.0.M6 and <=2.0.0.M6
Finchley.M3Spring Boot >=2.0.0.M5 and <=2.0.0.M5
Finchley.M2Spring Boot >=2.0.0.M3 and <2.0.0.M5
Edgware.SR51.5.20.RELEASE
Edgware.SR51.5.16.RELEASE
Edgware.RELEASE1.5.9.RELEASE
Dalston.RC11.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文库

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值