参考阿里云的EDAS开发文档: 使用 Ali-Tomcat 开发应用
我们自己在内网搭建CentOS7 的测试环境,需要的资源如下:
假设我们要在内网的一台CentOS7 的PC机上部署shop.war、goods.war、user.war,
如何实现?看下面的步骤:
1.安装配置中心(查看服务列表、状态,实现服务的注册、发现):
a). 从前面所需资源链接下载配置中心,通过ftp等工具放到CentOS机器上,启动
b). 启动 EDAS 配置中心将会占用此台机器的 8080 和 9600 端口,因此确认 8080 和 9600 端口未被使用。
2.安装Ali-Tomcat、Pandora容器:
通过ftp等工具把前面链接下载的Ali-Tomcat、Pandora容器放到CentOS机器上,并且把Pandora 容器放到Ali-Tomcat的deploy目录下解压,最终目录路径如下:
taobao-tomcat-7.0.59
├── bin
│ ├── bootstrap.jar
│ ├── catalina.bat
│ ├── catalina.sh
│ ├── ...
│ ├── setenv.sh >>这个文件是手动加上去的,后面会有介绍
│ ├── ...
│ └── version.sh
├── catalina.pid
├── conf
│ ├── Catalina
│ ├── catalina-legacy.properties
│ ├── catalina.policy
│ ├── catalina.properties
│ ├── context.xml
│ ├── logging.properties
│ ├── server.xml
│ ├── tomcat-users.xml
│ └── web.xml
├── deploy
│ ├── admin
│ ├── admin.war
│ ├── taobao-hsf.sar >>这个就是Pandora容器解压后的目录
├── lib
│ ├── annotations-api.jar
│ ├── catalina-ant.jar
│ ├── catalina-ha.jar
│ ├── ...
│ ├── tomcat-jdbc.jar
│ ├── tomcat-util.jar
│ └── websocket-api.jar
├── LICENSE
├── logs
│ ├── agent
│ ├── catalina.log.2018-10-10
│ ├── catalina.out
│ ├── hsf.lock
│ └── localhost.log.2018-10-10
├── NOTICE
├── RELEASE-NOTES
├── RUNNING.txt
├── temp
│ └── safeToDelete.tmp
├── tools
│ ├── findclass.py
│ └── hotcode2.jar
├── webapps
│ ├── docs
│ ├── examples
│ ├── host-manager
│ ├── manager
│ └── ROOT
└── work
└── Catalina
3.配置中心需要设置hosts里面的域名映射:本机ip
jmenv.tbsite.net
4.本次需要部署三个war包,所以要再复制2个如上所示的taobao-tomcat-7.0.59并重命名文件夹,或者不重命名(放到不同目录下)。
由于都是独占式部署的,所以每个taobao-tomcat 启动后的端口不能一样,需要修改每个tomcat目录下面的conf里面的server.xml,改掉其中的
Server port、Connector port、redirectPort
这三个端口,保证三个taobao-tomcat的这个配置都不一样。
5.由于每个Ali-Tomcat启动都需要实例化一个Pandora,默认端口是12200,会出现第一个war包启动后,
其服务可以正常出现在配置中心的服务列表中,后续的war包都无法正常启动部署,因为端口冲突,
所以在启动tomcat时要配置JVM参数制定这个端口:在每个taobao-tomcat里面的bin目录,添加一个setenv.sh文件,这个会在启动tomcat时自动被调用,
设置如下内容(注意其中的端口号,三个tomcat里面的配置需要不一样,否则无法正常启动):
#!/bin/sh#!/bin/sh
#添加JVM选项
JAVA_OPTS=" -Dhsf.server.port=12205 "
有个小插曲:这三个tomcat里面配置的hsf服务端口,不要刚好只是相差1(比如可以分别设置为12210/12220/12230),
因为一个服务会占用两个端口,一个是上面这个配置文件配置的,还要占用顺延递增一个端口号,比如下图:
如果因为这个端口冲突,会导致taobao-tomcat启动失败,日志如下(实例内容可能会不一样,这里因为用到了redis所以打印的出错的是redis日志):
2018-10-11 14:31:31,460 org.apache.catalina.loader.WebappClassLoader loadClass
INFO: Illegal access: this web application instance has been stopped already. Could not load redis.clients.jedis.Client. The eventual following stack trace is caused by an error thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access, and has no functional impact.
Exception in thread "commons-pool-EvictionTimer" java.lang.NoClassDefFoundError: redis/clients/jedis/Client
at redis.clients.jedis.BinaryJedis.<init>(BinaryJedis.java:57)
at redis.clients.jedis.Jedis.<init>(Jedis.java:42)
at redis.clients.jedis.JedisFactory.makeObject(JedisFactory.java:82)
at org.apache.commons.pool2.impl.GenericObjectPool.create(GenericObjectPool.java:861)
at org.apache.commons.pool2.impl.GenericObjectPool.ensureIdle(GenericObjectPool.java:920)
at org.apache.commons.pool2.impl.GenericObjectPool.ensureMinIdle(GenericObjectPool.java:899)
at org.apache.commons.pool2.impl.BaseGenericObjectPool$Evictor.run(BaseGenericObjectPool.java:1036)
at java.util.TimerThread.mainLoop(Timer.java:555)
at java.util.TimerThread.run(Timer.java:505)
Caused by: java.lang.ClassNotFoundException: redis.clients.jedis.Client
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1722)
at com.taobao.tomcat.container.context.loader.AliWebappClassLoader.loadClass(AliWebappClassLoader.java:81)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
... 9 more
更准确的日志,可以从taobao-tomcat的logs/localhost.log.2018-10-11 日志查看,例如:
2018-10-11 14:31:23,024 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
java.lang.RuntimeException: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.RuntimeException: Failed to start HSF server! ERR-CODE: [HSF-0016], Type: [ENV], More: [http://edas.console.aliyun.com/code?faq=HSF-0016]
at com.taobao.hsf.app.api.util.HSFApiProviderBean.publish(HSFApiProviderBean.java:518)
at com.taobao.hsf.app.spring.util.HSFSpringProviderBean.onApplicationEvent(HSFSpringProviderBean.java:387)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:883)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:443)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:325)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5028)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5547)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:649)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1086)
at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1891)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.RuntimeException: Failed to start HSF server! ERR-CODE: [HSF-0016], Type: [ENV], More: [http://edas.console.aliyun.com/code?faq=HSF-0016]
at com.taobao.hsf.model.metadata.ServiceMetadata.exportSync(ServiceMetadata.java:351)
at com.taobao.hsf.app.api.util.HSFApiProviderBean.publish(HSFApiProviderBean.java:516)
... 23 more
Caused by: java.util.concurrent.ExecutionException: java.lang.RuntimeException: java.lang.RuntimeException: Failed to start HSF server! ERR-CODE: [HSF-0016], Type: [ENV], More: [http://edas.console.aliyun.com/code?faq=HSF-0016]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at com.taobao.hsf.threadpool.impl.SingleThreadScheduledExecutor$RunnableScheduleFutureDecorator.get(SingleThreadScheduledExecutor.java:198)
at com.taobao.hsf.model.metadata.ServiceMetadata.exportSync(ServiceMetadata.java:341)
... 24 more
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Failed to start HSF server! ERR-CODE: [HSF-0016], Type: [ENV], More: [http://edas.console.aliyun.com/code?faq=HSF-0016]
at com.taobao.hsf.model.metadata.ServiceMetadata.export(ServiceMetadata.java:315)
at com.taobao.hsf.model.metadata.ServiceMetadata$1.call(ServiceMetadata.java:336)
at com.taobao.hsf.model.metadata.ServiceMetadata$1.call(ServiceMetadata.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at com.taobao.hsf.threadpool.impl.SingleThreadScheduledExecutor$RunnableScheduleFutureDecorator.run(SingleThreadScheduledExecutor.java:169)
... 3 more
Caused by: java.lang.RuntimeException: Failed to start HSF server! ERR-CODE: [HSF-0016], Type: [ENV], More: [http://edas.console.aliyun.com/code?faq=HSF-0016]
at com.taobao.hsf2dubbo.DubboProtocol.registerProvider(DubboProtocol.java:72)
at com.taobao.hsf2dubbo.DubboProtocol.export(DubboProtocol.java:45)
at com.taobao.hsf.protocol.MultiplexingProtocol.export(MultiplexingProtocol.java:48)
at com.taobao.hsf.edas.tps.component.WhiteListTPSRuleProtocolInterceptor.export(WhiteListTPSRuleProtocolInterceptor.java:51)
at com.taobao.hsf.service.ServicePubComponent.export(ServicePubComponent.java:35)
at com.taobao.hsf.edas.tps.component.TPSRuleProtocolInterceptor.export(TPSRuleProtocolInterceptor.java:38)
at com.taobao.hsf.tps.component.TPSRuleProtocolInterceptor.export(TPSRuleProtocolInterceptor.java:38)
at com.taobao.hsf.protocol.AbstractDelegateProtocolInterceptor.export(AbstractDelegateProtocolInterceptor.java:26)
at com.taobao.hsf.registry.RegistryProtocolInterceptor.export(RegistryProtocolInterceptor.java:42)
at com.taobao.hsf.grouping.component.GroupingProtocolInterceptor.export(GroupingProtocolInterceptor.java:55)
at com.taobao.hsf.protocol.AbstractDelegateProtocolInterceptor.export(AbstractDelegateProtocolInterceptor.java:26)
at com.taobao.hsf.route.protocol.MachineGroupProtocolInterceptor.export(MachineGroupProtocolInterceptor.java:34)
at com.taobao.hsf.plugins.eagleeye.protocol.EagleEyeProtocolInterceptor.export(EagleEyeProtocolInterceptor.java:37)
at com.taobao.hsf.process.component.CodeployProtocolInterceptor.export(CodeployProtocolInterceptor.java:31)
at com.taobao.hsf.protocol.DelayPublishProtocolInterceptor.export(DelayPublishProtocolInterceptor.java:26)
at com.taobao.hsf.metadata.store.MetadataReportProtocolInterceptor.export(MetadataReportProtocolInterceptor.java:46)
at com.taobao.hsf.model.metadata.ServiceMetadata.export(ServiceMetadata.java:310)
... 9 more
Caused by: com.taobao.hsf.exception.HSFException:
error message : fail to start HSF remoting server module
at com.taobao.hsf.io.provider.impl.ProviderServerImpl.startHSFServer(ProviderServerImpl.java:95)
at com.taobao.hsf2dubbo.DubboProtocol.registerProvider(DubboProtocol.java:61)
... 25 more
Caused by: java.net.BindException: 地址已在使用
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:128)
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558)
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1283)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
at com.taobao.hsf.io.netty.server.NettyBindHandler.bind(NettyBindHandler.java:58)
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501)
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486)
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:989)
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254)
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:365)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:403)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:463)
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:858)
at com.taobao.hsf.io.netty.util.PooledThreadFactory$PooledByteBufRunnable.run(PooledThreadFactory.java:37)
... 1 more
2018-10-11 14:31:23,028 org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
2018-10-11 14:31:23,041 org.apache.catalina.core.ApplicationContext log
INFO: Shutting down Logback
6.最后,把三个war包分别放到taobao-tomcat的deploy目录,跟taobao-hsf.sar目录同级,依次启动配置中心、三个tomcat即可