spring cloud踩坑之旅(1)搭建eureka集群并利用spring cloud config实现eureka集群在线扩容

1:搭建eureka集群

   1.1:pom.xml中引入依赖git

      (1)引入spring boot依赖github

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.3.RELEASE</version>
    </parent>

      (2)引入spring cloud依赖web

<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Camden.SR4</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

      (3)引入eurekaspring

<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
    </dependencies>

     (4)添加spring-boot的maven插件app

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

     (5)application.yml配置maven

server:
  port: 8051
eureka:
  client:
    fetch-registry: false   #是否注册本身,当没有其余节点启动时,因改成false,不注册本身
    register-with-eureka: false   #同上
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8052/eureka/  #若是是单节点 应当写的是本身 http://localhost:8051/eureka/ 
  server:
     enable-self-preservation: false
     eviction-interval-timer-in-ms: 1000  #刷新时间
  instance:
     hostname: sever1

       (5)新建项目重复(1)-(3)并修改application.ymlspring-boot

server:
  port: 8052
eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    healthcheck:
      enabled: true
    serviceUrl:
      defaultZone: http://localhost:8051/eureka/
  server:
     enable-self-preservation: false
     eviction-interval-timer-in-ms: 1000
  instance:
     hostname: sever2

      (6)先启动server1,在启动server2,访问localhost:8051,localhost:8052,出现了相同的结果fetch

能够看到只有8052端口注册上了,并无8051这个端口ui

分析一下:server1启动时this

eureka.client.serviceUrl.defaultZone=http://localhost:8052/eureka/

这个属性意思是将本身做为一个服务注册到8052上,可是如今8052并无启动,因此server1没有注册到server2上,又由于启动时本身没有注册本身,因此无论是在server1仍是server2上都没有发现8051这个的端口,那么将server1的application.yml修改

eureka.client.fetch-registry=true
eureka.client..register-with-eureka=true

从新启动server1,那么这个时候server1和server2就相互注册造成了一个双节点的eureka集群

2:eureka集群不重启在线扩容

通过以上了解发现了个问题,先要启动server1,在启动server2,而后修改server1的配置,重启才会互相注册造成集群。由此思考,若是须要添加一个节点server3,你那么要同时修改各自的配置文件

server1

eureka.client.serviceUrl.defaultZone=http://localhost:8052/eureka/,http://localhost:8053/eureka/

server2

eureka.client.serviceUrl.defaultZone=http://localhost:8051/eureka/,http://localhost:8053/eureka/

而后重启,那么有什么办法使改变了配置文件,不重启服务就能生效呢?

首先咱们知道在项目启动时会加载配置文件的信息,将配置文件的信息存储到了内存里。之后进程在内存里读取,因此在不重启的状况下,改变了配置文件的参数进程并不知道改变了,由于修改的参数没有放进内存里。

通过百度有许多解决方法,可是不是过于复杂,就是复杂,又是注解,又是反射。这篇博文简单耐操的解决了这个问题

https://blog.csdn.net/qq_27385301/article/details/82716218

增长一个controller

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.context.refresh.ContextRefresher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {
  @Value("${eureka.client.serviceUrl.defaultZone}")
  private String profile;
  @Autowired
  private ContextRefresher contextRefresher;

  @GetMapping("/profile")
  public String hello() {
    contextRefresher.refresh();
    return this.profile;
  }
}

修改配置文件以后访问  /profile,执行 contextRefresher.refresh();就将配置文件从新加载到内存里,不重启就生效了。

为了更方便的管理配置文件能够启用spring cloud config对配置文件进行统一管理

搭建spring cloud config server

(1)引入依赖,其余同上

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
</dependencies>

(2)启动类加入注解

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);
  }
}

(3)配置文件

server:
  port: 7001
spring:
  application:
    name: framework-config-server
  profiles:
    active: native #本地
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/config
        #git:
          #uri: http://123.206.58.154:3000/admintelecom/framework.git
          #username:
          #password:

(4)将server1和server2的配置文件复制进resources/config下,并启动

(5)将server1和server2的application.yml清空,并新建bootstrat.yml

spring:
  application:
    name: product-eureka-server
  cloud:
    config:
      name: eureka-server1 #eureka-server2   这个是配置中心的配置文件 好比是eureka-server1-dev.yml
      uri: http://localhost:7001/
      profile: dev #文件的后缀

(6)启动server1,启动server2,修改config server上的eureka-server1-dev.yml使server1注册,而后访问http://server1的地址/profile刷新配置

这样就实现了不重启eureka节点在线扩容的功能

源码:https://github.com/DisMirror/shooter

相关文章
相关标签/搜索