Spring Cloud Netfix Eureka (Hoxton版) 使用

Spring Cloud Hoxton.SR4 Spring Boot 2.3.0.RELEASE

GitHub:shpunishment/spring-cloud-learning/spring-cloud-eureka-test

1. 简介

Spring Cloud Netflix Eureka是Spring Cloud Netflix子项目的核心组件之一,主要用于微服务架构中的服务治理。

在微服务架构中往往会有一个注册中心,每个微服务都会向注册中心去注册自己的地址及端口信息,注册中心维护着服务名称与服务实例的对应关系。每个微服务都会定时从注册中心获取服务列表,同时汇报自己的运行情况,这样当有的服务需要调用其他服务时,就可以从自己获取到的服务列表中获取实例地址进行调用,Eureka实现了这套服务注册与发现机制。

2. 使用

先用IDEA创建一个Spring Boot的项目,可以随意引用一个Spring Cloud的组件,之后也会删掉。

创建完,删掉除了pom.xml以外的其他文件,再修改pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.RELEASE</version>
    </parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-eureka-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-cloud-eureka-test</name>
    <description>spring cloud eureka test</description>
    <!--修改打包方式为pom-->
    <packaging>pom</packaging>

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

    <modules>
        <!--后续添加子模块用-->
    </modules>

    <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>
</project>

2.1 独立模式

2.1.1 eureka-server

创建子模块eureka-server

修改pom继承

<parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-eureka-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

再添加依赖

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

修改application.yml

server:
  port: 8100

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    # 是否从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false
    # 是否将服务注册到注册中心(注册中心不需要开启)
    fetch-registry: false

在启动类上添加@EnableEurekaServer注解来启用Euerka注册中心功能。

2.1.2 eureka-client

创建子模块eureka-server

修改pom继承

<parent>
    <groupId>com.shpun</groupId>
    <artifactId>spring-cloud-eureka-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
</parent>

再添加依赖

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

修改application.yml

server:
  port: 8101

spring:
  application:
    name: eureka-client-1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8100/eureka

在启动类上添加@EnableDiscoveryClient注解表明是一个服务发现的客户端。

相应启动eureka-server和eureka-client,访问eureka-server,可看到eureka-client完成注册。

eureka-client完成注册

2.2 对等复制

修改hosts

127.0.0.1 replica1
127.0.0.1 replica2
2.2.1 eureka-server

复制eureka-server的application.yml,给eureka-server添加application-replica1.yml和application-replica2.yml

application-replica1.yml

server:
  port: 8110

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: replica1
  client:
    # 是否从注册中心获取服务
    register-with-eureka: true
    # 是否将服务注册到注册中心
    fetch-registry: true
    service-url:
      # 注册到Eureka replica2注册中心
      defaultZone: http://replica2:8120/eureka

application-replica2.yml

server:
  port: 8120

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: replica2
  client:
    # 是否从注册中心获取服务
    register-with-eureka: true
    # 是否将服务注册到注册中心
    fetch-registry: true
    service-url:
      # 注册到Eureka replica1注册中心
      defaultZone: http://replica1:8110/eureka

修改Active profiles,使用不同的配置文件来启动同一个Spring Boot应用。
添加启动类
分别启动EurekaServerReplica1Application和EurekaServerReplica2Application,访问可看到两个注册中心完成相互备份。
replica1
replica2

2.2.2 eureka-client

修改eureka-client的application.yml

server:
  port: 8101

spring:
  application:
    name: eureka-client-1

eureka:
  client:
    service-url:
      # 多注册中心
      defaultZone: http://replica1:8110/eureka,http://replica2:8120/eureka

启动eureka-client,可看到在两个注册中心都完成了注册。

如果application.yml中只配置了其中一个,因为注册中心完成了复制,所以两个注册中心也都会有eureka-client。

2.3 添加认证

2.3.1 eureka-server

修改pom,添加依赖

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

默认每个请求都需要添加CSRF token才能访问。

Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

修改application.yml,添加账号密码

server:
  port: 8100

spring:
  application:
    name: eureka-server
  security:
    user:
      name: admin
      password: admin

eureka:
  instance:
    hostname: localhost
  client:
    # 是否从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false
    # 是否将服务注册到注册中心(注册中心不需要开启)
    fetch-registry: false
2.3.2 eureka-client

修改application.yml

server:
  port: 8101

spring:
  application:
    name: eureka-client-1

eureka:
  client:
    service-url:
      # 注册中心加认证 按格式添加帐号密码 http://${username}:${password}@${hostname}:${port}/eureka/
      defaultZone: http://admin:admin@localhost:8100/eureka

访问eureka-server,完成认证后,即可看到eureka-client已注册。

多个注册中心的时候也一样,注意添加账号密码。

3. 常用配置

eureka:
  # eureka客户端配置
  client:
    # 是否将自己注册到eureka服务端上去
    register-with-eureka: true
    # 是否获取eureka服务端上注册的服务列表
    fetch-registry: true
    # 指定注册中心地址
    service-url:
      defaultZone: http://localhost:8001/eureka/ 
    # 启用eureka客户端
    enabled: true
    #定义去eureka服务端获取服务列表的时间间隔
    registry-fetch-interval-seconds: 30
  # eureka客户端实例配置
  instance:
    # 心跳时间,即服务续约间隔时间(可以适当再调小)
    lease-renewal-interval-in-seconds: 30
    # 没有心跳的淘汰时间(可以适当再调小)
    lease-expiration-duration-in-seconds: 90
    # 所在区域
    metadata-map:
      zone: guangdong
      # 服务主机名称
    hostname: localhost
    # 是否优先使用ip来作为主机名
    prefer-ip-address: false
  # eureka服务端配置
  server:
    # 状态刷新时间间隔毫秒
    peer-eureka-status-refresh-time-interval-ms: 10000
    # 关闭eureka服务端的保护机制
    enable-self-preservation: false
    # 扫描失效服务的间隔时间
    eviction-interval-timer-in-ms: 1000

参考:
Spring Cloud Netfix 文档
Spring Cloud入门-Eureka服务注册与发现(Hoxton版本)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值