com.alibaba.nacos.api.naming.NamingFactory Java Examples

The following examples show how to use com.alibaba.nacos.api.naming.NamingFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ServiceConsumer.java    From nacos-tutorial with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws NacosException {
    Properties properties = new Properties();
    properties.setProperty("serverAddr", Constants.NACOS_SERVER_ADDRESS);
    properties.setProperty("namespace", Constants.NAMESPACE);

    NamingService naming = NamingFactory.createNamingService(properties);
    naming.subscribe(Constants.SERVICE_NAME, new EventListener() {
        @Override
        public void onEvent(Event event) {
            NamingEvent namingEvent = (NamingEvent) event;
            printInstances(namingEvent);
            mockConsume(naming, Constants.SERVICE_NAME);
        }
    });
    try {
        int in = System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: ServiceProvider.java    From nacos-tutorial with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws NacosException {
    Properties properties = new Properties();
    properties.setProperty("serverAddr", Constants.NACOS_SERVER_ADDRESS);
    properties.setProperty("namespace", Constants.NAMESPACE);

    NamingService naming = NamingFactory.createNamingService(properties);

    naming.registerInstance(Constants.SERVICE_NAME, Constants.IP_1, Constants.PORT_1, Constants.CLUSTER_NAME_1);
    naming.registerInstance(Constants.SERVICE_NAME, Constants.IP_2, Constants.PORT_2, Constants.CLUSTER_NAME_2);
    List<Instance> instances = naming.getAllInstances(Constants.SERVICE_NAME);
    System.out.println("getAllInstances after registered\ninstance size="
            + instances.size() + "\ninstance list=" + instances);
    try {
        int in = System.in.read();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: NacosServerHolder.java    From nacos-sync with Apache License 2.0 5 votes vote down vote up
@Override
NamingService createServer(String clusterId, Supplier<String> serverAddressSupplier, String namespace) throws Exception {
    Properties properties = new Properties();
    properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverAddressSupplier.get());
    properties.setProperty(PropertyKeyConst.NAMESPACE, namespace);
    return NamingFactory.createNamingService(properties);
}
 
Example #4
Source File: NacosRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void init() {
    if (namingService != null) {
        return;
    }

    String addressInput = registryConfig.getAddress(); // xxx:8848,yyy:8848/namespace
    if (StringUtils.isEmpty(addressInput)) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_EMPTY_ADDRESS, EXT_NAME));
    }
    int idx = addressInput.indexOf(CONTEXT_SEP);
    String namespace;
    String address; // IP地址
    if (idx > 0) {
        address = addressInput.substring(0, idx);
        namespace = addressInput.substring(idx + 1);
        //for host:port/ this scene
        if (StringUtils.isBlank(namespace)) {
            namespace = DEFAULT_NAMESPACE;
        }
    } else {
        address = addressInput;
        namespace = DEFAULT_NAMESPACE;
    }

    defaultCluster = Collections.singletonList(NacosRegistryHelper.DEFAULT_CLUSTER);

    nacosConfig.put(PropertyKeyConst.SERVER_ADDR, address);
    nacosConfig.put(PropertyKeyConst.NAMESPACE, namespace);

    try {
        namingService = NamingFactory.createNamingService(nacosConfig);
    } catch (NacosException e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_INIT_NACOS_NAMING_SERVICE, address), e);
    }
}