parent
0007b4a95b
commit
690431363f
File diff suppressed because one or more lines are too long
@ -1,98 +1 @@
|
||||
package com.tencent.cloud.polaris.config.configdata;
|
||||
|
||||
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
|
||||
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
|
||||
import org.springframework.boot.context.config.ConfigData;
|
||||
import org.springframework.boot.context.config.ConfigDataResource;
|
||||
import org.springframework.boot.context.config.Profiles;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* A polaris configData resource from which {@link ConfigData} can be loaded.
|
||||
*
|
||||
* @author wlx
|
||||
* @date 2022/7/5 11:13 下午
|
||||
*/
|
||||
public class PolarisConfigDataResource extends ConfigDataResource {
|
||||
|
||||
private final PolarisConfigProperties polarisConfigProperties;
|
||||
|
||||
private final PolarisContextProperties polarisContextProperties;
|
||||
|
||||
private final Profiles profiles;
|
||||
|
||||
private final boolean optional;
|
||||
|
||||
private final String fileName;
|
||||
|
||||
private final String serviceName;
|
||||
|
||||
public PolarisConfigDataResource(PolarisConfigProperties polarisConfigProperties,
|
||||
PolarisContextProperties polarisContextProperties,
|
||||
Profiles profiles,
|
||||
boolean optional,
|
||||
String fileName,
|
||||
String serviceName) {
|
||||
this.polarisConfigProperties = polarisConfigProperties;
|
||||
this.polarisContextProperties = polarisContextProperties;
|
||||
this.profiles = profiles;
|
||||
this.optional = optional;
|
||||
this.fileName = fileName;
|
||||
this.serviceName = serviceName;
|
||||
}
|
||||
|
||||
public PolarisConfigProperties getPolarisConfigProperties() {
|
||||
return polarisConfigProperties;
|
||||
}
|
||||
|
||||
public PolarisContextProperties getPolarisContextProperties() {
|
||||
return polarisContextProperties;
|
||||
}
|
||||
|
||||
public Profiles getProfiles() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
public boolean isOptional() {
|
||||
return optional;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return fileName;
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
return serviceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
PolarisConfigDataResource that = (PolarisConfigDataResource) o;
|
||||
return optional == that.optional &&
|
||||
polarisConfigProperties.equals(that.polarisConfigProperties) &&
|
||||
polarisContextProperties.equals(that.polarisContextProperties) &&
|
||||
profiles.equals(that.profiles) &&
|
||||
fileName.equals(that.fileName) &&
|
||||
serviceName.equals(that.serviceName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(polarisConfigProperties, polarisContextProperties, profiles, optional, fileName, serviceName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PolarisConfigDataResource{" +
|
||||
"polarisConfigProperties=" + polarisConfigProperties +
|
||||
", polarisContextProperties=" + polarisContextProperties +
|
||||
", profiles=" + profiles +
|
||||
", optional=" + optional +
|
||||
", fileName='" + fileName + '\'' +
|
||||
", serviceName='" + serviceName + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
package com.tencent.cloud.polaris.config.configdata;
import java.util.Objects;
import com.tencent.cloud.polaris.config.config.PolarisConfigProperties;
import com.tencent.cloud.polaris.context.config.PolarisContextProperties;
import org.springframework.boot.context.config.ConfigData;
import org.springframework.boot.context.config.ConfigDataResource;
import org.springframework.boot.context.config.Profiles;
/**
* A polaris configData resource from which {@link ConfigData} can be loaded.
*
* @author wlx
* @date 2022/7/5 11:13 下午
*/
public class PolarisConfigDataResource extends ConfigDataResource {
private final PolarisConfigProperties polarisConfigProperties;
private final PolarisContextProperties polarisContextProperties;
private final Profiles profiles;
private final boolean optional;
private final String fileName;
private final String groupName;
private final String serviceName;
public PolarisConfigDataResource(PolarisConfigProperties polarisConfigProperties,
PolarisContextProperties polarisContextProperties,
Profiles profiles, boolean optional,
String fileName, String groupName, String serviceName) {
this.polarisConfigProperties = polarisConfigProperties;
this.polarisContextProperties = polarisContextProperties;
this.profiles = profiles;
this.optional = optional;
this.fileName = fileName;
this.groupName = groupName;
this.serviceName = serviceName;
}
public PolarisConfigProperties getPolarisConfigProperties() {
return polarisConfigProperties;
}
public PolarisContextProperties getPolarisContextProperties() {
return polarisContextProperties;
}
public Profiles getProfiles() {
return profiles;
}
public boolean isOptional() {
return optional;
}
public String getFileName() {
return fileName;
}
public String getGroupName() {
return groupName;
}
public String getServiceName() {
return serviceName;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PolarisConfigDataResource that = (PolarisConfigDataResource) o;
return optional == that.optional &&
polarisConfigProperties.equals(that.polarisConfigProperties) &&
polarisContextProperties.equals(that.polarisContextProperties) &&
profiles.equals(that.profiles) &&
fileName.equals(that.fileName) &&
groupName.equals(that.groupName) &&
serviceName.equals(that.serviceName);
}
@Override
public int hashCode() {
return Objects.hash(polarisConfigProperties, polarisContextProperties, profiles, optional, fileName, groupName, serviceName);
}
}
|
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
package com.tencent.cloud.polaris.config.example;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* the endpoint for get config.
|
||||
*
|
||||
* @author lepdou 2022-03-10
|
||||
*/
|
||||
@RestController
|
||||
@RefreshScope
|
||||
public class ConfigController {
|
||||
|
||||
@Value("${timeout:1000}")
|
||||
private int timeout;
|
||||
|
||||
@Autowired
|
||||
private Person person;
|
||||
|
||||
@Autowired
|
||||
private Environment environment;
|
||||
|
||||
@GetMapping("/timeout")
|
||||
public int timeout() {
|
||||
environment.getProperty("timeout", "1000");
|
||||
return timeout;
|
||||
}
|
||||
|
||||
@GetMapping("/person")
|
||||
public String person() {
|
||||
return person.toString();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.config.example;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* example property object.
|
||||
*
|
||||
* @author lepdou 2022-03-28
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "teacher")
|
||||
public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name='" + name + '\'' + ", age=" + age + '}';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
*
|
||||
* Licensed under the BSD 3-Clause License (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://opensource.org/licenses/BSD-3-Clause
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed
|
||||
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.tencent.cloud.polaris.config.example;
|
||||
|
||||
import com.tencent.cloud.polaris.config.annotation.PolarisConfigKVFileChangeListener;
|
||||
import com.tencent.cloud.polaris.config.listener.ConfigChangeEvent;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Custom Config Listener Example .
|
||||
*
|
||||
* @author Palmer Xu 2022-06-06
|
||||
*/
|
||||
@Component
|
||||
public final class PersonConfigChangeListener {
|
||||
|
||||
/**
|
||||
* PolarisConfigKVFileChangeListener Example .
|
||||
* @param event instance of {@link ConfigChangeEvent}
|
||||
*/
|
||||
@PolarisConfigKVFileChangeListener(interestedKeyPrefixes = "teacher")
|
||||
public void onChange(ConfigChangeEvent event) {
|
||||
Set<String> changedKeys = event.changedKeys();
|
||||
|
||||
for (String changedKey : changedKeys) {
|
||||
System.out.printf("%s = %s \n", changedKey, event.getChange(changedKey));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue