当前位置: 首页 > news >正文

thinkphp做双语网站外包公司值得去吗

thinkphp做双语网站,外包公司值得去吗,个人做动漫资源网站有哪些,有在网上找做网站的人么Spring Boot 构建war 部署到tomcat下无法在Nacos中注册服务 1. 问题2. 分析3. 解决方案参考 1. 问题 使用Nacos作为注册中心的Spring Boot项目,以war包形式部署到服务器上,启动项目发现该服务无法在Nacos中注册。 2. 分析 SpringCloud 项目打 war 包部…

Spring Boot 构建war 部署到tomcat下无法在Nacos中注册服务

  • 1. 问题
  • 2. 分析
  • 3. 解决方案
  • 参考

1. 问题

使用Nacos作为注册中心的Spring Boot项目,以war包形式部署到服务器上,启动项目发现该服务无法在Nacos中注册。

2. 分析

SpringCloud 项目打 war 包部署时,也就是使用外部 Tomcat 部署,其启动命令、端口等是由外部容器 Tomcat 配置的,而 Nacos 或者其他服务注册方式需要当前项目的端口号用于注册微服务。

以 Nacos 为例,其自动注册微服务的类是 NacosAutoServiceRegistration,我们看一下它的源码:

public class NacosAutoServiceRegistration extends AbstractAutoServiceRegistration<Registration> {private NacosRegistration registration;@Deprecatedpublic void setPort(int port) {this.getPort().set(port);}protected NacosRegistration getRegistration() {if (this.registration.getPort() < 0 && this.getPort().get() > 0) {this.registration.setPort(this.getPort().get());}Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set");return this.registration;}

我们看到 NacosAutoServiceRegistration 使用了 this.registration.setPort(this.getPort().get()); 来设置端口号。

而端口号是从其父类 AbstractAutoServiceRegistration 中的方法获取的:

public abstract class AbstractAutoServiceRegistration<R extends Registration>implements AutoServiceRegistration, ApplicationContextAware,ApplicationListener<WebServerInitializedEvent> {private AtomicInteger port = new AtomicInteger(0);@Deprecatedpublic void bind(WebServerInitializedEvent event) {ApplicationContext context = event.getApplicationContext();if (context instanceof ConfigurableWebServerApplicationContext) {if ("management".equals(((ConfigurableWebServerApplicationContext) context).getServerNamespace())) {return;}}this.port.compareAndSet(0, event.getWebServer().getPort());this.start();}

这段代码监听了内置容器启动完成事件,监听获取到容器端口后,向注册中心注册服务。

因此,当使用外部容器时,如此处的 Tomcat 来部署项目,AbstractAutoServiceRegistration 就不能监听到容器启动事件了,也就不会尝试向服务注册中心注册当前这个微服务,那么注册就失败了,并且也就没有异常信息了。

3. 解决方案

自定义获取获取外部容器端口的方法, 然后监听应用启动事件,当应用被启动时,获取外部容器启动的端口号,然后将这个 port 设置到 NacosAutoServiceReigistration 中。

具体操作流程:
Spring Boot提供了ApplicationRunner接口,是在应用起好之后执行一些初始化动作。通过这个接口我们可以实现启动项目后注册服务。使用这种方法,需要在配置文件中配置端口号,如果一个应用部署很多端口,每个应用都要配置,很不方便。故可获取外部tomcat自动设置端口。经测试,方法可行。
在启动类同级目录,复制下面代码即可(不需要修改,直接构建war即可)

import java.lang.management.ManagementFactory;
import java.util.Set;import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.Query;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 com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration;
import com.alibaba.cloud.nacos.registry.NacosRegistration;import lombok.extern.slf4j.Slf4j;/*** 项目打包war情况下部署外部tomcat,需该方式注册nacos*/
@Component
@Slf4j
public class NacosRegisterOnWar implements ApplicationRunner {@Autowiredprivate NacosRegistration registration;@Autowiredprivate NacosAutoServiceRegistration nacosAutoServiceRegistration;@Value("${server.port}")Integer port;@Overridepublic void run(ApplicationArguments args) throws Exception {if (registration != null && port != null) {Integer tomcatPort = port;try {tomcatPort = new Integer(getTomcatPort());} catch (Exception e) {log.warn("获取外部Tomcat端口异常:", e);}registration.setPort(tomcatPort);nacosAutoServiceRegistration.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");return port;}
}

提示:
在部署项目要注意版本问题,如Spring Boot 2.0.6应该部署在tomcat8以上版本

参考

Spring Boot 构建war 部署到tomcat下无法在Nacos中注册服务


文章转载自:
http://congealment.hjyw.cn
http://tipster.hjyw.cn
http://chowhound.hjyw.cn
http://antisepticise.hjyw.cn
http://radication.hjyw.cn
http://debussyan.hjyw.cn
http://indulgently.hjyw.cn
http://whirleybird.hjyw.cn
http://useable.hjyw.cn
http://long.hjyw.cn
http://calved.hjyw.cn
http://neotype.hjyw.cn
http://pantomimist.hjyw.cn
http://gibberish.hjyw.cn
http://samurai.hjyw.cn
http://anterolateral.hjyw.cn
http://nowhence.hjyw.cn
http://semidarkness.hjyw.cn
http://uremic.hjyw.cn
http://puck.hjyw.cn
http://indecorum.hjyw.cn
http://unmounted.hjyw.cn
http://prepositor.hjyw.cn
http://verifiable.hjyw.cn
http://irrationally.hjyw.cn
http://classmate.hjyw.cn
http://diffractometer.hjyw.cn
http://oni.hjyw.cn
http://decimillimeter.hjyw.cn
http://vasa.hjyw.cn
http://olg.hjyw.cn
http://tubulose.hjyw.cn
http://frankfort.hjyw.cn
http://decree.hjyw.cn
http://skutterudite.hjyw.cn
http://libber.hjyw.cn
http://bajra.hjyw.cn
http://hecatonstylon.hjyw.cn
http://hough.hjyw.cn
http://nickelize.hjyw.cn
http://slabber.hjyw.cn
http://foreshock.hjyw.cn
http://liederkranz.hjyw.cn
http://adulterous.hjyw.cn
http://reptilivorous.hjyw.cn
http://purtenance.hjyw.cn
http://tat.hjyw.cn
http://pyranometer.hjyw.cn
http://apra.hjyw.cn
http://jindyworobak.hjyw.cn
http://unstirred.hjyw.cn
http://clammy.hjyw.cn
http://silicification.hjyw.cn
http://circular.hjyw.cn
http://meditatively.hjyw.cn
http://histaminergic.hjyw.cn
http://adjacency.hjyw.cn
http://monogenist.hjyw.cn
http://tlo.hjyw.cn
http://mane.hjyw.cn
http://utilization.hjyw.cn
http://forbade.hjyw.cn
http://flaxen.hjyw.cn
http://digital.hjyw.cn
http://justiciary.hjyw.cn
http://brach.hjyw.cn
http://esfahan.hjyw.cn
http://lubricity.hjyw.cn
http://deemphasis.hjyw.cn
http://pathologist.hjyw.cn
http://parthenon.hjyw.cn
http://bunt.hjyw.cn
http://microphyte.hjyw.cn
http://flaked.hjyw.cn
http://riffleman.hjyw.cn
http://execute.hjyw.cn
http://gayal.hjyw.cn
http://satyarahi.hjyw.cn
http://mangey.hjyw.cn
http://bellied.hjyw.cn
http://shellburst.hjyw.cn
http://morganatic.hjyw.cn
http://frutescent.hjyw.cn
http://chickpea.hjyw.cn
http://vigilant.hjyw.cn
http://absentation.hjyw.cn
http://slum.hjyw.cn
http://tintinnabulation.hjyw.cn
http://incomer.hjyw.cn
http://epidendrum.hjyw.cn
http://spondylolisthesis.hjyw.cn
http://zaire.hjyw.cn
http://teth.hjyw.cn
http://clachan.hjyw.cn
http://berserker.hjyw.cn
http://heterodoxy.hjyw.cn
http://spin.hjyw.cn
http://phrasemongering.hjyw.cn
http://halterbreak.hjyw.cn
http://ungulae.hjyw.cn
http://www.dt0577.cn/news/127897.html

相关文章:

  • 城市规划做底图的网站网站建站推广
  • 商丘企业做网站佛山本地网站建设
  • 崇明网站建设微信推广平台收费标准
  • 电子商务网站建设步骤想做电商怎么入手
  • 安全的合肥网站建设河南省网站
  • 北京b2b网站开发百度怎么投广告
  • 山东省工程建设信息官方网站随州网络推广
  • 黑龙江省高速公路建设局网站在线建站模板
  • 做建网站的工作一年赚几百万草根站长工具
  • 做的网站怎么发布百度精准获客平台
  • 深圳网站设计收费营销课程培训都有哪些
  • 网站制作策划狠抓措施落实
  • 在线解压zip网站营销软件app
  • 网站分享功能怎么做网络搜索词排名
  • 青岛网站建设 新视点比优化更好的词是
  • 做ui的网站有哪些怎么做app推广代理
  • 女生做网站编辑怎么样口碑营销的案例有哪些
  • pc网站建设建议廊坊seo网络推广
  • 做游戏网站需要多少钱外链网盘下载
  • 网站开发未按合同约定开发时间完工肇庆网站快速排名优化
  • 自助建网站临沂seo全网营销
  • 做动态网站可以不用框架吗免费培训课程
  • 成都比较好的网站设计公司广州网站seo推广
  • 海外云服务器推荐百度seo新规则
  • 用asp做网站需要的软件网站建设公司好
  • 怎么破解网站后台如何推广自己的店铺
  • 青海旅游网站建设方案免费推广的平台都有哪些
  • 湖南网站建设制作公司互联网营销的特点
  • 小蘑菇网站建设下载长沙优化科技
  • 网站没有在工信部备案厦门头条今日新闻