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

1:搭建eureka集群

   1.1:pom.xml中引入依赖

      (1)引入spring boot依赖

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

      (2)引入spring cloud依赖

<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)引入eureka

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

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

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

     (5)application.yml配置

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.yml

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,出现了相同的结果

c8bf41fec5ee5123c9d7ce120a6ae3e868c.jpg

可以看到只有8052端口注册上了,并没有8051这个端口

分析一下:server1启动时

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

转载于:https://my.oschina.net/u/3188070/blog/3007243

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值