基本概念和方案

    Eureka是基于REST(Representational State Transfer,代表性状态传输)的服务,主要用于AWS云中定位服务,以实现中间层服务器的负载平衡和故障转移。我们称这个服务为Eureka服务器。Eureka还带有一个基于Java的客户端组件,即Eureka客户端,它使与服务的交互更容易。

Eureka-Server

1.pom引入依赖:

 <dependencies>
        <!--eureka-server服务端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <!-- 修改后立即生效,热部署 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

2.配置文件application.yml编写

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
#       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
        defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/

如果为单机的话则defaultZone那边只需要一条即可,这边为集群环境。集群环境下的其余EurekaServer唯一不同在于配置文件中的hostname和defaultZone,hostname为各自唯一,defaultZone为除了自身外的另外EurekaServer地址。

7002的application.yml:

server:
  port: 7002

eureka:
  instance:
    hostname: eureka7002.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
#       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
        defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/

7003的application.yml:

server:
  port: 7003

eureka:
  instance:
    hostname: eureka7003.com #eureka服务端的实例名称
  client:
    register-with-eureka: false     #false表示不向注册中心注册自己。
    fetch-registry: false     #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    service-url:
#       defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #设置与Eureka Server交互的地址查询服务和注册服务都需要依赖这个地址(单机)。
       defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/

注意:3个application.yml的hostname都可用域名来表示,此例子在本机127.0.0.1测试,需要在host文件做下域名映射:

127.0.0.1  eureka7001.com
127.0.0.1  eureka7002.com
127.0.0.1  eureka7003.com

3.启动类编写

package com.zhanghf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

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

4.查看效果:

配置好后启动3个服务,访问http://eureka7001.com:7001http://eureka7002.com:7002和http://eureka7003.com:7003都可以,并且可以看到,eurekaserver的集群已经生效

访问:http://eureka7001.com:7001

一、SpringCloud五大神兽之Eureka(eurekaServer集群)-LMLPHP

 访问:http://eureka7002.com:7002

一、SpringCloud五大神兽之Eureka(eurekaServer集群)-LMLPHP

访问:http://eureka7003.com:7003

一、SpringCloud五大神兽之Eureka(eurekaServer集群)-LMLPHP

next:二、SpringCloud五大神兽之Eureka(服务注册与发现)

10-11 04:26