微服务Spring Cloud (一) 注册中心 Eureka

转载请表明出处 https://blog.csdn.net/Amor_Leo/article/details/87873898 谢谢

Spring Cloud Eureka 概述

Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能。Eureka包含两个组件:Eureka Server和Eureka Client。
Eureka Server提供服务注册服务,各个节点启动后,会在Eureka Server中进行注册,这样EurekaServer中的服务注册表中会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观的看到。
​Eureka Client是一个java客户端,用于简化与Eureka Server的交互,客户端同时也就别一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳,默认周期为30秒,告诉Eureka Server; Eureka Server在启动的时候会创建一个定时任务,默认每隔一段时间(默认60秒)将当前服务注册表中超时(默认90秒)没有续约的服务节点移除。
Eureka Server 在运行期间会统计心跳失败的比例在15分钟内是否低于85%,如果出现低于的情况,Eureka就认为客户端与注册中心出现了网络故障,Eureka Server 会自动进入自我保护机制,将当前的实例注册信息保护起来,让这些实例不会过期,此时会出现以下几种情况:

  1. Eureka Server不再从注册列表中移除因为长时间没收到心跳而应该过期的服务。
  2. Eureka Server仍然能够接受新服务的注册和查询请求,但是不会被同步到其它节点上,保证当前节点依然可用。
  3. 当网络稳定时,当前Eureka Server新的注册信息会被同步到其它节点中。

但是在保护期内如果服务刚好这个服务提供者非正常下线,此时服务消费者就会拿到一个无效的服务实例,此时会调用失败,对于这个问题需要服务消费者端要有一些容错机制,如重试、断路器等。也可以eureka.server.enable-self-preservation=false来关闭保护机制,这样可以确保注册中心中不可用的实例被及时的剔除,但不推荐。
Eureka Server之间通过复制的方式完成数据的同步,Eureka还提供了客户端缓存机制,即使所有的Eureka Server都挂掉,客户端依然可以利用缓存中的信息消费其他服务的API。

Spring Cloud Eureka 搭建

单机

服务端 Eureka Server

  • pom
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  • yml
server:
  port: 8761  #注册中心的地址
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
    lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
  server:
    enable-self-preservation: false #关闭自我保护机制
  client:
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
    register-with-eureka: false # 对于注册中心的优化,表明不向注册中心注册自己
    fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
  • Application类上
@EnableEurekaServer  //开启eureka server功能

Eureka是有图形界面的,我们打开浏览器,访问 http://localhost:8761/

  • 如果是在虚拟机上用Docker运行
    • 修改yml
    server:
      port: 8761  #注册中心的地址
    spring:
      application:
        name: eureka-server
    eureka:
      instance:
        hostname:  ${HOST}
        lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
        lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
      server:
        enable-self-preservation: false #关闭自我保护机制
      client:
        service-url:
          defaultZone: ${EUREKA_SERVER_URL}
        register-with-eureka: false # 对于注册中心的优化,表明不向注册中心注册自己
        fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
    management:
      endpoints:
        web:
          exposure:
            include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
    
    • 打包jar 编写dockerfile
    FROM java:8-alpine
    MAINTAINER "LHL <aomrlee412@gmail.com>" 
    ADD *.jar app.jar
    EXPOSE 8761
    ENTRYPOINT ["java","-jar","/app.jar"]
    
    • 构建镜像
    docker build  ./eureka -t "eureka-server:1.0"
    
    • 创建并运行容器
    docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka -e HOST=192.168.0.110  -d  eureka-server:1.0
    

客户端 Eureka Client

  • pom
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • yml
eureka:
  client:
    service‐url:
      defaultZone: http://localhost:8761/eureka
  instance:
    prefer‐ip‐address: true  #访问路径可以显示ip地址
    instance-id: USER-SERVICE   #自定义服务名称
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
  • Application类上
@EnableDiscoveryClient

Eureka集群(本地)与SpringSecurity结合 (springBoot2.1以上版本)

修改host

C:\Windows\System32\drivers\etc

127.0.0.1       peer1
127.0.0.1       peer2
127.0.0.1       peer3

服务端 Eureka Server

  • pom
		<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
  • yml
    • application-peer1.yml
    server:
      port: 8761
    spring:
      application:
        name: eureka-server
      security:
        user:
          name: amor
          password: amor
    eureka:
      instance:
        hostname: peer1
        lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
        lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
      server:
        enable-self-preservation: false #关闭自我保护机制
      client:
        service-url:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer2:8762/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer3:8763/eureka/
        register-with-eureka: false # 对于注册中心的优化
        fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
    management:
      endpoints:
        web:
          exposure:
            include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
    
    • application-peer2.yml
    server:
      port: 8762
    spring:
      application:
        name: eureka-server
      security:
        user:
          name: amor
          password: amor
    eureka:
      instance:
        hostname: peer2
        lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
        lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
      server:
        enable-self-preservation: false #关闭自我保护机制
      client:
        service-url:
          defaultZone:  http://${spring.security.user.name}:${spring.security.user.password}@peer1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer3:8763/eureka/
        register-with-eureka: false # 对于注册中心的优化
        fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
    management:
      endpoints:
        web:
          exposure:
            include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
    
    • application-peer3.yml
    server:
      port: 8763
    spring:
      application:
        name: eureka-server
      security:
        user:
          name: amor
          password: amor
    eureka:
      instance:
        hostname: peer3
        lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
        lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
      server:
        enable-self-preservation: false #关闭自我保护机制
      client:
        service-url:
          defaultZone: http://${spring.security.user.name}:${spring.security.user.password}@peer1:8761/eureka/,http://${spring.security.user.name}:${spring.security.user.password}@peer2:8762/eureka/
        register-with-eureka: false # 对于注册中心的优化
        fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
    management:
      endpoints:
        web:
          exposure:
            include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
    
  • Application类上
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaApplication.class, args);
    }
	
	//springBoot2.1以上版本 (如果不加 注册不了)
    @EnableWebSecurity
    public static class WebSecurityConfig extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().ignoringAntMatchers("/eureka/**");
            super.configure(http);
        }
    }

}
  • 启动
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer1
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer2
java -jar eureka-server-1.0.0.jar --spring.profiles.active=peer3

Eureka是有图形界面的,我们打开浏览器,访问 http://localhost:8761/

客户端 Eureka Client

  • pom
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • yml
eureka:
  client:
    serviceUrl:
      defaultZone: http://${eurekaUserName}:${eurekaPassWord}@peer1:8761/eureka/,http://${eurekaUserName}:${eurekaPassWord}@peer2:8762/eureka/,http://${eurekaUserName}:${eurekaPassWord}@peer3:8763/eureka/
  instance:
    prefer‐ip‐address: true
    instance-id: USER-SERVICE   #自定义服务名称
eurekaUserName: amor
eurekaPassWord: amor
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
  • Application类上
@EnableDiscoveryClient

高可用集群(Docker)

服务端 Eureka Server

  • pom
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
  • yml
server:
  port: 8761  #注册中心的地址
spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: ${HOST}
    lease-renewal-interval-in-seconds: 1  #心跳检测检测时间,每间隔1s,向服务端发送一次心跳,表明自己依然”存活“
    lease-expiration-duration-in-seconds: 2 #续约时间,告诉服务端,如果我2s之内没有给你发心跳,就代表我“死”了,将我剔除
  server:
    enable-self-preservation: false #关闭自我保护机制
  client:
    service-url:
      defaultZone: ${EUREKA_SERVER_URL}
    register-with-eureka: false # 对于注册中心的优化,表明不向注册中心注册自己
    fetch-registry: false # 注册中心的优化,代表该注册中心服务,不拉取任何的服务列表
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
  • Application类上
@EnableEurekaServer  //开启eureka server功能
  • 打包jar 编写dockerfile
FROM java:8-alpine
MAINTAINER "LHL <aomrlee412@gmail.com>" 
ADD *.jar app.jar
EXPOSE 8761
ENTRYPOINT ["java","-jar","/app.jar"]
  • 构建镜像
docker build  ./eureka -t "eureka-server:1.0"
  • 创建并运行容器
    • 192.168.0.110
    docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.111:8761/eureka,http://192.168.0.112:8761/eureka -e HOST=192.168.0.110  -d  eureka-server:1.0
    
    • 192.168.0.111
    docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka,http://192.168.0.112:8761/eureka -e HOST=192.168.0.111  -d  eureka-server:1.0
    
    • 192.168.0.112
    docker run --name eureka-server -p 8761:8761 -e EUREKA_SERVER_URL=http://192.168.0.110:8761/eureka,http://192.168.0.111:8761/eureka -e HOST=192.168.0.112  -d  eureka-server:1.0
    

客户端 Eureka Client

  • pom
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  • yml
eureka:
  client:
    service‐url:
      defaultZone: http://192.168.0.110:8761/eureka,http://192.168.0.111:8761/eureka,http://192.168.0.112:8761/eureka
  instance:
    prefer‐ip‐address: true  #访问路径可以显示ip地址
    instance-id: USER-SERVICE   #自定义服务名称
management:
  endpoints:
    web:
      exposure:
        include: "*"  #因为springboot2.1必须加上,支持访问/actuator/hystrix.stream
  • Application类上
@EnableDiscoveryClient
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值