SpringCloud微服务又想变回单体怎么办

你好,我是柳岸花开。

在当今的企业级应用开发中,微服务架构因其灵活性和可扩展性而受到广泛欢迎。然而,随着业务需求的变化和系统复杂度的增加,部分企业开始探索将微服务架构合并为单体应用的可能性。本文将基于两个实际的Spring Boot配置示例,探讨如何实现这一转变,并分享一些最佳实践。

背景介绍

微服务架构通过将应用拆分为多个独立的服务,增强了系统的灵活性和可扩展性。然而,在某些场景下,将这些独立服务重新整合为单体应用可以简化部署和维护流程,尤其是在开发和测试环境中。

私有云部署模式的配置

在私有云部署模式下,所有服务和组件都打包在一个JAR包中,进行统一的部署和管理。以下是一个典型的配置示例:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
@ComponentScan(basePackages = {"com.bob.custom"}, nameGenerator = BeanNameGenerator.class)
@ConditionalOnDeployMode(mode 
= DeployModeEnum.MERGE)
public class MergeAutoConfiguration {

    @Configuration
    @AutoConfigureAfter(MergeAutoConfiguration.class)
    @ConditionalOnMissingBean(InternalOpenUserController.class)
    @ConditionalOnDeployMode(mode 
= DeployModeEnum.MERGE)
    public static class TestDuplicateConfiguration implements InitializingBean {

        @Override
        public void afterPropertiesSet() {
            throw new RuntimeException("In the pre-deployment environment, the controller implementation for the interface was not scanned. Please check if the deploy.mode configuration is correct and confirm if an incorrect scan path is configured in the code @ComponentScan");
        }
    }
}

关键点解析

  1. @ComponentScan:扫描并注册指定包下的组件,如controller、service、mapper等。
  2. @ConditionalOnDeployMode:根据部署模式条件进行配置,仅在 DeployModeEnum.MERGE模式下生效。
  3. TestDuplicateConfiguration:检查关键Controller是否存在于IOC容器中,如果缺失则抛出异常提醒配置错误。

公有云部署模式的配置

在公有云部署模式下,产品服务通过Feign调用服务提供接口,需要扫描和注册Feign客户端,同时避免扫描指定包下的组件。以下是一个典型的配置示例:

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;

@Slf4j
@Configuration
@ConditionalOnDeployMode(mode = DeployModeEnum.SPLIT)
@EnableFeignClients(basePackages = {"com.bob"})
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.bob\\..*"))
public class SplitAutoConfiguration {

    @Configuration
    @AutoConfigureAfter(SplitAutoConfiguration.class)
    @ConditionalOnBean(InternalOpenUserController.class)
    @ConditionalOnDeployMode(mode 
= DeployModeEnum.SPLIT)
    @ConditionalOnMissingBean(name = "platformApiApplication")
    public static class TestDuplicateConfiguration implements InitializingBean {

        @Override
        public void afterPropertiesSet() {
            throw new RuntimeException("In the cloud environment, the controller implementation for the interface was found. Please check if the deploy.mode configuration is correct and confirm if an incorrect scan path is configured in the code @ComponentScan");
        }
    }
}

关键点解析

  1. @EnableFeignClients:启用Feign客户端扫描和注册。
  2. @ComponentScan:通过排除过滤器避免扫描指定包下的组件。
  3. UcDuplicateConfiguration:在公有云环境中,如果检测到不应该存在的Controller,则抛出异常提醒配置错误。

从微服务到单体的转变

在私有云部署模式下,通过将所有服务和组件打包在一个JAR包中,我们可以实现将微服务架构合并为单体应用的效果。这种方式简化了开发和测试环境中的部署和维护流程。然而,在生产环境中,我们仍然可以保持公有云部署模式,通过Feign客户端进行服务调用,确保系统的灵活性和可扩展性。

总结

通过上述两种配置方式,我们可以根据不同的部署模式,灵活地调整Spring Boot应用的配置,满足从微服务到单体的转变需求。这不仅提高了系统的灵活性和可维护性,也为开发者提供了更多的选择。在实际开发中,可以根据具体的业务需求和部署环境,进一步优化和扩展这些配置策略,以实现最佳的系统架构。

👇关注我,下期了解👇 ​ SpringMVC源码 ​ ​ alt ​ ​ 回复 222,获取Java面试题合集 ​ 关于我 ​ 一枚爱折腾的Java程序猿,专注Spring干货。把路上的问题记录下来,帮助那些和我一样的人。 ​ 好奇心强,喜欢并深入研究古天文。 ​ 崇尚 个人系统创建,做一些时间越长越有价值的事情。思考 把时间留下来 又 每刻都是新的。

本文由 mdnice 多平台发布

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值