Program Club

서비스 패브릭에 대한 환경 별 런타임 구성 매개 변수를 어디에 설정하고 액세스합니까?

proclub 2020. 10. 13. 19:35
반응형

서비스 패브릭에 대한 환경 별 런타임 구성 매개 변수를 어디에 설정하고 액세스합니까?


로컬 및 클라우드의 두 환경에서 Sql 데이터베이스, 스토리지 계정 등과 같은 리소스에 대한 사용자 지정 설정 또는 매개 변수를 어떻게 설정합니까? 데이터베이스, 로컬 또는 클라우드 환경에 대한 구성이 다릅니다. 감사합니다.


Service Fabric을 로컬 및 클라우드에서 실행하기위한 환경 별 변수를 가지려면 다음을 수행해야합니다.

  1. 사용자 지정 구성 섹션 및 매개 변수를 Service / Actor 프로젝트의 Settings.xml 파일에 추가합니다 (프로젝트 루트의 \ PackageRoot \ Config \ Settings.xml에 있음). 환경별로 다른 곳에서 설정하므로 매개 변수를 비워 둡니다. 다음은 하나의 예입니다.
<?xml version="1.0" encoding="utf-8" ?>
<Settings xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2011/01/fabric">
<!-- Add your custom configuration sections and parameters here -->
    <Section Name="UserDatabase">
        <Parameter Name="UserDatabaseConnectionString" Value="" />
    </Section>
</Settings>
  1. Service Fabric 프로젝트의 ApplicationManifest.xml 파일 <ServiceManifestImport>에는 포함 된 각 프로젝트에 대한 요소 가 있습니다 . 그 아래 <ConfigOverrides>에는 Service Fabric 프로젝트의 ApplicationParameters 아래에있는 로컬 및 클라우드 xml 파일에서 환경별로 설정된 값으로 대체 될 구성 값을 선언 할 요소가 있습니다. 동일한 ApplicationManifest.xml 파일에서 로컬 및 클라우드 xml 파일에있는 매개 변수를 추가해야합니다. 그렇지 않으면 빌드시 덮어 쓰게됩니다.

위의 예를 계속하면 이것이 설정되는 방법입니다.

<Parameters>
    <Parameter Name="ServiceName_InstanceCount" DefaultValue="-1" />
    <Parameter Name="UserDatabaseConnectionString" DefaultValue="" />
</Parameters>
<ConfigOverrides>
    <ConfigOverride Name="Config">
        <Settings>
            <Section Name="UserDatabase">
                <Parameter Name="UserDatabaseConnectionString" Value="[UserDatabaseConnectionString]" />
            </Section>
        </Settings>
    </ConfigOverride>
</ConfigOverrides>
  1. Service Fabric 프로젝트의 ApplicationParameters 아래에있는 local.xml 및 cloud.xml 파일에서 이와 같이 환경 별 변수를 지정합니다.
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <Parameter Name="ServiceName_InstanceCount" Value="1" />
        <Parameter Name="UserDatabaseConnectionString" Value="Server=(localdb)\MsSqlLocalDb;Database=Users;User=ReadOnlyUser;Password=XXXXX;" />
    </Parameters>
</Application>
  1. 마지막으로, 서비스 / 액터에서 이와 같은 환경 별 구성 변수에 액세스 할 수 있습니다.
var configurationPackage = Context.CodePackageActivationContext.GetConfigurationPackageObject("Config");

var connectionStringParameter = configurationPackage.Settings.Sections["UserDatabase"].Parameters["UserDatabaseConnectionString"];

다른 애플리케이션과 마찬가지로 환경 변수를 사용할 수 있으며 , 기본 제공 서비스 패브릭 런타임이 필요한 것과 달리 서비스 패브릭 내에서 게스트 실행 파일 에서도 작동 settings.xml합니다.

응용 프로그램 내 GetEnvironmentVariable에서 Environment클래스 메서드를 통해 다른 .net 응용 프로그램과 마찬가지로 환경 변수에 액세스 할 수 있습니다 .

var baseUri = Environment.GetEnvironmentVariable("SuperWebServiceBaseUri");

그런 다음 몇 가지 기본 환경 변수 값을 설정해야합니다.이 작업 ServiceManifest.xml은 서비스 매니페스트 파일 내에서 수행됩니다 .

<?xml version="1.0" encoding="utf-8" ?>
<ServiceManifest Name="MyServicePkg" Version="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <!-- snip -->
    <CodePackage Name="Code" Version="1.0.0">
        <!-- snip -->
        <EnvironmentVariables>
            <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="http://localhost:12345"/>
        </EnvironmentVariables>
    </CodePackage>
    <!-- snip -->
</ServiceManifest>

이러한 환경 변수는 ApplicationManifest.xml다음 코드를 사용하여 파일 내에서 재정의 할 수 있습니다 .

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <!-- snip -->
    </Parameters>
    <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
        <EnvironmentOverrides CodePackageRef="Code">
            <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="https://the-real-live-super-base-uri.com/"/>
        </EnvironmentOverrides>
    </ServiceManifestImport>
    <!-- snip -->
</ApplicationManifest>

This then can be parameterised like any other application manifest setting using the local.xml and cloud.xml.

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="fabric:/AppFabricName.ServiceFabric" xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <Parameter Name="MyService_SuperWebServiceBaseUri" Value="https://another-base-uri.com/" />
    </Parameters>
</Application>

Then we'll have to update the ApplicationManifest.xml to support these parameters;

<?xml version="1.0" encoding="utf-8"?>
<ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="ChileTargetType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric">
    <Parameters>
        <Parameter Name="MyService_SuperWebServiceBaseUri" DefaultValue="https://the-real-live-super-base-uri.com/" />
    </Parameters>
    <ServiceManifestImport>
        <ServiceManifestRef ServiceManifestName="MyServicePkg" ServiceManifestVersion="1.0.0" />
        <EnvironmentOverrides CodePackageRef="Code">
            <EnvironmentVariable Name="SuperWebServiceBaseUri" Value="[MyService_SuperWebServiceBaseUri]"/>
        </EnvironmentOverrides>
    </ServiceManifestImport>
    <!-- snip -->
</ApplicationManifest>

The above answers explain well how it is done. I want to add a sidemark, why it is that 'convoluted':

It has to be this way, as the services are intended to be self-contained. They should run by default in any application they are linked into. Independent of the application's Manifest. So the service can only rely on parameters, which are at least predefined in its own configuration.

These presettings can then be overwritten by the application. This is the only universal approach.

참고URL : https://stackoverflow.com/questions/33928204/where-do-you-set-and-access-run-time-configuration-parameters-per-environment-fo

반응형