Spring Cloud入门教程(二):Eureka服务注册与发现

简介

Eureka是Netflix公司开源的一款提供服务注册与发现的组件,Spring Cloud基于Netflix公司的Eureka做了封装,Eureka是Spring Cloud中最重要的组件。Eureka分为Eureka Client和Eureka Server。每个服务都是Eureka Client,Eureka Client向Eureka Server注册自己,并从Eureka Server中获取注册表。这样就使多个独立的服务关联到一起了,并且每个服务都可以水平扩展和收缩。

流程图

eureka-architecture-overview.png

如上图,主要分为三个角色:
Eureka Server
Eureka服务器,所有Eureka Client都向他注册,Eureka服务器保存所有Eureka Client的注册表

Service Provider
服务生产者,属于Eureka Client,启动时向Eureka Server注册,并定时向Eureka Server发送心跳,关闭时向Eureka Server取消注册

Service Consumer
服务消费者,属于Eureka Client,跟Service Provider一样也会向Eureka Server注册、发送心跳、取消注册,同时也会从Eureka Server获取Eureka Client的注册表,从而消费服务

Maven父项目

新建一个Maven Project,名称为springcloud,pom.xml配置如下:

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>1.5.13.RELEASE</version>
  </parent>
  <groupId>com.itersblog.springcloud</groupId>
  <artifactId>springcloud</artifactId>
  <version>1.0.0</version>
  <packaging>pom</packaging>
  
  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Edgware.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
  </dependencyManagement>
  
  <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
  </dependencies>
  
  <repositories>
    <repository>
        <id>aliyun</id>
        <name>aliyun nexus</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
            <enabled>true</enabled>
        </releases>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
  </repositories>
  
  <build>
      <plugins>
          <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                  <source>1.8</source>
                  <target>1.8</target>
              </configuration>
          </plugin>
          <plugin>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-maven-plugin</artifactId>
          </plugin>
      </plugins>
  </build>

Eureka Server

springcloud项目中,新建一个Maven Module,名称为springcloud-eureka,pom.xml配置如下:

  <parent>
      <groupId>com.itersblog.springcloud</groupId>
      <artifactId>springcloud</artifactId>
      <version>1.0.0</version>
  </parent>
  <artifactId>springcloud-eureka</artifactId>
  <packaging>jar</packaging>

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

springcloud-eureka项目中,新增bootstrap.properties,配置如下:

spring.application.name=springcloud-eureka

logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p [%15.15t] %-40.40logger{39} : %X{GLOBAL_REQUEST_ID}%m%n
logging.pattern.console=${logging.pattern.file}

spring.application.name:设置服务名,同一个服务的多个实例,服务名必须相同

springcloud-eureka项目中,新增application.properties,配置如下:

server.port=8001

eureka.instance.instanceId=${spring.cloud.client.ipAddress}:${spring.application.name}:${server.port}
eureka.instance.preferIpAddress=true

eureka.client.registerWithEureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://${spring.cloud.client.ipAddress}:${server.port}/eureka/

eureka.instance.instanceId:设置实例的唯一ID
eureka.instance.preferIpAddress:是否IP优先,忽略主机名,默认:false
eureka.client.registerWithEureka:是否将自己注册到Eureka Server,默认:true
eureka.client.fetch-registry:是否从Eureka Server获取注册表,默认:true
eureka.client.serviceUrl.defaultZone:设置Eureka Server的地址,多个用英文逗号隔开

PS:在生产环境中,一般Eureka Server会有多个,以达到高可用的目的,Eureka Server同时也是Eureka Client,需要向其他Eureka Server注册,多个Eureka Server之间会同步注册表,目前我们是单个Eureka Server,所以不需要向其他Eureka Server注册以及获取注册表

springcloud-eureka项目中,新增CloudEurekaApplication.java,代码如下:

@SpringBootApplication
@EnableEurekaServer
public class CloudEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(CloudEurekaApplication.class, args);
    }
}

@SpringBootApplication:表示这是SpringBoot应用
@EnableEurekaServer:表示启用EurekaServer

运行CloudEurekaApplication的main方法,在浏览器中访问Eureka Server的dashboard:http://localhost:8001/,如果出现下图,表示Eureka Server启动成功
eureka-dashboard-1.png

至此,Eureka Server已经准备就绪,等待Eureka Client向其注册了

示例源码:https://github.com/itersblog/springcloud

标签: Spring Cloud入门教程(二):Eureka服务注册与发现

添加新评论