Spring Cloud eureka HA 高可用服务注册中心

  1. 创建spring-boot项目,命名eureka-server, pom.xml如下

    org.springframework.boot
    spring-boot-starter-parent
    1.3.7.RELEASE

    org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.cloud spring-cloud-starter-eureka
      <dependencyManagement>
     	<dependencies>
     	<dependency>
     	  <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka-server</artifactId>
           <version>Brixton.SR5</version>
           <type>pom</type>
           <scope>import</scope>
     	</dependency>
     	</dependencies>
     </dependencyManagement>
    
2. 高可用注册中心
application-peer1.properties
spring.application.name=eureka-server
server.port=1111
eureka.instance.hostname=peer1
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false //false表示只维护服务
eureka.client.serviceUrl.defaultZone=http://peer2:112/eureka/
eureka.instance.prefer-ip-address=true  //开启IP注册服务中心(非主机名)

application-peer2.properties
spring.application.name=eureka-server
server.port=1112
eureka.instance.hostname=peer2
eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://peer1:1111/eureka/
eureka.instance.prefer-ip-address=true 开启IP注册服务中心(非主机名)
  1. 开启eurekaServer注册中心
    @EnableEurekaServer
    @SpringBootApplication
    public class Application
    {
    public static void main(String[] args)
    {
    new SpringApplicationBuilder(Application.class).web(true).run(args);
    }
    }

将hello-service注册为高可用注册中心的hello-service
spring.application.name=hello-service
eureka.client.serviceUrl.defaultZone=http://peer1:111/eureka/,
http://peer2:1112/eureka/

  1. Ribbon的客户端负载均衡的服务发现与消费(pom.xml)

    org.springframework.boot
    spring-boot-starter-parent
    1.3.7.RELEASE




    org.springframework.boot
    spring-boot-starter-web


    org.springframework.boot
    spring-boot-starter-eureka


    org.springframework.boot
    spring-boot-starter-ribbon





    org.springframework.cloud
    spring-cloud-dependencies
    Brixtong.SR5
    pom
    import


    @EnableDiscoveryClient
    @SpringBootApplication
    public class  ConsumerApplication{
    
         //ribbon负载均衡的restTemplate模板
    	@Bean
    	@LoadBalanced
    	RestTemplate restTemplate()
    	{
    		return new RestTemplate();
    	}
    
    	public static void main(String[] args)
    	{
    		SpringApplication.run(ConsumerApplication.class,args);
    		}
    

}

    @RestController
    public class ConsumerController()
    {
@Autowired
RestTemplate restTemplate;

@RequestMapping(value="/ribbon-consumer",method=RequestMethod.GET)
public  String  helloConsumer()
{
return  restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();

}

}

application.properties
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

  1. 服务治理机制
    1. 服务注册中心
      –提供服务注册与发现
      1. 服务剔除
      Eureka Server 再启动时候创建一个定时任务,默认60s将当前清单中超时(默认90s),没有续约的服务剔除出去。
      2. 自我保护
      eureka.server.enableself-preservation=false来关闭保护机制

    2. 服务提供者
      1. 服务注册 eureka.client.register-with-eureka=true
      Eureka Server接收到这个rest请求之后,将元数据信息存储在一个双层结构Map中,第一层是key服务名,第二层的key是具体服务实例名。
      2. 服务同步
      两个服务分别注册到HA的注册中心上,由于服务注册中心互相注册,从而实现注册中i性能之间的服务同步
      3. 服务续约
      服务提供者维护一个心跳告诉Eureka Server,防止Eureka Server的“剔除任务”,将该服务实例从服务列表中排除。
      eureka.instance.lease-renewal-interval-in-seconds=30(缓存清单的更新时间)
      eureka.instance.lease-expiration-duration-in-seconds=90

    3. 服务消费者

      1. 获取服务
        Eureka Server会维护一个只读的服务清单来返回给客户端,同时该缓存清单会每隔30秒更新一次。
        获取服务确保 eureka.client.fetch-registry=true
        修改清单更新时间
        eureka.client.registry-fetch-interval-seconds=30
      2. 服务调用
        Ribbon中会采用默认轮询方式调用实现客户端负载均衡
      3. 服务下线
        再客户端中,服务实例进行正常关闭时。出发一个服务下线的rest请求给eureka server 告诉服务注册中心“我要下线了” 服务端接收后将该服务状态置为DOWN。并把该下线事件传播出去。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值