yangys
2024-04-02 b773661de485d9748386bfdd15a7ac7cd399be36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.qianwen.core.launch;
 
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Properties;
import java.util.ServiceLoader;
import java.util.function.Function;
import java.util.stream.Collectors;
import com.qianwen.core.launch.constant.AppConstant;
import com.qianwen.core.launch.constant.NacosConstant;
import com.qianwen.core.launch.service.LauncherService;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.SimpleCommandLinePropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
 
public class BladeApplication {
    public static ConfigurableApplicationContext run(String appName, Class source, String... args) {
        SpringApplicationBuilder builder = createSpringApplicationBuilder(appName, source, args);
        return builder.run(args);
      }
      
      public static SpringApplicationBuilder createSpringApplicationBuilder(String appName, Class source, String... args) {
        String profile;
        Assert.hasText(appName, "[appName]服务名不能为空");
        StandardEnvironment standardEnvironment = new StandardEnvironment();
        MutablePropertySources propertySources = standardEnvironment.getPropertySources();
        
        propertySources.addFirst(new SimpleCommandLinePropertySource(args));
        propertySources.addLast(new MapPropertySource("systemProperties", standardEnvironment.getSystemProperties()));
        propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", standardEnvironment.getSystemEnvironment()));
        
        String[] activeProfiles = standardEnvironment.getActiveProfiles();
        List<String> profiles = Arrays.asList(activeProfiles);
        List<String> presetProfiles = new ArrayList<>(Arrays.asList(new String[] { "dev", "test", "prod" }));
        presetProfiles.retainAll(profiles);
        List<String> activeProfileList = new ArrayList<>(profiles);
        Function<Object[], String> joinFun = StringUtils::arrayToCommaDelimitedString;
        SpringApplicationBuilder builder = new SpringApplicationBuilder(new Class[] { source });
        if (activeProfileList.isEmpty()) {
          profile = "dev";
          activeProfileList.add(profile);
          builder.profiles(new String[] { profile });
        } else if (activeProfileList.size() == 1) {
          profile = activeProfileList.get(0);
        } else {
          throw new RuntimeException("同时存在环境变量:[" + StringUtils.arrayToCommaDelimitedString(activeProfiles) + "]");
        } 
        String startJarPath = BladeApplication.class.getResource("/").getPath().split("!")[0];
        String activePros = joinFun.apply(activeProfileList.toArray());
        System.out.printf("----启动中,读取到的环境变量:[%s],jar地址:[%s]----%n", new Object[] { activePros, startJarPath });
        Properties props = System.getProperties();
        props.setProperty("spring.application.name", appName);
        props.setProperty("spring.profiles.active", profile);
        props.setProperty("info.version", "9.3.0.0-SNAPSHOT");
        props.setProperty("info.desc", appName);
        props.setProperty("file.encoding", StandardCharsets.UTF_8.name());
        props.setProperty("blade.env", profile);
        props.setProperty("blade.name", appName);
        props.setProperty("blade.is-local", String.valueOf(isLocalDev()));
        props.setProperty("blade.dev-mode", profile.equals("prod") ? "false" : "true");
        props.setProperty("blade.service.version", "9.3.0.0-SNAPSHOT");
        Properties defaultProperties = new Properties();
        defaultProperties.setProperty("spring.main.allow-bean-definition-overriding", "true");
        defaultProperties.setProperty("spring.sleuth.sampler.percentage", "1.0");
        defaultProperties.setProperty("spring.cloud.alibaba.seata.tx-service-group", appName.concat("-group"));
        builder.properties(defaultProperties);
        
        List<LauncherService> launcherList = new ArrayList<>();
        ServiceLoader.<LauncherService>load(LauncherService.class).forEach(launcherList::add);
        
        launcherList.stream().sorted(Comparator.comparing(LauncherService::getOrder)).collect(Collectors.toList())
          .forEach(launcherService -> launcherService.launcher(builder, appName, profile, isLocalDev()));
        return builder;
 
      }
      
      public static boolean isLocalDev() {
        String osName = System.getProperty("os.name");
        return (StringUtils.hasText(osName) && !"LINUX".equalsIgnoreCase(osName));
      }
}