< dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < optional> </ optional> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < scope> </ scope> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> </ dependencies> server : 
  port :  9001 
eureka :  
  instance : 
    hostname :  localhost 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //${ eureka.instance.hostname} : ${ server.port} /eureka 
package  com. sun. springcloud ; 
import  org. springframework. boot.  SpringApplication ; 
import  org. springframework. boot. autoconfigure.  SpringBootApplication ; 
import  org. springframework. cloud. netflix. eureka. server.  EnableEurekaServer ; 
@EnableEurekaServer  
@SpringBootApplication 
public  class  EurekaApplication  { 
    public  static  void  main ( String [ ]  args)  { 
        SpringApplication . run ( EurekaApplication . class ,  args) ; 
    } 
} 
 
 
        
        < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> server : 
  port :  10001  
spring : 
  application : 
    name :  member- service- provider- 10001  
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      defaultZone :  http: //localhost: 9001/eureka 
 
 
 
 
        
        < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> server : 
  port :  81  
spring : 
  application : 
    name :  member- service- consumer- 81 
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      defaultZone :  http: //localhost: 9001/eureka 
 
 
简单来说Eureka就两个功能,发现服务,注册服务! 基本流程就是会员中心启动之后将自己的信息注册到Eureka Server(注册服务),服务消费者启动之后通过Eureka Server来发现服务(发现服务),得到会员中心的调用地址,然后通过rpc远程调用 
 
 
服务在注册之后会不断地向服务端发送心跳,正常情况下如果90s内没有心跳,则服务端会认为这个客户端down掉了,就会清除记录 但是一旦自我保护机制开启,即使90s内没有心跳,也不会请求记录 server : 
  port :  9001 
eureka :  
  instance : 
    hostname :  localhost 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //${ eureka.instance.hostname} : ${ server.port} /eureka 
  server : 
    enable-self-preservation :  false  
    eviction-interval-timer-in-ms :  2000  
server : 
  port :  10001  
spring : 
  application : 
    name :  member- service- provider- 10001  
  datasource : 
    type :  com.alibaba.druid.pool.DruidDataSource
    
    url :  
    username :  
    password :  
mybatis : 
  mapper-locations :  classpath: mapper/*.xml  
  type-aliases-package :  com.sun.springcloud.entity 
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      defaultZone :  http: //localhost: 9001/eureka 
  instance : 
    lease-renewal-interval-in-seconds :  1  
    lease-expiration-duration-in-seconds :  2  
 
 
 
 
    < dependencies> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < optional> </ optional> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < scope> </ scope> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> </ dependency> < dependency> < groupId> </ groupId> < artifactId> </ artifactId> < version> </ version> </ dependency> </ dependencies> server : 
  port :  9002 
eureka :  
  instance : 
    hostname :  eureka9002.com 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/ 
server : 
  port :  9001 
eureka :  
  instance : 
    hostname :  eureka9001.com 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //eureka9002.com: 9002/eureka/ 
package  com. sun. springcloud ; 
import  org. springframework. boot.  SpringApplication ; 
import  org. springframework. boot. autoconfigure.  SpringBootApplication ; 
import  org. springframework. cloud. netflix. eureka. server.  EnableEurekaServer ; 
@SpringBootApplication 
@EnableEurekaServer 
public  class  EurekaApplication9002  { 
    public  static  void  main ( String [ ]  args)  { 
        SpringApplication . run ( EurekaApplication9002 . class ,  args) ; 
    } 
} 
由于上面配置的两个Eureka服务使用的是域名eureka9001.com和eureka9002.com 所以需要配置hosts文件 
 
这样eureka9002.com和eureka9001.com都表示127.0.0.1了 如果这里不能直接修改就把文件复制一份在别的地方修改一下然后覆盖这个文件即可 注意:你如果现在开的代理服务器,hosts文件是不生效的! 
 
 
 
server : 
  port :  9001 
eureka :  
  instance : 
    hostname :  eureka9001.com 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //eureka9002.com: 9002/eureka/ 
server : 
  port :  9002 
eureka :  
  instance : 
    hostname :  eureka9002.com 
  client :  
    register-with-eureka :  false  
    fetch-registry :  false  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/ 
 
server : 
  port :  81  
spring : 
  application : 
    name :  member- service- consumer- 81 
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/,  http: //eureka9002.com: 9002/eureka/
server : 
  port :  10001  
spring : 
  application : 
    name :  member- service- provider- 10001  
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/,  http: //eureka9002.com: 9002/eureka/  
 
 
 
server : 
  port :  81  
spring : 
  application : 
    name :  member- service- consumer- 81  
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/,  http: //eureka9002.com: 9002/eureka/
server : 
  port :  10001  
spring : 
  application : 
    name :  member- service- provider- 10001  
eureka :  
  client : 
    register-with-eureka :  true  
    fetch-registry :  true  
    service-url : 
      
      defaultZone :  http: //eureka9001.com: 9001/eureka/,  http: //eureka9002.com: 9002/eureka/  
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    
    @GetMapping ( "/member/consumer/discovery" ) 
    public  Object  discovery ( )  { 
        
        List < String > =  discoveryClient. getServices ( ) ; 
        for  ( String  service :  services)  { 
            log. info ( "服务名小写={}" ,  service) ; 
            
            List < ServiceInstance > =  discoveryClient. getInstances ( service) ; 
            for  ( ServiceInstance  instance :  instances)  { 
                log. info ( "id={}, host={}, port={}, uri={}" ,  service,  instance. getHost ( ) ,  instance. getPort ( ) ,  instance. getUri ( ) ) ; 
            } 
        } 
        return  services; 
    }