SpringCloud整合Eureka

       SpringCloud是在SpringBoot的基础上构建的,用于快速构建分布式系统的工具集。SpringCloud全家桶提供了微服务很多的解决方案。首先从注册中心开始吧,常见的注册中心有 Eureka ,ZooKeeper,Consul。注册中心的作用是用于服务治理,在微服务架构体系中,服务众多而且相互依赖,那么管理起来就很麻烦。

 

        Eureka简单的原理如下图:SpringCloud整合Eureka_第1张图片

1.Eureka Server提供服务的注册与发现,各个微服务启动的时候会将自己的ip,端口等信息注册达到Eureka Server 上面去,会给服务取别名,并会储存这些信息。

2.Eureka Client 是服务的提供者也是服务的消费者,消费者会订阅注册中心的信息,那么这些信息会缓存到Clinet中,不需要每次调用服务都要去Server中查询,即使Server宕机掉在一定的时间内这些节点也不会消失,服务也能够调用。

3.微服务启动之后会每隔30s如果(默认)像Server发送心跳证明自己还活着,如果90s(默认)没有收到心跳信息那么会认为这个服务已经挂掉,该服务会被剔除。

4.Eureka的自我保护机制 ,有时候发生网络故障     但微服务本身没有出现问题这个时候因为网络的原因剔除了服务是不合适的。为了应对这种情况,Eureka提供了自我保护机制,如果在短时间内丢失太多的节点,节点会进入自我保护模式。进入保护模式Server保护注册表中的信息,不会去删除服务。网络恢复之后会退出保护模式。使用eureka.server.enable-self-preservation= false 禁止自我保护模式。

5.Eureka注册的时候也可以将自己进行注册,Eureka集群的时候就是相互注册的,一般Eureka都要进行集群使用,一旦Eureka Server宕机会造成整个微服务不可用,所以集群使用比较好。

接下来上代码分析:       

1.创建Eureka Server   pom依赖如下




  4.0.0

  com.sen
  springcloud-eureka-server
  1.0-SNAPSHOT
  war

  springcloud-eureka-server
  http://www.example.com
 //基于SpringBoot2.0以上版本
  
    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
  

  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Finchley.M7
        pom
        import
      
    
  
  
  
    //引入Eureka Server依赖
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-server
    

  

  
    
      spring-milestones
      Spring Milestones
      https://repo.spring.io/libs-milestone
      
        false
      
    
  


  application.yml的配置如下

###服务端口号
server:
  port: 8888
###eureka 基本信息配置
eureka:
  instance:
    ###注册到eurekaip地址
    hostname: 127.0.0.1
  client:
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###这里没有做集群不需要自己注册自己
    register-with-eureka: false
###因为自己是为注册中心,不需要检索服务
    fetch-registry: false

Eureka Server启动的代码  @EnableEurekaServer 加上这个注解就是作为Server启动。

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {

    public static void main(String[] args){
        SpringApplication.run(EurekaServerApp.class,args);
    }

}

 

 

启动Eureka Server   看到如下信息就启动成功了。SpringCloud整合Eureka_第2张图片                                                                                                      ,

接着创建Eureka Client  pom如下:




  4.0.0

  com.sen
  springcloud-user
  1.0-SNAPSHOT
  war

  springcloud-user
  
  http://www.example.com

  
    org.springframework.boot
    spring-boot-starter-parent
    2.0.1.RELEASE
  
  
  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        Finchley.M7
        pom
        import
      
    
  
  
    
    
      org.springframework.boot
      spring-boot-starter-web
    
    
    
      org.springframework.cloud
      spring-cloud-starter-netflix-eureka-client
    
  
  
    
      spring-milestones
      Spring Milestones
      https://repo.spring.io/libs-milestone
      
        false
      
    
  

application.xml的配置如下:

###服务启动端口号
server:
  port: 8889
###服务名称(服务注册到eureka server名称)
spring:
    application:
        name: user
###服务注册到eureka server地址
eureka:
  client:
    service-url:
           defaultZone: http://localhost:8888/eureka


###这个是client 要注册自己的信息这里设置为true
    register-with-eureka: true
###是否需要从eureka上获取注册信息,要调用其他的服务这里就要设置成true 
    fetch-registry: true

启动的代码   @EnableEurekaClient 加上这个注解User就作为client注册服务发现服务。

@SpringBootApplication
@EnableEurekaClient
public class AppUser {

    public static void main(String[] args){
        SpringApplication.run(AppUser.class,args);
    }

}

这时候可以看到User服务已经注册进了Eureka Serverl里面

SpringCloud整合Eureka_第3张图片

接着将Pay服务也注册进Eureka Server. 流程和User是一样的。两个服务都注册进去了,最后就是两个服务的调用了。

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(微服务)