lilele200706

 

Eureka的学习思路

Eureka服务注册与发现

它是什么

Eureka是一个基于REST的服务。Netflix的核心子模块之一。

它能干什么,或者说什么情况下使用它

Eureka可以定位服务,实现云端中间层服务发现和故障转移。Eureka对于微服务是非常重要的,有了Eureka,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件,功能类似于Dubbo的注册中心Zookeeper。

它是怎么用的

一、第一模块EurekaServer_7001

1.导包

<dependencies>
   <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
   <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-eureka-server</artifactId>
       <version>1.4.6.RELEASE</version>
   </dependency>
   <!--热部署工具-->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-devtools</artifactId>
   </dependency>
</dependencies>

2.编写配置文件

server:
port: 80

#Eureka的配置
eureka:
instance:
  hostname: localhost #Eureka服务端的实例名字
client:
  register-with-eureka: false #是否向注册中心注册自己
  fetch-registry: false #如何为false,表示自己为注册中心
  service-url: #监控页面
    defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

3.开启这个功能,注解@Enable***

@SpringBootApplication
@EnableEurekaServer //Eureka服务端启动类,可以接受别人注册进来
public class EurekaServer_7001 {
   public static void main(String[] args) {
       SpringApplication.run(EurekaServer_7001.class,args);
  }
}

4.启动测试

二、重构服务提供者模块,将服务注册到EurekaServer中

1.导包

<!--Eureka-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka</artifactId>
           <version>1.4.6.RELEASE</version>
       </dependency>
<!--       完善监控信息-->
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-actuator</artifactId>
       </dependency>

2.编写配置文件

#Eureka 配置,服务注册到哪里
eureka:
client:
  service-url:
    defaultZone: http://localhost:7001/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001 #服务的状态描述
#info 配置
info:
app.name: lele-springcloud
company: org.leletest.com

3.开启这个功能,注解

//启动类
@SpringBootApplication
@EnableEurekaClient //表示将这个服务自动注册到EurekaServer中
public class DeptProvider_8001 {
  public static void main(String[] args) {
      SpringApplication.run(DeptProvider_8001.class,args);
  }
}

4.启动测试

先启动EurekaServer服务,再启动EurekaClient。

5.增加一些服务的信息Discover

public class DeptController {
   @Autowired
   private DiscoveryClient client;
//获取注册进去的服务的一些消息
   @GetMapping("/dept/discovery")
   public Object discovery(){
       List<String> services = client.getServices();
       System.out.println("disvocery=>"+services);
       //获取具体一个服务的信息,需要通过服务的id,即apllicationName
       List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT-8001");
       for (ServiceInstance instance : instances) {
           System.out.println(
                   instance.getHost()+"\t"+
                   instance.getPort()+"\t"+
                   instance.getUri()+"\t"+
                   instance.getServiceId()
                  );
      }
       return this.client;

  }
}

5.1将开启discovery功能的注解编写到启动类上

//启动类
@SpringBootApplication
@EnableEurekaClient //表示将这个服务注册到EurekaServer中
@EnableDiscoveryClient//服务的一些消息
public class DeptProvider_8001 {
   public static void main(String[] args) {
       SpringApplication.run(DeptProvider_8001.class,args);
  }
}

三、搭建Eureka注册中心集群

注册中心如何挂了,问题比较大,所以需要搭集群。

1.再建两个模块EurekaServer:EurekaServer_7002,EurekaServer_7003

除了端口与启动类 不同,其他地方基本相似

2.编写配置文件

#单机 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#集群
defaultZone: http://localhost:7002/eureka/,http://localhost:7003/eureka/

3.服务提供者配置文件中注册中心地址需要增加

#Eureka 配置,服务注册到哪里
eureka:
client:
  service-url:
    defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/
  instance:
    instance-id: springcloud-provider-dept-8001

四、重构服务消费者模块

1.导包

<!--Eureka 的客户端-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-eureka</artifactId>
   <version>1.4.6.RELEASE</version>
</dependency>

2.编写配置文件

eureka:
client:
  register-with-eureka: false #不向Eureka中注册自己
  service-url:
    defaultZone: http://localhost:7001/eureka/,http://localhost:7002/eureka/,http://localhost:7003/eureka/

3.开启这个功能

//Eureka
@SpringBootApplication
@EnableEurekaClient
public class DeptConsumer_80 {
   public static void main(String[] args){
       SpringApplication.run(DeptConsumer_80.class,args);
  }
}

4.启动测试

五、Eureka和Zookeeperd 的区别

1.CAP原则是什么

C(Consistency):一致性

A(Availibility):可用性

P(Partition tolerance):分区容错性

2.Eureka保证的是AP

Eureka集群各个节点都是平等的,一个down了,会自动切换到其他节点,但信息可能不是最新的。如果客户端和注册中心出现网络故障,Eureka还有一种保护机制,不再移除因长时间没有收到心跳而应该过期的服务。

3.Zookeeper保证的是CP

Zookeeper集群中有一个master节点,如果master节点down掉,需要在其他节点中重新选出一个新的master节点,选举期间注册服务会瘫痪,影响可用性。

总结:Eureka可以很好的应对因网络故障导致部分节点失去联系的情况,而不会像Zookeeper那样使整个服务瘫痪。

posted on 2021-12-04 18:13  lilele200706  阅读(44)  评论(0)    收藏  举报

导航