spring工程dubbo docker迁移

1 springmvc工程迁移
使用当当的dubbox,也就是dubbo2.8.4版本时,我们的配置可以这样加载,一方面可以批量加载spring的配置,另一方面可以动态加载dubbo的消费端配置


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">

	
	<import resource="classpath*:/spring/spring-*.xml" />
	<import resource="classpath*:/dubbo/dubbo-*.xml" />
beans>

将接口类和消费端配置打包在一起,这样只要引用这个api,就都可以使用了。
spring工程dubbo docker迁移_第1张图片




	

但是dubbo2.8.4并不支持docker化,因为没有k8s中dubbo注册IP问题中所属的环境变量。
直接切换jar到2.6.7版本,并不适用,因为他会不识别

1.1 web工程

<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>dubboartifactId>
			<version>2.6.7version>
		dependency>


		<dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-frameworkartifactId>
			<version>4.0.0version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeepergroupId>
					<artifactId>zookeeperartifactId>
				exclusion>
			exclusions>
		dependency>
		<dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-recipesartifactId>
			<version>4.0.0version>
		dependency>
		<dependency>
			<groupId>org.apache.zookeepergroupId>
			<artifactId>zookeeperartifactId>
			<exclusions>
				<exclusion>
					<artifactId>nettyartifactId>
					<groupId>io.nettygroupId>
				exclusion>
			exclusions>
		dependency>
		<dependency>
			<groupId>com.github.sgroschupfgroupId>
			<artifactId>zkclientartifactId>
		dependency>
	dependencies>

<dependency>
			<groupId>com.google.guavagroupId>
			<artifactId>guavaartifactId>
			
		dependency>

下面这个问题是curator相关的jar缺失造成的

Caused by: java.lang.NoClassDefFoundError: org/apache/curator/RetryPolicy
	at com.alibaba.dubbo.remoting.zookeeper.curator.CuratorZookeeperTransporter.connect(CuratorZookeeperTransporter.java:27)
	at com.alibaba.dubbo.remoting.zookeeper.ZookeeperTransporter$Adaptive.connect(ZookeeperTransporter$Adaptive.java)
	at com.alibaba.dubbo.registry.zookeeper.ZookeeperRegistry.<init>(ZookeeperRegistry.java:69)
	at com.alibaba.dubbo.registry.zook

spring-dubbo.xml,这里要注意实现类不要用dubbo的service,而要用spring的service,否则会出现两条注册信息,一个带版本号,另一个不带,且不带版本的没有消费者,消费者也访问不到
1


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="${domain}" owner="${dubbo.owner}"
                       organization="${dubbo.organization}" />

    
    <dubbo:registry address="${dubbo.registry.address}" />

    
    <dubbo:protocol name="dubbo" port="${dubbo.protocol}" />

    
    

    

    
    <dubbo:annotation package="com.test" />
    <dubbo:service interface="com.test.bdk.base.service.BdkWxService"
                   ref="bdkWxServiceImpl" validation="${dubbo.service.validation}"
                   version="${dubbo.service.version}" timeout="${dubbo.service.timeout}"
                   retries="${dubbo.service.retries}" loadbalance="${dubbo.service.loadbalance}"
                   actives="${dubbo.service.actives}" />

    <dubbo:service interface="com.test.bdk.base.service.BdkPgsService"
                   ref="bdkPgsServiceImpl" validation="${dubbo.service.validation}"
                   version="${dubbo.service.version}" timeout="${dubbo.service.timeout}"
                   retries="${dubbo.service.retries}" loadbalance="${dubbo.service.loadbalance}"
                   actives="${dubbo.service.actives}" />

beans>

1.2 dubbo的实现类工程

<dependency>
			<groupId>com.alibabagroupId>
			<artifactId>dubboartifactId>
			<version>2.6.7version>
			<exclusions>
				<exclusion>
					<artifactId>springartifactId>
					<groupId>org.springframeworkgroupId>
				exclusion>
			exclusions>
		dependency>

如果有dubbo2.8.4导致冲突,则将他的引用排除出去

<dependency>
			<groupId>com.testgroupId>
			<artifactId>test-baseartifactId>
			<version>1.0.0version>
			<exclusions>
				<exclusion>
					<artifactId>dubboartifactId>
					<groupId>com.alibabagroupId>
				exclusion>
			exclusions>
		dependency>

出现下面的问题即是guava缺失造成的

	tory.BeanCreationException: Could not autowire field: private com.test.bdk.base.service.BdkWxService com.test.bwn.base.rest.controller.StoreController.bdkWxService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wxService': FactoryBean threw exception on object creation; nested exception is java.lang.NoClassDefFoundError: com/google/common/base/Function
        at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)

1.3 生产者和消费者配置
这个不用改变

2 springboot工程docker迁移(采用dubbo.xml的方式)
2.1 配置文件
dubbo.xml使用nacos会加载不了变量,所以配置可以放在bootstrap.yml
2.2 启动工程
注意要排出dubbo2.8.4的版本

 <dependency>
            <groupId>com.alibaba.bootgroupId>
            <artifactId>dubbo-spring-boot-starterartifactId>
            <version>0.2.0version>
            <exclusions>
                <exclusion>
                    <artifactId>dubboartifactId>
                    <groupId>com.alibabagroupId>
                exclusion>
            exclusions>
        dependency>

启动类不作任何更改
如果使用加载配置,为防止多个消费者api的应用,可以采取下面的方式

import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
@RefreshScope
@ImportResource({"classpath*:dubbo/*.xml"
        ,"classpath*:spring/spring-*-api.xml"
})
public class DubboConfig {

dubbo.xml配置如下


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    <dubbo:application name="${domain}" owner="${dubbo.owner}"
                       organization="${dubbo.organization}" />

    
    <dubbo:registry address="${dubbo.registry.address}" check="${dubbo.reference.check}"/>

    
    <dubbo:protocol name="dubbo" port="${dubbo.registry.protocol}" />

    <dubbo:consumer check="false" />
    
    

    
    
    <dubbo:service interface="com.test.bzt.api.service.BztService"
                   ref="bztServiceImpl" validation="${dubbo.service.validation}"
                   version="${dubbo.service.version}" timeout="${dubbo.service.timeout}"
                   retries="${dubbo.service.retries}" loadbalance="${dubbo.service.loadbalance}"
                   actives="${dubbo.service.actives}" />
beans>

2.2 dubbo服务实现类工程
配置xml即可


			<dependency>
				<groupId>com.alibabagroupId>
				<artifactId>dubboartifactId>
				<version>${dubbo.version}version>
				<exclusions>
					<exclusion>
						<groupId>org.springframeworkgroupId>
						<artifactId>springartifactId>
					exclusion>
					<exclusion>
						<groupId>javax.servletgroupId>
						<artifactId>servlet-apiartifactId>
					exclusion>
					<exclusion>
						<groupId>log4jgroupId>
						<artifactId>log4jartifactId>
					exclusion>
				exclusions>
			dependency>
			<dependency>
			<groupId>org.apache.curatorgroupId>
			<artifactId>curator-frameworkartifactId>
			<version>4.0.0version>
			<exclusions>
				<exclusion>
					<groupId>org.apache.zookeepergroupId>
					<artifactId>zookeeperartifactId>
				exclusion>
			exclusions>
		dependency>
		<dependency>
			<groupId>org.apache.zookeepergroupId>
			<artifactId>zookeeperartifactId>
			
		dependency>
		<dependency>
			<groupId>com.github.sgroschupfgroupId>
			<artifactId>zkclientartifactId>
		dependency>

2.3 接口部分
与springmvc的一致
1.1
问题

下面这个问题应该是docker镜像使用的tomcat7导致的


SEVERE: Unable to process Jar entry [module-info.class] from Jar [jar:file:/usr/local/tomcat/webapps/bdk/WEB-INF/lib/log4j-api-2.10.0.jar!/] for annotations
org.apache.tomcat.util.bcel.classfile.ClassFormatException: Invalid byte tag in constant pool: 19
	at org.apache.tomcat.util.bcel.classfile.Constant.readConstant(Constant.java:97)
	at org.apache.tomcat.util.bcel.classfile.ConstantPool.<init>(ConstantPool.java:55)
	at org.apache.tomcat.util.bcel.classfile.ClassParser.readConstantPool(ClassParser.java:176)
	at org.apache.tomcat.util.bcel.classfile.ClassParser.parse(ClassParser.java:85)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2104)
	at org.apache.catalina.startup.ContextConfig.processAnnotationsJar(ContextConfig.java:1980)
FROM tomcat:8.0.47-jre8
MAINTAINER test
COPY xwiki /usr/local/tomcat/webapps/xwiki

FROM tomcat:7.0.67-jre8
MAINTAINER test
ENV TOMCAT_HOME /usr/local/tomcat
ADD server.xml $TOMCAT_HOME/conf/service.xml
ADD dubbo-admin.war $TOMCAT_HOME/webapps

3.2 问题
工程代码中是找不到,出现这个问题好像是因为当前这个工程即是生产者,又是消费者

Caused by: java.lang.IllegalStateException: Duplicate application configs: <dubbo:application name="peb-server" owner="test" organization="bwjf" id="peb-server" /> and <dubbo:application />
	at com.alibaba.dubbo.config.spring.ReferenceBean.afterPropertiesSet(ReferenceBean.java:109)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1761)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1698)
	... 17 common frames omitted

实际上并不是生产者、消费者问题导致的,而是dubbo-spring-boot-starter版本导致的0.2.1.RELEASE
0.2.1.RELEASE中无需设置,但是0.2.0则需要,为了兼容,我这边采用了0.2.0的版本


            com.alibaba.boot
            dubbo-spring-boot-starter
            0.2.0
            
                
                    dubbo
                    com.alibaba
                
            
        

3.2 dubbo兼容性问题
从下图可以看到使用dubbo2.6.7协议版本号是2.0.2
spring工程dubbo docker迁移_第2张图片
从下图可以看到dubbox使用的dubbo2.8.4,这两者协议版本不同,兼容应该会出现问题
1
德不孤,必有邻,这里dubbox升级至apache-dubbo-2.7.2的兼容方案遇到跟我一样的问题,按照此方法调整解码编码即可
注意

<dependency>
			<groupId>org.javassistgroupId>
			<artifactId>javassistartifactId>
			<version>3.20.0-GAversion>
		dependency>

你可能感兴趣的:(Web系统最佳实践,springmvc,docker)