Spring Cloud(Greenwich.SR1) - 服务注册,注册中心eureka ha与consul

前言

上一章讲了Spring cloud的注册中心eureka server,现在说说如何实现eureka server的高可用,并尝试使用第三方注册中心consul,毕竟eureka server已经不在迭代维护。

1. eureka server高可用的本质

eureka server是使用相互注册实现高可用,数据相互写入实现数据的同步。eureka server自身也是一个eureka client端。

那么用一个eureka server如何实现高可用,使用spring.profile区分。

1.1 修改eureka server的application.yml的文件

server:
  port: 8082

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8083/eureka/

spring:
  profiles: eur1
  application:
    name: eurka-server

---

server:
  port: 8083

eureka:
  instance:
    hostname: 127.0.0.1
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8082/eureka/

spring:
  profiles: eur2
  application:
    name: eurka-server

里面注意两点:

registerWithEureka: true
fetchRegistry: true

这两个配置改为true,表示向其他eureka server注册;另外spring.profiles,参考Spring官方文档: 25. Profiles

defaultZone: http://${eureka.instance.hostname}:8083/eureka/

注意这个地址,当前server注册是其他server的地址,可以是多个,多个使用逗号隔开。 

1.2 运行eureka server实现ha

按照intellij idea修改spring boot的main函数运行JVM参数

 启动main方法

以此方法,修改eur2启动,访问http://localhost:8082/,http://localhost:8083/

 可以看到服务已经相互注册了,信息同步,eureka server实现高可用。

1.2 修改eureka client端

server:
  port: 8201

spring:
  application:
    name: service-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8082/eureka/,http://localhost:8083/eureka/

defaultZone: http://localhost:8082/eureka/,http://localhost:8083/eureka/ 

注册地址修改为两个eureka server即可。 

启动服务,即可看到两个注册中心同时存在相同的一个client注册

2. consul注册中心

consul是go语言写的,笔者曾经学过一点皮毛,go语言性能介于c++与java之间,对并发编程支持较好,系统依赖较少,跨平台……

2.1 consul服务端

下载consul服务端:https://www.consul.io/downloads.html,解压,只有一个文件,是可执行文件(go语言典型特点)

 官方可以查看一系列命令:https://www.consul.io/docs/commands/index.html

按照上面的官方doc,使用./consul agent -dev,启动consul

 

访问http://localhost:8500/

2.2 consul客户端

新建Spring Boot项目,命名consul-client,pom依赖如下:

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

在application.yml文件加入consul的注册地址,我使用8301挂了,说端口占用,如果出现此异常,自行排除端口占用问题。

server:
  port: 8601
spring:
  application:
    name: consul-client
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        serviceName: consul-client

main方法打上服务注册发现的注解@EnableDiscoveryClient

package com.spring.cloud.consul.client;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConsulClientMain {
    public static void main(String[] args) {
        SpringApplication.run(ConsulClientMain.class, args);
    }
}

启动后,访问localhost:8085/

 

可以看到已经有服务注册了。

总结:

Spring Cloud在设计的时候已经模块化分离,使用服务注册发现的注解@EnableDiscoveryClient即可对各种注册中心实现注册。

注册需要不同注册中的的URL地址,引用不同依赖即可,不同的注册中心具有很多不同的特性,需要根据项目实际需求选择。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值