java 本地调试引用tars的服务

文章讲述了如何配置TarsJava的tarsjava.conf文件,包括设置应用参数如locator、超时时间等。同时,文中提到了在yml中添加tars配置项,并提供了检查和读取yml属性值的方法。此外,还展示了启动类如何根据tars配置来决定应用的启动行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 在resources中添加tarsjava.conf文件

<tars>
  <application>
    enableset=n
    setdivision=NULL
    <client>
        locator=
        sync-invoke-timeout=20000
        async-invoke-timeout=20000
        stat=tars.tarsstat.StatObj
        modulename=TestHttp.HelloJavaServer
    </client>
    <server>
      node=
      lognum=10
      logLevel=DEBUG
      deactivating-timeout=3000
      activating-timeout=10000
      app=TestHttp
      server=HelloHttp
      localip=127.0.0.1
      local=tcp -h 127.0.0.1 -p 42460 -t 10000
      basepath=.
      datapath=.
      logpath=.
      loglevel=DEBUG
      logsize=15M
      log=tars.tarslog.LogObj
      config=tars.tarsconfig.ConfigObj
      notify=tars.tarsnotify.NotifyObj
      sessiontimeout=120000
      sessioncheckinterval=60000
      tcpnodelay=true
      udpbuffersize=8192
      charsetname=UTF-8
      backupfiles=conf
      packageFormat=jar
      <TestHttp.HelloHttp.HttpObjAdapter>
        allow
        endpoint=tcp -h 127.0.0.1 -p 17608 -t 60000
        maxconns=100000
        protocol=not_tars
        queuecap=50000
        queuetimeout=20000
        servant=TestHttp.HelloHttp.HttpObj
        threads=5
      </TestHttp.HelloHttp.HttpObjAdapter>
    </server>
  </application>
</tars>
  1. 在yml中添加

  tars:
    enabled: false
  1. 添加类


public class HierwaySpringApplication {
    public  static Boolean value = false;
    //bootstrap.yml为上方的yml文件 名字不同的请手动更改
    public static ConfigurableApplicationContext run(Class<?> primarySource,String... args) {
        System.setProperty("nacos.logging.default.config.enabled", "false");
        String smlPath = "";
        if (FileUtil.exist("./bootstrap.yml")){
            smlPath = FileUtil.getAbsolutePath("./bootstrap.yml");
        }
        if (FileUtil.exist(System.getProperty("user.dir")+"/config/bootstrap.yml")){
            smlPath = System.getProperty("user.dir")+"/config/bootstrap.yml";
        }
        String tarsEnabled = System.getProperty("spring.tars.enabled");
        if (StringUtils.isEmpty(tarsEnabled)){
            value = (Boolean) getYmlPropertyValue(smlPath,"spring.tars.enabled");
        }else{
            value = Boolean.valueOf(tarsEnabled);
        }
        System.out.println("----------------tarsEnable----------------------"+ value);
        if (!value){
            String path = "";
            if (FileUtil.exist("./tarsjava.conf")){
                path = FileUtil.getAbsolutePath("./tarsjava.conf");
            }
            if (FileUtil.exist(System.getProperty("user.dir")+"/config/tarsjava.conf")){
                path = System.getProperty("user.dir")+"/config/tarsjava.conf";
            }
            if (path==""){
                URL url=primarySource.getProtectionDomain().getCodeSource().getLocation();
                path=url.getPath();
                int index = path.lastIndexOf("/");
                path = path.substring(0,index);
                if (FileUtil.exist(path+"/config/tarsjava.conf")){
                    path = FileUtil.getAbsolutePath(path+"/config/tarsjava.conf");
                }
            }
            System.out.println("jar包目录:"+path);
            System.out.println("----------------path----------------------"+path);
            System.setProperty("config", path);
        }
       return SpringApplication.run(primarySource,args);

    }
    public static Object getYmlPropertyValue(String path,String key) {

        Properties properties = null;
        try {
            YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            if (StringUtils.isEmpty(path)){
                Resource resource = new ClassPathResource("bootstrap.yml");
                yamlFactory.setResources(resource);
            }else{
                yamlFactory.setResources(new FileSystemResource(path));
            }
            properties = yamlFactory.getObject();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return properties.get(key);
    }
}
  1. 启动类调用


 public static void main(String[] args) {
        HierwaySpringApplication.run(AssetManageApplication.class);
        if (!HierwaySpringApplication.value){
            ScheduledServiceMngr.getInstance().shutdown();
        }
    }
  1. 打包成可在tars上运行和正常环境运行的包可参考java 打包发布

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值