在一般情况下,我们会将系统拆分为几个部分,每个部分作为一个服务多实例部署,以此来保证系统的高可用。而随着业务调整,部分服务可能仅提供给内部其他服务,不需要单独作为一个服务去部署,而是使用以jar包方式作为依赖供其他服务使用。
那么假设现在有两个服务A和B,由于业务调整,B需要打成jar包,A引入B.jar,且A、B都是maven项目。
改造服务提供方B
1.修改pom文件
打包方式改为jar
使用war或zip的改成jar
<packaging>jar</packaging>
打包时排除启动类
当服务A依赖B服务时,B.jar如果有启动类,Spring容器会加载两次,导致的结果就是B里面所有的bean会有两个然后启动失败。如果B服务中controller不想暴露出来,也可以在这里排除
<build>
<!--打包项目包名-->
<finalName>${project.artifactId}</finalName>
<!-- 打包插件仅保留maven插件,如果有springboot插件,jar包目录不正确,则无法正确引包 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<excludes>
<exclude>
<!-- 打包时排除启动类 -->
**/Application.java
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
改造服务调用方A
1.修改pom文件
依赖引入B
<dependency>
<groupId>com.example</groupId>
<artifactId>B</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
2.修改启动类
把扫描包的加上扫描B的包,并且设置命名nameGenerator 为FullyQualifiedAnnotationBeanNameGenerator.class
。
(1)@SpringBootApplication
如果要使用B中的Service,就必须扫描B的包。在spring加载bean的时候,默认给bean的名称是类名首字母小写后的名称例如bServiceImpl。
如果A和B中有相同的类名就会冲突导致如下的异常,因此需要修改bean的名称生成方式。
Caused by: org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'XServiceImpl' for bean class [com.example.a.XServiceImpl] conflicts with existing, non-compatible bean definition of same name and class [com.example.b.XServiceImpl]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:132)
at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:296)
at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:250)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:207)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:175)
我们使用全限定名的方式FullyQualifiedAnnotationBeanNameGenerator.class
,这样的bean名称是在默认名称前面加上包路径例如com.example.b.bServiceImpl。
@SpringBootApplication(scanBasePackages = {"com.example.a", "com.example.b"}, nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
(2)@MapperScan
同理,如果你使用了mybatis的@MapperScan
扫描包且有同名的mapper,可能因为它是mybatis的注解,上面Spring的注解并不对它生效,所以会导致这样的异常。
org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean name 'XMapper' for bean class [com.example.a.XMapper] conflicts with existing, non-compatible bean definition of same name and class [com.example.b.XMapper]
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.checkCandidate(ClassPathBeanDefinitionScanner.java:349)
at org.mybatis.spring.mapper.ClassPathMapperScanner.checkCandidate(ClassPathMapperScanner.java:256)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:287)
at org.mybatis.spring.mapper.ClassPathMapperScanner.doScan(ClassPathMapperScanner.java:181)
at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:254)
at org.mybatis.spring.mapper.MapperScannerConfigurer.postProcessBeanDefinitionRegistry(MapperScannerConfigurer.java:356)
@MapperScan(value = {"com.example.**.mapper"}, nameGenerator = FullyQualifiedAnnotationBeanNameGenerator.class)
(3)@EnableFeignClients
如果B有用feign
@EnableFeignClients(value = {"com.example.a.feign", "com.example.b.feign"})
(4)@ServletComponentScan
如果B有扫描过滤器、监听器之类的
@ServletComponentScan(basePackages = {"com.example.a", "com.example.b"})
3.修改feign调用
删除BFeign接口以及相应的fallback或fallbackfactory。原来A通过feign调用B,现在可以直接注入B的service,如果调用的代码过多,改动很麻烦,可以写一个实现类BFeign接口,但是BFeign接口需要去掉@FeignClient
,在实现类里调用B的方法,这样改动小。
4.配置文件
启动A时,spring并不会读取B.jar里的application.properties,也就是说需要将B中有但A中没有的配置copy过来。