Eureka实战-1【Eureka Server在线扩容】

1、准备工作

PS:为了偷懒,每个pom文件都要依赖的公共依赖配置放在下面:


   org.springframework.boot
   spring-boot-starter-parent
   2.0.3.RELEASE
    



   UTF-8
   UTF-8
   1.8
   Finchley.RELEASE



   
      
         org.springframework.cloud
         spring-cloud-dependencies
         ${spring-cloud.version}
         pom
         import
      
   

 

1.1、由于需要动态修改配置,这里需要创建config-server工程,pom依赖如下:


        
            org.springframework.cloud
            spring-cloud-config-server
        
  



org.springframework.boot
spring-boot-maven-plugin


 

1.2、config-server工程启动类如下:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

/**
 * 程序入口
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

 

1.3、添加项目配置文件,资源路径:config-server\src\main\resources\bootstrap.yml

spring:
  application:
    name: config-server
  profiles:
    active: native
server:
  port: 8888

这里如果不明白bootstrap.yml/properties与application.yml/properties的文件区别,这里推荐看一下这篇文章:https://blog.csdn.net/ThinkWon/article/details/100007093

为了便于演示效果,这里使用native的profile,即使用文件来存储配置,默认存放位置是resources\config目录下。另外为了演示eureka server的动态扩容,还需要创建eureka-server工程和eureka-client工程。

 

1.4、eureka-server工程,pom配置如下:


        
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        
        
            org.springframework.cloud
            spring-cloud-starter-config
        



       
            
                org.springframework.boot
                spring-boot-maven-plugin
            
       

 

1.4.1、eureka-server工程启动类:

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

 

1.4.2、eureka-server工程配置文件,路径:eureka-server\src\main\resources\bootstrap.yml,eureka-server\src\main\resources\application.yml

bootstrap.yml:

spring:
  application:
    name: eureka-server
  cloud:
    config:
      uri: http://localhost:8888
management:
  endpoints:
    web:
      exposure:
        include: '*'

application.yml:

eureka:
  server:
    peer-eureka-nodes-update-interval-ms: 10000 #默认是10分钟即600000,这里为了验证改为10秒,
#    指定peerUpdateTask调度的时间间隔,用于从配置文件刷新peerEurekaNodes节点的配置信息

 

1.5、eureka-client工程,pom文件配置如下:


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



       
            
                org.springframework.boot
                spring-boot-maven-plugin
            
       

 

1.5.1、eureka-client工程启动类:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaClientConfigBean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/query")
@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClientApplication {

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

    @Autowired
    private EurekaClientConfigBean eurekaClientConfigBean;

    @GetMapping("/eureka-server")
    public Object getEurekaServerUrl() {
        return eurekaClientConfigBean.getServiceUrl();
    }
}

 

1.5.2、eureka-client工程配置文件,路径:eureka-client\src\main\resources\bootstrap.yml,eureka-client\src\main\resources\application.yml

bootstrap.yml:

spring:
  application:
    name: eureka-client
  cloud:
    config:
      uri: http://localhost:8888
management:
  endpoints:
    web:
      exposure:
        include: '*'

application.yml:

eureka:
  client:
    eureka-service-url-poll-interval-seconds: 10 #默认为300秒,这里为了验证改为10秒

 

2、添加config-server配置文件

2.1、添加eureka-client配置文件,路径:config-server\src\main\resources\config\eureka-client.yml

server:
  port: 8081

spring:
  application:
    name: eureka-client1

eureka:
  client:
    serviceUrl:
#      defaultZone: http://localhost:8761/eureka/ # one eureka server
#      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # two eureka server
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/ # three eureka server

 

2.2、添加eureka-server配置文件,路径:config-server\src\main\resources\config\eureka-server-peer1.yml,config-server\src\main\resources\config\eureka-server-peer2yml,config-server\src\main\resources\config\eureka-server-peer3.yml

config-server\src\main\resources\config\eureka-server-peer1.yml:

server:
  port: 8761

spring:
  application:
    name: eureka-server
eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
#      defaultZone: http://localhost:8761/eureka/ # one eureka server
#      defaultZone: http://localhost:8762/eureka/ # two eureka server
      defaultZone: http://localhost:8762/eureka/,http://localhost:8763/eureka/ # three eureka server
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

config-server\src\main\resources\config\eureka-server-peer2.yml:

server:
  port: 8762

eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
#      defaultZone: http://localhost:8761/eureka/ # two eureka server
      defaultZone: http://localhost:8761/eureka/,http://localhost:8763/eureka/ # three eureka server
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

config-server\src\main\resources\config\eureka-server-peer3.yml:

server:
  port: 8763

eureka:
  instance:
    hostname: localhost
    preferIpAddress: true
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/ # three eureka server
  server:
      waitTimeInMsWhenSyncEmpty: 0
      enableSelfPreservation: false

从配置文件可以看出,eureka-server一共有三个实例,都是两两互相注册,1-2、2-3、1-3。

 

3、启动工程

分别启动工程:config-server,eureka-server,eureka-client。

启动config-server命令:

  mvn spring-boot:run

启动eureka-server命令:

  mvn spring-boot:run -Dspring.profiles.active=peer1

  mvn spring-boot:run -Dspring.profiles.active=peer2

  mvn spring-boot:run -Dspring.profiles.active=peer3

按照profiles启动的时候会报错

2019-10-03 17:10:07.519 ERROR 15853 --- [           main] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_EUREKA-SERVER/10.2.240.30:eureka-server:8761 - was unable to refresh its cache! status = Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1051) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:965) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.DiscoveryClient.(DiscoveryClient.java:414) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.DiscoveryClient.(DiscoveryClient.java:269) [eureka-client-1.9.2.jar:1.9.2]
    at org.springframework.cloud.netflix.eureka.CloudEurekaClient.(CloudEurekaClient.java:63) [spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:269) [spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$ee9298f0.CGLIB$eurekaClient$0() [spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$ee9298f0$$FastClassBySpringCGLIB$$c9690964.invoke() [spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) [spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) [spring-context-5.0.7.RELEASE.jar:5.0.7.RELEASE]
    at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$ee9298f0.eurekaClient() [spring-cloud-netflix-eureka-client-2.0.0.RELEASE.jar:2.0.0.RELEASE]

2019-10-03 17:10:12.759 ERROR 15853 --- [nfoReplicator-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEncodingFilter.java:123) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.register(AbstractJerseyEurekaHttpClient.java:56) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.java:73) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.java:118) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:79) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:120) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829) [eureka-client-1.9.2.jar:1.9.2]
    at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:121) [eureka-client-1.9.2.jar:1.9.2]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_151]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_151]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_151]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_151]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_151]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151]

这是因为这里为了后续演示扩容方便,peer1的fetchRegistry及fetchRegistry的属性设置为true的缘故,如果是standalone的eureka server,不想看到这个错误,可以自行设置为false。

访问:localhost:8761

可以看到,此时eureka-server已经有了三个实例。

启动eureka-client命令:

  mvn spring-boot:run

访问:localhost:8081/query/eureka-server/

 可以看到,得到是三个server实例,为了方便这里使用的是native的profile,如果有修改文件,需要重启后才能生效,如果使用的是git仓库或者是GitHub仓库,则无需重启config-server。 

 

你可能感兴趣的:(Eureka实战-1【Eureka Server在线扩容】)