spring cloud eureka服务发现(高可用)

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

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

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <parent>  
  6.         <groupId>com.jb.cloud</groupId>  
  7.         <artifactId>microservice-spring-cloud</artifactId>  
  8.         <version>0.0.1-SNAPSHOT</version>  
  9.     </parent>  
  10.   
  11.     <artifactId>microservice-discovery-eureka</artifactId>  
  12.     <packaging>jar</packaging>  
  13.   
  14.     <properties>  
  15.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>org.springframework.cloud</groupId>  
  21.             <artifactId>spring-cloud-starter-eureka-server</artifactId>  
  22.         </dependency>  
  23.         <dependency>  
  24.             <groupId>org.springframework.boot</groupId>  
  25.             <artifactId>spring-boot-starter-security</artifactId>  
  26.         </dependency>  
  27.     </dependencies>  
  28.   
  29. </project>  

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

  1. package com.jb.cloud;  
  2.   
  3. import org.springframework.boot.SpringApplication;  
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;  
  6. /** 
  7.  * 使用eureka做服务发现 
  8.  * @author pangps 
  9.  */  
  10. @SpringBootApplication  
  11. @EnableEurekaServer  
  12. public class EurekaApplication {  
  13.   public static void main(String[] args) {  
  14.     SpringApplication.run(EurekaApplication.class, args);  
  15.   }  
  16. }  

配置application.yml 

  1. spring:  
  2.   profiles:  
  3.     active: dev #指定读取的配置文件,默认读取application.yml,如果有指定的会吧默认的配置替换掉  
  4. security:   
  5.   basic:  
  6.     enabled: true #eureka用户是否认证  
  7.   user:  
  8.     name: eureka #eureka 用户名  
  9.     password: eureka #eureka 密码  
  10.       
  11. server:  
  12.   port: 8761  
  13.     
  14. eureka:  
  15.   instance:   
  16.     hostname: localhost #Eureka实例的主机名  
  17.   client:  
  18.     register-with-eureka: false  #在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为  
  19.     fetch-registry: false  
  20.     service-url:  
  21.       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文件

  1. # 添加主机名:  
  2. 7.0.0.1 peer1 peer2 peer3  

配置application.yml

  1. spring:  
  2.   application:  
  3.     name: EUREKA-HA  
  4. ---  
  5. server:  
  6.   port: 7777  
  7. spring:  
  8.   profiles: peer1  
  9. eureka:  
  10.   instance:  
  11.     hostname: peer1  
  12.   client:  
  13.     serviceUrl:  
  14.       defaultZone: http://peer2:8888/eureka/,http://peer3:9999/eureka/  
  15. ---  
  16. server:  
  17.   port: 8888  
  18. spring:  
  19.   profiles: peer2  
  20. eureka:  
  21.   instance:  
  22.     hostname: peer2  
  23.   client:  
  24.     serviceUrl:  
  25.       defaultZone: http://peer1:7777/eureka/,http://peer3:9999/eureka/  
  26. ---  
  27. server:  
  28.   port: 9999  
  29. spring:  
  30.   profiles: peer3  
  31. eureka:  
  32.   instance:  
  33.     hostname: peer3  
  34.   client:  
  35.     serviceUrl:  
  36.       defaultZone: http://peer1:7777/eureka/,http://peer2:8888/eureka/  

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

 

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

  1. eureka:  
  2.   client:  
  3.     serviceUrl:  
  4.       defaultZone:  http://peer1:7777/eureka/,http://peer2:8888/eureka,http://peer3:9999/eureka 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值