All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.cloud.ans.registry.AnsRegistration Maven / Gradle / Ivy

There is a newer version: 1.1.3
Show newest version
package com.alibaba.cloud.ans.registry;

import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.TenantInfo;
import com.alibaba.ans.shaded.com.taobao.vipserver.client.utils.UtilAndComs;
import com.alibaba.boot.context.domain.AddressInfo;
import com.alibaba.boot.context.project.ProjectInfo;
import com.alibaba.boot.context.tenant.TenantInformation;
import com.alibaba.cloud.ans.AnsProperties;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;

import javax.annotation.PostConstruct;
import java.net.URI;
import java.util.Map;

/**
 * @author yizhan
 *
 * after spring-cloud-commons 1.3.0 , registration extends ServiceInstance
 * support 1.1, 1.2 and 1.3 , implements ServiceInstance and Registration
 */
public class AnsRegistration implements Registration, ServiceInstance{


    /**
     * 自动注册与租户相关的信息,以及应用信息
     */
    @Autowired
    private TenantInformation tenantInformation;

    @Autowired
    private ProjectInfo projectInfo;

    @Autowired
    private AnsProperties ansProperties;

    @Autowired
    private AddressInfo addressInfo;


    @PostConstruct
    public void init() {
        System.setProperty(UtilAndComs.PROPERTY_NAME_TENANT_ID, tenantInformation.getTenantId());
        System.setProperty(UtilAndComs.PROPERTY_NAME_AK, tenantInformation.getAccessKey());
        System.setProperty(UtilAndComs.PROPERTY_NAME_SK, tenantInformation.getSecretKey());
        System.setProperty(UtilAndComs.PROPERTY_NAME_APP, projectInfo.getName());
        System.setProperty(UtilAndComs.PROPERTY_NAME_ENV, ansProperties.getEnv());

        TenantInfo.tenantId = tenantInformation.getTenantId();
        TenantInfo.tenantAk = tenantInformation.getAccessKey();
        TenantInfo.tenantSK = tenantInformation.getSecretKey();
        TenantInfo.tenantApp = projectInfo.getName();
        TenantInfo.tenantEnv = ansProperties.getEnv();

        //在EDAS中启动时,会通过JVM参数进行address.server.domain的配置,此时 endpoint 配置将无效
        if(StringUtils.isBlank(addressInfo.getDomain()) && StringUtils.isNotBlank(addressInfo.getEndpoint())){
            System.setProperty("address.server.domain", addressInfo.getEndpoint());
        }

        if (StringUtils.isNotBlank(addressInfo.getPort())) {
            System.setProperty("address.server.port", addressInfo.getPort());
        } else {
            System.setProperty("address.server.port", "8080");
        }
    }


    @Override
    public String getServiceId() {
        return ansProperties.getDoms();
    }

    @Override
    public String getHost() {
        return ansProperties.getIp();
    }

    @Override
    public int getPort() {
        return ansProperties.getPort();
    }

    public void setPort(int port){
        //if spring.cloud.ans.port is not set,use the port detected from context
        if(ansProperties.getPort() < 0) {
            this.ansProperties.setPort(port);
        }
    }

    @Override
    public boolean isSecure() {
        return ansProperties.isSecure();
    }

    @Override
    public URI getUri() {
        return DefaultServiceInstance.getUri(this);
    }

    @Override
    public Map getMetadata() {
        return ansProperties.getMetadata();
    }


    public String getRegisterToken(String serviceId) {
        if(StringUtils.isNotBlank(ansProperties.getTokens().get(serviceId))){
            return ansProperties.getTokens().get(serviceId);
        }
        return ansProperties.getToken();
    }

    public String getDefaultToken(){
        return ansProperties.getToken();
    }

    public boolean isRegisterEnabled() {
        return ansProperties.isRegisterEnabled();
    }

    public String getCluster() {
        return ansProperties.getCluster();
    }

    public float getRegisterWeight(String dom) {
        if(null != ansProperties.getWeights().get(dom) && ansProperties.getWeights().get(dom) > 0){
            return ansProperties.getWeights().get(dom);
        }
        return ansProperties.getWeight();
    }


    public String getApp() {
        return projectInfo.getName();
    }
    public String getEnv() {
        return ansProperties.getEnv();
    }

    public String getTenantId(){
        return tenantInformation.getTenantId();
    }

    public String summary() {
        return JSON.toJSONString(this);
    }

    public AnsProperties getAnsProperties() {
        return ansProperties;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy