Spring Cloud 套餐——服务发现组件 Eureka

2017-06-11  本文已影响0人  码农套餐

服务注册中心 Eureka

Eureka 分为 Eureka Server 和 Eureka Client。主要用于 Spring Cloud 的服务注册与服务发现。
代码:https://github.com/coding-farmer-ming/learning-spring-cloud-sample

Eureka Server 的使用

1、添加依赖

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

2、配置 application.yml

server:
  port: 8761
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      default-zone: http://localhost:8761/eureka
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
}

Eureka Client 的使用

1、添加依赖

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

2、配置 application.yml

spring:
  application:
    name: microservice-name
eureka:
  client:
    service-url:
      default-zone: http://localhost:8761/eureka
  instance:
    prefer-ip-address: true
@SpringBootApplication
@EnableDiscoveryClient
public class ProvuderUserApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProvuderUserApplication.class, args);
    }
}

高可用 Eureka Server

1、高可用 Eureka Server 的依赖与启动类与单点 Eureka Server 是完全相同的
2、在高可用 Eureka Server 的 application.yml 配置中,需要开启相互注册以及相互同步彼此的服务注册信息

为了方便,可以在 host 中配置 127.0.0.1 eureka1 eureka2

spring:
  application:
    name: microservice-eureka
---
spring:
  profiles: eureka1
server:
  port: 8761
eureka:
  instance:
    hostname: eureka1
  client:
    service-url:
      default-zone: http://eureka2:8762/eureka/
---
spring:
  profiles: eureka2
server:
  port: 8762
eureka:
  instance:
    hostname: eureka2
  client:
    service-url:
      default-zone: http://eureka1:8761/eureka/

3、将应用注册到 Eureka Server 集群

spring:
  application:
    name: microservice-name
eureka:
  client:
    service-url:
      default-zone: http://eureka1:8761/eureka,http://eureka2:8762/eureka
  instance:
    prefer-ip-address: true

带有用户认证的 Eureka Server

1、在添加 Eureka 自身依赖后,还需要添加 Eureka Server 认证的依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <aftifactId>spring-boot-starter-security</aftifactId>
</denpendency>

2、在配置中,需要添加相关的用户名与密码配置

security:
  basic:
    enabled: true
  user:
    name: user
    password: password123
eureka:
  client:
    service-url:
      defaultZone: http://username:password@EUREKA_HOST:EUREKA_PORT/eureka/
上一篇 下一篇

猜你喜欢

热点阅读