Eureka服务注册与发现


# 基于 Eureka 搭建高可用(双节点服务注册中心)

> 添加`pom`依赖

```xml
<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.5.19.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  <java.version>1.8</java.version>
  <spring-cloud.version>Edgware.SR5</spring-cloud.version>
</properties>

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

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

  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<build>
  <plugins>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
    </plugin>
  </plugins>
</build>
```

> 版本说明 

* 本项目是一个基于 SpringBoot(1.5.19.RELEASE)、SpringCloud(Edgware.SR5)、Eureka 的服务注册中心

## 启动类 @EnableEurekaServer

```java
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}
```

## 配置文件

单个 `Eureka` 服务注册中心

> `application.yml` 
  
```yaml
server:
  port: 1001
spring:
  application:
      name: eureka-server
eureka:
  instance:
    hostname: localhost
  server:
    enable-self-preservation: false # 设为 false,关闭自我保护
    eviction-interval-timer-in-ms: 4000  #清理间隔(单位毫秒,默认是 60*1000)
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
```

> `application-colony.yml`  

```yaml
server:
  port: 1001
spring:
  profiles: server-1
eureka:
  instance:
    hostname: server-1
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
#      defaultZone: http://${eureka.instance.hostname}:${server.port}/${spring.application.name}
      defaultZone: http://server-2:1002/eureka-server

---

  server:
    port: 1002
  spring:
    profiles: server-2
  eureka:
    instance:
      hostname: server-2
    client:
      register-with-eureka: false
      fetch-registry: false
      service-url:
        #      defaultZone: http://${eureka.instance.hostname}:${server.port}/${spring.application.name}
        defaultZone: http://server-1:1001/eureka-server
```
  
双节点注册中心示例,实现双向注册从而提高可用性详细可以看<a href="http://www.ityouknow.com/springcloud/2017/05/10/springcloud-eureka.html">注册中心Eureka</a>

1. 默认使用 `application.yml` 配置文件;

2. 使用`application-conlony.yml` 配置文件时,需要指定激活的配置项.(例如启动`server-1`,需运行`java -jar xx.jar --spring.profiles.active=server-1`)

## 配套代码

GitHub : <a href="https://github.com/ColorCoinK/springcloud-learning/tree/master/eureka-server" target="_blank">服务注册中心Eureka-Server</a>

> 推荐阅读

* 翟永超: <a href="http://blog.didispace.com/springcloud1/" target="_blank">Eureka服务注册与发现</a>
* 周立: <a href="http://www.itmuch.com/spring-cloud/finchley-5/" target="_blank">SpringCloud服务注册与发现Eureka</a>

> 相关文章

<a href="http://www.itmuch.com/spring-cloud/finchley-6/">周立——Eureka服务注册与发现深入</a>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值