升级背景

因项目使用的框架比较老,而且zk作为注册中心其实是有问题的,因为zk不是高可用的(详情请自行百度zk leader选举机制),经内部决定,将升级成nacos为注册中心(配置中心功能暂时不需要)

升级过程

版本选择

Spring Cloud Alibaba VersionSentinel VersionNacos VersionRocketMQ VersionDubbo VersionSeata Version
2.2.7.RELEASE1.8.12.0.34.6.12.7.131.3.0
2.2.6.RELEASE1.8.11.4.24.4.02.7.81.3.0
2021.1 or 2.2.5.RELEASE or 2.1.4.RELEASE or 2.0.4.RELEASE1.8.01.4.14.4.02.7.81.3.0
2.2.3.RELEASE or 2.1.3.RELEASE or 2.0.3.RELEASE1.8.01.3.34.4.02.7.81.3.0
2.2.1.RELEASE or 2.1.2.RELEASE or 2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.0
2.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.0
2.1.1.RELEASE or 2.0.1.RELEASE or 1.5.1.RELEASE1.7.01.1.44.4.02.7.30.9.0
2.1.0.RELEASE or 2.0.0.RELEASE or 1.5.0.RELEASE1.6.31.1.14.4.02.7.30.7.1
SCA版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
因项目springboot版本使用的是1.5.9.RELEASE,最终选择nacos版本1.4.1

将原本的zk注册中心的包注释掉

<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>-->
<!--            <version>1.2.0.RELEASE</version>-->
<!--        </dependency>-->

引入nacos注册中心的包

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependencyManagement>
    <dependencies>
        <!--整合springCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Edgware.SR4</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- 整合springcloud-alibaba-nacos(服务管理中心) -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>1.5.1.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    <!-- 暂时不需要配置中心 -->
    <!--  <dependency>-->
    <!--      <groupId>com.alibaba.cloud</groupId>-->
    <!--      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>-->
    <!--      <version>1.5.1.RELEASE</version>-->
    <!--  </dependency>-->
    </dependencies>
</dependencyManagement>

最后修改项目的nacos配置

spring:
  cloud: 
    nacos: 
      discovery:
        server-addr: localhost:8848

后续问题跟进

1.项目部署到服务上 发现客户端没有注册功能并且没有注册日志打印,服务端服务列表没有显示改服务注册上来,后排查发现和部署方式有关,在打war包部署到外置tomcat的时候,nacos会获取不到外部tomcat端口,从而导致注册失败而且没注册日志打印,解决方法:

新增配置类

import com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;
import java.lang.management.ManagementFactory;
import java.util.Set;

@Component
@Slf4j
public class NacosConfig implements ApplicationRunner {

    @Autowired(required = false)
    private NacosAutoServiceRegistration registration;

    @Value("${server.port}")
    Integer port;

    @Override
    public void run(ApplicationArguments args) {
        if (registration != null && port != null) {
            Integer tomcatPort = port;
            try {
                tomcatPort = new Integer(getTomcatPort());
            } catch (Exception e) {
                e.printStackTrace();
            }

            registration.setPort(tomcatPort);
            registration.start();
        }
    }

    /**
     * 获取外部tomcat端口
     */
    public String getTomcatPort() throws Exception {
        MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = beanServer.queryNames(new ObjectName("*:type=Connector,*"), Query.match(Query.attr("protocol"), Query.value("HTTP/1.1")));
        String port = objectNames.iterator().next().getKeyProperty("port");
        log.info("获取外部tomcat端口=={}", port);
        return port;
    }
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐