Spring Cloud Eureka服务发现(高可用)

在微服务架构中,服务发现(Service  Discovery)是关键原则之一。手动配置每个客户端或某种形式的约定是很难做的,并且很脆弱。SpringCloud提供了多种服务发现的实现方式,例如:Eureka、Consul、Zookeeper。Spring Cloud支持得最好的是Eureka,其次是Consul,最次是Zookeeper。

创建一个Maven工程(microservice-discovery-eureka),并在pom.xml中加入如下内容:

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>com.jb.cloud</groupId>
		<artifactId>microservice-spring-cloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>

	<artifactId>microservice-discovery-eureka</artifactId>
	<packaging>jar</packaging>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>
	</dependencies>

</project>

编写SpringBoot启动程序:通过@EnableEurekaServer申明一个注册中心:

package com.jb.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
 * 使用eureka做服务发现
 * @author pangps
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
  public static void main(String[] args) {
    SpringApplication.run(EurekaApplication.class, args);
  }
}

配置application.yml 

spring:
  profiles:
    active: dev #指定读取的配置文件,默认读取application.yml,如果有指定的会吧默认的配置替换掉
security: 
  basic:
    enabled: true #eureka用户是否认证
  user:
    name: eureka #eureka 用户名
    password: eureka #eureka 密码
    
server:
  port: 8761
  
eureka:
  instance: 
    hostname: localhost #Eureka实例的主机名
  client:
    register-with-eureka: false  #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为
    fetch-registry: false
    service-url:
      defaultZone: http://${security.user.name}:${security.user.password}@${eureka.instance.hostname}:${server.port}/eureka


启动工程后,访问:http://loalhost:8761/,如下图。我们会发现此时还没有服务注册到Eureka上面。

按照前文对Eureka的讲解,我们即可构建出一个简单的注册中心。但此时的Eureka是单点的,不适合于生产环境,那么如何实现Eureka的高可用呢?c:/windows/system32/drivers/etc找到hosts文件

  # 添加主机名:
127.0.0.1 peer1 peer2 peer3

配置application.yml

spring:
  application:
    name: EUREKA-HA
---
server:
  port: 7777
spring:
  profiles: peer1
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      defaultZone: http://peer2:8888/eureka/,http://peer3:9999/eureka/
---
server:
  port: 8888
spring:
  profiles: peer2
eureka:
  instance:
    hostname: peer2
  client:
    serviceUrl:
      defaultZone: http://peer1:7777/eureka/,http://peer3:9999/eureka/
---
server:
  port: 9999
spring:
  profiles: peer3
eureka:
  instance:
    hostname: peer3
  client:
    serviceUrl:
      defaultZone: http://peer1:7777/eureka/,http://peer2:8888/eureka/

访问http://peer1:7777我们会发现已经有peer2、peer3节点了。

如果注册中心是高可用的,那么各个微服务配置只需要将   defaultZone改为如下即可:

eureka:
  client:
    serviceUrl:
      defaultZone:	http://peer1:7777/eureka/,http://peer2:8888/eureka,http://peer3:9999/eureka

最后的最后

为初学者提供学习指南,为从业者提供参考价值。我坚信码农也具有产生洞见的能力。扫描下图二维码关注,学习和交流!

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农洞见

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值