Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加zookeeper注册中心,统一各注册中心写法 #475

Merged
merged 19 commits into from Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,6 +31,9 @@ public final class GrpcUtils {
*/
public static final String CLOUD_DISCOVERY_METADATA_PORT = "gRPC.port";


public static final String INTER_PROCESS_DISABLE = "-1";
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

/**
* Extracts the service name from the given method.
*
Expand Down
1 change: 1 addition & 0 deletions grpc-server-spring-boot-autoconfigure/build.gradle
Expand Up @@ -19,6 +19,7 @@ dependencies {
optionalSupportImplementation 'org.springframework.security:spring-security-core'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-sleuth'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-consul-discovery'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-zookeeper-discovery'
optionalSupportImplementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
optionalSupportImplementation "com.alibaba.cloud:spring-cloud-starter-alibaba-nacos-discovery:${springCloudAlibabaNacosVersion}"
optionalSupportImplementation 'com.google.inject:guice:4.2.3' // Only needed to avoid some warnings during compilation (for eureka)
Expand Down
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2016-2020 Michael Zhang <yidongnan@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package net.devh.boot.grpc.server.autoconfigure;

import java.util.Objects;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.zookeeper.serviceregistry.ZookeeperRegistration;
import org.springframework.context.annotation.Configuration;

import net.devh.boot.grpc.common.util.GrpcUtils;
import net.devh.boot.grpc.server.config.GrpcServerProperties;

/**
* Configuration class that configures the required beans for grpc discovery via Zookeeper.
*
* @author zhaochunlin (946599275@gmail.com)
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({ZookeeperRegistration.class})
public class GrpcMetaDataZookeeperConfiguration {
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

@Autowired(required = false)
ZookeeperRegistration zookeeperRegistration;
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved

@Autowired
private GrpcServerProperties grpcServerProperties;


@PostConstruct
public void init() {
if (zookeeperRegistration != null) {
final String port = String.valueOf(grpcServerProperties.getPort());
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
zookeeperRegistration.setPort(0);
ZMRWEGO marked this conversation as resolved.
Show resolved Hide resolved
if (!GrpcUtils.INTER_PROCESS_DISABLE.equals(port)) {
zookeeperRegistration.getServiceInstance().getPayload().getMetadata()
.put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, port);
}
}
}
}
Expand Up @@ -17,17 +17,19 @@

package net.devh.boot.grpc.server.autoconfigure;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistrationCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.context.annotation.Configuration;

import com.ecwid.consul.v1.ConsulClient;

import net.devh.boot.grpc.server.cloud.ConsulGrpcRegistrationCustomizer;
import net.devh.boot.grpc.common.util.GrpcUtils;
import net.devh.boot.grpc.server.config.GrpcServerProperties;

/**
Expand All @@ -38,20 +40,25 @@
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({ConsulDiscoveryProperties.class, ConsulClient.class, GrpcServerProperties.class})
@ConditionalOnClass({ConsulRegistration.class})
public class GrpcMetadataConsulConfiguration {

/**
* Creates a bean that registers the gRPC endpoint via consul.
*
* @param grpcServerProperties The server properties used to fill in the blanks.
* @return The newly created consulGrpcRegistrationCustomizer bean.
*/
@ConditionalOnMissingBean
@Bean
public ConsulRegistrationCustomizer consulGrpcRegistrationCustomizer(
final GrpcServerProperties grpcServerProperties) {
return new ConsulGrpcRegistrationCustomizer(grpcServerProperties);
}
@Autowired(required = false)
private ConsulRegistration consulRegistration;

@Autowired
private GrpcServerProperties grpcProperties;

@PostConstruct
public void init() {
if (consulRegistration != null) {
final String port = String.valueOf(grpcProperties.getPort());
List<String> tags = consulRegistration.getService().getTags();
tags = tags==null ? new ArrayList<>() : tags;
if (!GrpcUtils.INTER_PROCESS_DISABLE.equals(port)) {
tags.add(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT + "=" + port);
consulRegistration.getService().setTags(tags);
}
}
}
ST-DDT marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Up @@ -17,17 +17,16 @@

package net.devh.boot.grpc.server.autoconfigure;

import java.util.Objects;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration;
import org.springframework.context.annotation.Configuration;

import com.netflix.appinfo.EurekaInstanceConfig;
import com.netflix.discovery.EurekaClient;

import net.devh.boot.grpc.common.util.GrpcUtils;
import net.devh.boot.grpc.server.config.GrpcServerProperties;

Expand All @@ -39,23 +38,23 @@
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({EurekaInstanceConfigBean.class, EurekaClient.class})
@ConditionalOnClass({EurekaRegistration.class})
public class GrpcMetadataEurekaConfiguration {

@Autowired(required = false)
private EurekaInstanceConfig instance;
private EurekaRegistration eurekaRegistration;

@Autowired
private GrpcServerProperties grpcProperties;

@PostConstruct
public void init() {
if (this.instance == null) {
return;
}
final int port = this.grpcProperties.getPort();
if (port != -1) {
this.instance.getMetadataMap().put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, Integer.toString(port));
if (eurekaRegistration != null) {
final String port = String.valueOf(grpcProperties.getPort());
if (!GrpcUtils.INTER_PROCESS_DISABLE.equals(port)) {
eurekaRegistration.getInstanceConfig().getMetadataMap().put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT,
ZMRWEGO marked this conversation as resolved.
Show resolved Hide resolved
port);
}
}
}

Expand Down
Expand Up @@ -17,16 +17,16 @@

package net.devh.boot.grpc.server.autoconfigure;

import java.util.Objects;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import com.alibaba.cloud.nacos.registry.NacosRegistration;
import com.alibaba.nacos.client.naming.NacosNamingService;

import net.devh.boot.grpc.common.util.GrpcUtils;
import net.devh.boot.grpc.server.config.GrpcServerProperties;
Expand All @@ -38,7 +38,7 @@
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
@ConditionalOnClass({NacosDiscoveryProperties.class, NacosNamingService.class})
@ConditionalOnClass({NacosRegistration.class})
public class GrpcMetadataNacosConfiguration {

@Autowired(required = false)
Expand All @@ -49,13 +49,13 @@ public class GrpcMetadataNacosConfiguration {

@PostConstruct
public void init() {
if (this.nacosRegistration == null) {
return;
}
final int port = this.grpcProperties.getPort();
if (port != -1) {
this.nacosRegistration.getMetadata().put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, Integer.toString(port));
if (nacosRegistration != null) {
final String port = String.valueOf(grpcProperties.getPort());
if (!GrpcUtils.INTER_PROCESS_DISABLE.equals(port)) {
nacosRegistration.getMetadata().put(GrpcUtils.CLOUD_DISCOVERY_METADATA_PORT, port);
}
}

}

}

This file was deleted.

Expand Up @@ -3,6 +3,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataConsulConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataEurekaConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcMetadataNacosConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcMetaDataZookeeperConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcServerFactoryAutoConfiguration,\
net.devh.boot.grpc.server.autoconfigure.GrpcServerSecurityAutoConfiguration,\
Expand Down