Spring Boot 配置文件中的Java Microsoft Insight连接字符串

ego6inou  于 5个月前  发布在  Spring
关注(0)|答案(1)|浏览(61)

我在项目中有以下文件夹层次结构:

- src
  - main
    - profile
      - dev
        - osoz-config.properties
      - prod
        - osoz-config.properties

字符串
我有每个环境的连接字符串。我如何从这些文件中设置它们?
我知道我可以通过文件applicationinsights.json或环境变量尝试。我也读了关于遥测配置,但它在最新版本中不可用

wko9yo5t

wko9yo5t1#

我正在使用最新版本,并将TelemetryConfiguration配置到我的springboot应用程序中。


的数据

ApplicationInsights.json:

{
  "InstrumentationKey": "your-instrumentation-key",
  "TelemetryChannel": {
    "DeveloperMode": false
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information"
    }
  },
  "Endpoints": {
    "LiveMetrics": "https://dc.services.visualstudio.com/v2/track"
  }
}

字符串

  • 下面的代码涉及从ApplicationInsights.json加载配置。
import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.telemetry.TraceTelemetry;

public class javatestapp {
    public static void main(String[] args) {
        // Load the configuration from ApplicationInsights.json
        TelemetryConfiguration configuration = TelemetryConfiguration.getActive();

        // Initialize TelemetryClient
        TelemetryClient telemetryClient = new TelemetryClient(configuration);

        // Create and send a custom trace
        TraceTelemetry traceTelemetry = new TraceTelemetry("Sample Trace");
        telemetryClient.track(traceTelemetry);

        // Flush the telemetry and stop the application
        telemetryClient.flush();
        telemetryClient.getContext().getTelemetryConfiguration().getChannel().stop();
    }
}

  • 要存储每个环境的连接字符串,您也可以使用osoz-config.properties,如下所示。
import java.io.InputStream;
import java.util.Properties;

public class ConfigurationManager {
    private static final String PROPERTIES_FILE_PATH = "profile/%s/osoz-config.properties";

    public static Properties loadProperties(String profile) {
        Properties properties = new Properties();
        String filePath = String.format(PROPERTIES_FILE_PATH, profile);

        try (InputStream input = ConfigurationManager.class.getClassLoader().getResourceAsStream(filePath)) {
            if (input == null) {
                System.out.println("Sorry, unable to find " + filePath);
                return null;
            }
            properties.load(input);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return properties;
    }
}

交易查询:


相关问题