Eureka服务注册与发现中心

简介

Spring Cloud封装了Netflix 公司开发的Eureka模块来实现服务治理

在传统的RPC远程调用框架中,管理每个服务与服务之间依赖关系比较复杂,管理比较复杂,所以需要使用服务治理,管理服务于服务之间依赖关系,可以实现服务调用、负载均衡、容错等,实现服务发现与注册。

Eureka包含两个组件:Eureka Server和Eureka Client

Eureka Server提供服务注册服务

各个微服务节点通过配置启动后,会在EurekaServer中进行注册,这样EurekaServer中的服务注册表中将会存储所有可用服务节点的信息,服务节点的信息可以在界面中直观看到。

EurekaClient通过注册中心进行访问

它是一个Java客户端,用于简化Eureka Server的交互,客户端同时也具备一个内置的、使用轮询(round-robin)负载算法的负载均衡器。在应用启动后,将会向Eureka Server发送心跳(默认周期为30秒)。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,EurekaServer将会从服务注册表中把这个服务节点移除(默认90秒)

Eureka Server :服务注册发现中心

pom文件:

        
        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            junit
            junit
        
    

application.yml:

server:
  port: 7001

eureka:
  instance:
    hostname: locathost #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动类:需要在上面加入@EnableEurekaServer

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

完成后启动浏览器访问 localhost:7001,如果访问到如下界面则启动成功。

在这里插入图片描述

Eureka Client:服务提供者、服务消费者

pom文件加入:


 
     org.springframework.cloud
     spring-cloud-starter-netflix-eureka-client

application.yml文件中加入:

eureka:
  client:
    #表示是否将自己注册进Eurekaserver默认为true。
    register-with-eureka: true
    #是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
    fetchRegistry: true
    service-url:
      defaultZone: http://localhost:7001/eureka

在启动类上加入@EnableEurekaClient注解

Eureka集群部署

多Eureka Server

创建两个不同的Eureka Server:7001、7002

7001在application中配置:

url :7001指向7002,7002指向7001.

server:
  port: 7001

eureka:
  instance:
    hostname: eureka7001 #eureka服务端的实例名称
  client:
    #false表示不向注册中心注册自己。
    register-with-eureka: false
    #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
    fetch-registry: false
    service-url:
      #设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址。
      defaultZone: http://localhost:7002/eureka/

7002配置同理。

将消费者、服务者的application.yml中的url改为两个Eureka Server的地址:

      service-url:
        defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka

多服务提供者(provider)

创建新的provider程序,修改application中的端口号为不同的端口,服务名相同。

在Eureka管理界面上可以看到存在两个服务提供者。

在这里插入图片描述

接着更改服务消费者的代码,通过服务名作为网址来调用服务。

修改url:

public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";//这里之前是localhost!!

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPayment(@PathVariable Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
    }

在RestTemplate配置类中声明负载平衡注解:

@Configuration
public class ApplicationContextConfig {
    @Bean
    @LoadBalanced
    RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

服务发现

在启动类上加入注解@EnableDiscoveryClient,即可通过DiscoveryClient获取位于Eureka注册中心的服务名,以及服务名对应的相关信息如IP端口。

	@Resource
    private DiscoveryClient discoveryClient;
    @GetMapping("/payment/discovery")
    public Object discovery(){
    	//获取所有服务名
        List services = discoveryClient.getServices();
        for(String service:services){
            System.out.println(service);
        }
		//获取提供指定服务的所有提供者
        List instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.stream().forEach((instance->{
            System.out.println(instance.getServiceId()+" "+instance.getHost()+":"+instance.getPort());
        }));
        return this.discoveryClient;
    }

保护模式

在Eureka服务端90s内丢失了大量的心跳包(大量服务失联),会进入保护模式,不再注销服务实例。

Eureka与consul、Zookeeper的区别

Eureka是满足CAP理论中的AP(可用性、分区一致性)

consul、Zookeeper满足CAP理论中的CP(强一致性、分区一致性)。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/7395fde6b6.html