当前位置:   article > 正文

Spring Cloud知识点整理 | (二) Eureka服务注册_spring-cloud-starter-eureka的版本号

spring-cloud-starter-eureka的版本号

本文章使用Spring Boot 2.x版本
Spring Boot2.x版本与1.x版本相比,依赖包发生了变化,如
1.0x版本:spring-cloud-starter-eureka
2.0x版本:spring-cloud-starter-netflix-eureka

项目代码:https://gitee.com/huchxproject/spring-cloud-simple

1. 简介

Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。

Eureka包含两个组件:Eureka ServerEureka Client

1.1. Eureka Server

Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。

1.2. Eureka Client

Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就是一个内置的、使用轮询(round-robin)负载算法的负载均衡器。

1.3. 作用

在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除(默认90秒)。
Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。综上,Eureka通过心跳检查客户端缓存等机制,确保了系统的高可用性灵活性可伸缩性

Spring Boot2.x 基础依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
   	<version>2.1.5.RELEASE</version>
 	<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

2. 服务端

2.1. 添加依赖

<!--Eureka服务端依赖-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<!--Spring Boot1.x版本依赖-->
	<!--<artifactId>spring-cloud-starter-eureka-server</artifactId>-->
	<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

<dependencyManagement>
	<dependencies>
    	<dependency>	
    		<groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
       	</dependency>
		<dependency>
        	<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
	</dependencies>
</dependencyManagement>
    
<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • dependencyManagement
    • 只是声明依赖, 并不实现引入,因此子项目需要显示的声明需要用的依赖。
    • 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并aversion和scope都读取自父pom
    • 如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
  • repository
    • 使用Spring Cloud时,阿里云的仓库好像不行,需要用spring的仓库,地址:https://repo.spring.io/libs-milestone

2.2. 配置applicaton.yml

server:
  port: 8081
eureka:
  instance:
    hostname: localhost
  client:
    ##是否将资深注册
    register-with-eureka: false
    ##如果为true,启动时报警
    fetch-registry: false
    service-url:
      default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

2.3. 启动服务

在启动类上面添加@EnableEurekaServer注解

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApp
{
    public static void main( String[] args )
    {
        SpringApplication.run(EurekaServerApp.class,args);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

2.4. 测试

启动服务后,浏览器打开http:/localhost:8081,出现如图即为成功:
Eureka

3. 服务提供者

作为请求的最终执行者,提供服务其他客户端调用。也可以系统中的客户端注册到注册中心中。

3.1. 引入依赖

<!--Eureka服务端依赖-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<!--Spring Boot1.x版本依赖-->
	<!--<artifactId>spring-cloud-starter-eureka</artifactId>-->
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependencyManagement>
	<dependencies>
    	<dependency>	
    		<groupId>org.springframework.cloud</groupId>
	        <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
       	</dependency>
		<dependency>
        	<groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Finchley.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
	</dependencies>
</dependencyManagement>
    
<repositories>
  <repository>
    <id>spring-milestones</id>
    <name>Spring Milestones</name>
    <url>https://repo.spring.io/milestone</url>
    <snapshots>
        <enabled>false</enabled>
    </snapshots>
  </repository>
</repositories>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

3.2. 配置application.yml

server:
  port: 8084
eureka:
  client:
    service-url:
    ## 地址为注册中心(2.2.中)配置的地址
      defaultZone: http://localhost:8081/eureka
spring:
  application:
  ## 必须要有此属性,指定在注册中心的名称
    name: eureka-provider
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

注意: 使用defaultZone,而不是default-zone,因为使用default-zone,启动服务时会提示找不到注册中心的地址。

3.3. 声明服务

在启动类上添加@EnableDiscoveryClient@EnableEurekaClient
,表明应用为client。

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaProviderApp {

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

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.4. 添加地址映射

@RequestMapping("client")
public String index(String name){
  return "Hello,"+name;
}
  • 1
  • 2
  • 3
  • 4

3.5. 测试

启动服务后,浏览器打开http:/localhost:8081,出现如图即为成功:
Encureka
访问http://localhost:8084/client?name=huchx,显示如图:
Eureka Test

4. 注意事项

  1. 关于Spring Boot2.x1.x版本对于Eureka依赖的差异
  2. 在配置文件中使用defaultZone,而不是default-zone
  3. 必须先启动Eureka Server(注册中心)的服务,再启动Eureka Client服务。

上一章:Spring Cloud 知识点整理 | (一)基本概念理解
下一章:Spring Cloud知识点整理 | (三) Eureka服务消费者

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/794169
推荐阅读
相关标签
  

闽ICP备14008679号