|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* |
| 19 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 20 | + * contributor license agreements. See the NOTICE file distributed with |
| 21 | + * this work for additional information regarding copyright ownership. |
| 22 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 23 | + * (the "License"); you may not use this file except in compliance with |
| 24 | + * the License. You may obtain a copy of the License at |
| 25 | + * |
| 26 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 27 | + * |
| 28 | + * Unless required by applicable law or agreed to in writing, software |
| 29 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 30 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 31 | + * See the License for the specific language governing permissions and |
| 32 | + * limitations under the License. |
| 33 | + */ |
| 34 | +package org.apache.dubbo.metadata.store.etcd; |
| 35 | + |
| 36 | +import org.apache.dubbo.common.Constants; |
| 37 | +import org.apache.dubbo.common.URL; |
| 38 | +import org.apache.dubbo.common.logger.Logger; |
| 39 | +import org.apache.dubbo.common.logger.LoggerFactory; |
| 40 | +import org.apache.dubbo.metadata.identifier.MetadataIdentifier; |
| 41 | +import org.apache.dubbo.metadata.support.AbstractMetadataReport; |
| 42 | +import org.apache.dubbo.remoting.etcd.jetcd.JEtcdClient; |
| 43 | + |
| 44 | +/** |
| 45 | + * Report Metadata to Etcd |
| 46 | + */ |
| 47 | +public class EtcdMetadataReport extends AbstractMetadataReport { |
| 48 | + |
| 49 | + private final static Logger logger = LoggerFactory.getLogger(EtcdMetadataReport.class); |
| 50 | + |
| 51 | + private final String root; |
| 52 | + |
| 53 | + /** |
| 54 | + * The etcd client |
| 55 | + */ |
| 56 | + private final JEtcdClient etcdClient; |
| 57 | + |
| 58 | + public EtcdMetadataReport(URL url) { |
| 59 | + super(url); |
| 60 | + if (url.isAnyHost()) { |
| 61 | + throw new IllegalStateException("registry address == null"); |
| 62 | + } |
| 63 | + String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT); |
| 64 | + if (!group.startsWith(Constants.PATH_SEPARATOR)) { |
| 65 | + group = Constants.PATH_SEPARATOR + group; |
| 66 | + } |
| 67 | + this.root = group; |
| 68 | + etcdClient = new JEtcdClient(url); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) { |
| 73 | + storeMetadata(providerMetadataIdentifier, serviceDefinitions); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) { |
| 78 | + storeMetadata(consumerMetadataIdentifier, value); |
| 79 | + } |
| 80 | + |
| 81 | + private void storeMetadata(MetadataIdentifier identifier, String v) { |
| 82 | + String key = getNodeKey(identifier); |
| 83 | + if (!etcdClient.put(key, v)) { |
| 84 | + logger.error("Failed to put " + identifier + " to etcd, value: " + v); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + String getNodeKey(MetadataIdentifier identifier) { |
| 89 | + return toRootDir() + identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH); |
| 90 | + } |
| 91 | + |
| 92 | + String toRootDir() { |
| 93 | + if (root.equals(Constants.PATH_SEPARATOR)) { |
| 94 | + return root; |
| 95 | + } |
| 96 | + return root + Constants.PATH_SEPARATOR; |
| 97 | + } |
| 98 | +} |
0 commit comments