Java SpringCloud Eureka

1、Spring Cloud Eureka

  • Eureka Client:服务注册
  • Eureka Server:服务发现

    2、Eureka Server

    A.Eureka Server依赖

    1. <dependency>
    2. <groupId>org.springframework.cloud</groupId>
    3. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    4. </dependency>

    B.在启动类添加Eureka Server服务端注解

    ```java package com.fcant.springcloudeurekasever;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

  • SpringCloudEurekaSeverApplication
  • 启用EurekaServer服务端注解 *
  • encoding:UTF-8
  • @author Fcant 22:42 2019/12/8 */ @EnableEurekaServer @SpringBootApplication public class SpringCloudEurekaSeverApplication {

    public static void main(String[] args) {

    1. SpringApplication.run(SpringCloudEurekaSeverApplication.class, args);

    }

}

  1. <a name="86GSs"></a>
  2. ## C.在配置文件配置服务端端口和Eureka服务相关配置
  3. ```yaml
  4. server:
  5. port: 8000
  6. spring:
  7. application:
  8. name: eureka-server
  9. eureka:
  10. client:
  11. # 此应用为注册中心,false:不向注册中心注册自己
  12. register-with-eureka: false
  13. # 注册中心职责是维护服务实例,false:不检索服务
  14. fetch-registry: false
  15. service-url:
  16. defaultZone: http://127.0.0.1:8000/eureka

如果不配置eureka.client则会出现如下报错
语雀内容

D.输入服务端访问的URL可以查看提供的Web管理的内容

image.png

3、GateWay Eureka Client

A.Eureka Client相关依赖

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>

B.在客户端启动类添加Eureka Client注解

I.直接在原有注解上添加@EnableEurekaClient注解

  1. package com.fcant.springcloudgateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  5. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
  6. /**
  7. * SpringCloudGateWayApplication
  8. *
  9. * encoding:UTF-8
  10. * @author Fcant 21:50 2019/12/8
  11. */
  12. @EnableZuulProxy
  13. @EnableEurekaClient
  14. @SpringBootApplication
  15. public class SpringCloudGateWayApplication {
  16. public static void main(String[] args) {
  17. SpringApplication.run(SpringCloudGateWayApplication.class, args);
  18. }
  19. }

II.使用@SpringCloudApplication注解替代@EnableEurekaClient@SpringBootApplication注解

  1. package com.fcant.springcloudgateway;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.cloud.client.SpringCloudApplication;
  4. import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
  5. /**
  6. * SpringCloudGateWayApplication
  7. *
  8. * encoding:UTF-8
  9. * @author Fcant 21:50 2019/12/8
  10. */
  11. @EnableZuulProxy
  12. @SpringCloudApplication
  13. public class SpringCloudGateWayApplication {
  14. public static void main(String[] args) {
  15. SpringApplication.run(SpringCloudGateWayApplication.class, args);
  16. }
  17. }

C.在配置文件配置客户端内容-application.yml

  1. zuul:
  2. routes:
  3. fcant:
  4. path: /fcant-filter
  5. spring:
  6. application:
  7. name: gateway-service
  8. eureka:
  9. client:
  10. service-url:
  11. defaultZone: http://127.0.0.1:8000/eureka

D.启动客户端、刷新Server端程序

image.png
image.png

4、Consul&Eureka对比

  • Consul :保证强一致性
    • 服务注册相比Eureka会稍慢一些,Consul要求过半的节点都写入成功。
    • Leader 挂掉时,重新选举期整个Consul不可用。
  • Eureka :保证高可用
    • 服务注册快,不需要等待注册信息复制到其他节点,也不保证复制成功。
    • 当注册信息不相同时,每个Eureka节点依然能够正常对外提供服务。