Spring Cloud Eureka Server

Service Discovery: Eureka

Service Discovery is one of the key tenets of a microservice based architecture. Trying to hand configure each client or some form of convention can be very difficult to do and can be very brittle. Eureka is the Netflix Service Discovery Server and Client. The server can be configured and deployed to be highly available, with each server replicating state about the registered services to the others.

译: 服务发现:Eureka

服务发现是基于微服务架构的关键原则之一。尝试配置每个客户端或某种形式的约定可能非常困难,可以非常脆弱。Netflix服务发现服务器和客户端是Eureka。可以将服务器配置和部署为高可用性,每个服务器将注册服务的状态复制到其他服务器。

Euraka集群

  • 编写启动类

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // spring start
    @SpringBootApplication
    // eureka server
    @EnableEurekaServer
    public class EurekaServerApp {
    public static void main(final String[] args) {
    SpringApplication.run(EurekaServerApp.class, args);
    }
    }
  • 配置Host文件

    1
    2
    3
    127.0.0.1 eureka-server1
    127.0.0.1 eureka-server2
    127.0.0.1 eureka-server3
  • 启动EurekaServerApp

    1. 运行server1,启动参数为

      1
      --spring.profiles.active=server1
    2. 运行server2,启动参数为

      1
      --spring.profiles.active=server2
    3. 运行server3,启动参数为

      1
      --spring.profiles.active=server3


  • 配置文件说明
    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
    # 端口
    server:
    port: 8761
    # eureka 中的名称
    spring:
    application:
    name: spring-cloud-eureka
    # eureka 配置
    eureka:
    instance:
    hostname: eureka-server1 #机器名称
    client:
    #是否要注册到服务器端
    register-with-eureka: true
    #是否从服务器端取得注册信息
    fetch-registry: true
    service-url:
    #user:password@hostname 账号密码为对应的服务的账号密码,设置相同
    defaultZone: http://${security.user.name}:${security.user.password}@eureka-server2:8762/eureka/,http://${security.user.name}:${security.user.password}@eureka-server3:8763/eureka/
    # 认证 配置
    security:
    basic:
    #启用认证
    enabled: true
    user:
    #用户名 不设置默认为user,密码为随机,会再启动的时候打印密码
    name: blant
    password: blant

代码:https://github.com/andrewblant/spring-cloud/

上一篇