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

add feign client annotation support #12736

Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions dubbo-dependencies-bom/pom.xml
Expand Up @@ -194,6 +194,7 @@
<maven_flatten_version>1.5.0</maven_flatten_version>
<commons_compress_version>1.23.0</commons_compress_version>
<revision>3.3.0-beta.1-SNAPSHOT</revision>
<open_feign_version>3.1.5</open_feign_version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -949,6 +950,11 @@
<artifactId>rxjava</artifactId>
<version>${rxjava.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<version>${open_feign_version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
6 changes: 6 additions & 0 deletions dubbo-metadata/dubbo-metadata-rest/pom.xml
Expand Up @@ -58,6 +58,12 @@
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -109,6 +109,11 @@ interface SPRING_MVC {
*/
String CONTROLLER_ANNOTATION_CLASS_NAME = "org.springframework.stereotype.Controller";

/**
* The annotation class name of @FeignClient
*/
String FEIGN_CLIENT_CLASS_NAME = "org.springframework.cloud.openfeign.FeignClient";

/**
* The annotation class name of @RequestMapping
*/
Expand Down Expand Up @@ -161,5 +166,8 @@ interface SPRING_MVC {
* @since 2.7.9
*/
Class<?> ANNOTATED_ELEMENT_UTILS_CLASS = resolveClass(ANNOTATED_ELEMENT_UTILS_CLASS_NAME, getClassLoader());

Class<? extends Annotation> FEIGN_CLIENT_CLASS = (Class<? extends Annotation>) resolveClass(FEIGN_CLIENT_CLASS_NAME, getClassLoader());

}
}
Expand Up @@ -39,6 +39,7 @@
import static org.apache.dubbo.common.utils.PathUtils.buildPath;
import static org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.ANNOTATED_ELEMENT_UTILS_CLASS;
import static org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.CONTROLLER_ANNOTATION_CLASS;
import static org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.FEIGN_CLIENT_CLASS;
import static org.apache.dubbo.metadata.rest.RestMetadataConstants.SPRING_MVC.REQUEST_MAPPING_ANNOTATION_CLASS;

/**
Expand All @@ -58,7 +59,9 @@ public SpringMvcServiceRestMetadataResolver(ApplicationModel applicationModel) {
@Override
protected boolean supports0(Class<?> serviceType) {
// class @Controller or @RequestMapping
return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS) || isAnnotationPresent(serviceType, REQUEST_MAPPING_ANNOTATION_CLASS);
return isAnnotationPresent(serviceType, CONTROLLER_ANNOTATION_CLASS)
|| isAnnotationPresent(serviceType, FEIGN_CLIENT_CLASS)
|| isAnnotationPresent(serviceType, REQUEST_MAPPING_ANNOTATION_CLASS);
}

@Override
Expand All @@ -84,9 +87,10 @@ protected String resolveRequestMethod(Method serviceMethod, Class<?> serviceType

@Override
protected String resolveRequestPath(Method serviceMethod, Class<?> serviceType, Class<?> serviceInterfaceClass) {
String requestBasePath = resolveRequestPath(serviceType);
String feignClientBasePath = resolveFeignClientBaseRequestPath(serviceType);
String requestMappingBasePath = resolveRequestPath(serviceType);
String requestRelativePath = resolveRequestPath(serviceMethod);
return buildPath(requestBasePath, requestRelativePath);
return buildPath(feignClientBasePath, buildPath(requestMappingBasePath, requestRelativePath));
}

@Override
Expand All @@ -103,6 +107,18 @@ protected void processConsumes(Method serviceMethod, Class<?> serviceType, Class
addMediaTypes(serviceInterfaceClass, "consumes", consumes);
}

private String resolveFeignClientBaseRequestPath(AnnotatedElement annotatedElement) {
Annotation feignClient = findAnnotation(annotatedElement, FEIGN_CLIENT_CLASS);

String path = getAttribute(feignClient, "path");

if (path == null) {
return "";
}

return path;
}

private String resolveRequestPath(AnnotatedElement annotatedElement) {
Annotation mappingAnnotation = getRequestMapping(annotatedElement);

Expand Down
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.metadata.rest.feign;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(path = "/feign")
@RequestMapping("/context")
public interface FeignClientController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.metadata.rest.feign;



public class FeignClientControllerImpl implements FeignClientController{

@Override
public String hello() {
return "hello, feign";
}
}
@@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.metadata.rest.springmvc;

import org.apache.dubbo.metadata.rest.PathMatcher;
import org.apache.dubbo.metadata.rest.RestMethodMetadata;
import org.apache.dubbo.metadata.rest.ServiceRestMetadata;
import org.apache.dubbo.metadata.rest.feign.FeignClientController;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

public class FeignClientAnnotationTest {

private SpringMvcServiceRestMetadataResolver instance = new SpringMvcServiceRestMetadataResolver(ApplicationModel.defaultModel());

@Test
void testFeignClientAnnotationResolve() {

Assertions.assertEquals(true, instance.supports(FeignClientController.class));
Class service = FeignClientController.class;
ServiceRestMetadata serviceRestMetadata = new ServiceRestMetadata();
serviceRestMetadata.setServiceInterface(service.getName());

ServiceRestMetadata resolve = instance.resolve(service, serviceRestMetadata);

Map<PathMatcher, RestMethodMetadata> unContainPathVariableToServiceMap = resolve.getPathUnContainPathVariableToServiceMap();
RestMethodMetadata restMethodMetadata = unContainPathVariableToServiceMap.get(PathMatcher.getInvokeCreatePathMatcher("/feign/context/hello", null, null, null, "GET"));
Assertions.assertNotNull(restMethodMetadata);
}
}
6 changes: 6 additions & 0 deletions dubbo-rpc/dubbo-rpc-rest/pom.xml
Expand Up @@ -149,5 +149,11 @@
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,78 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.rpc.protocol.rest;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.rpc.Exporter;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.ProxyFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.FrameworkModel;
import org.apache.dubbo.rpc.model.ModuleServiceRepository;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.protocol.rest.mvc.feign.FeignClientController;
import org.apache.dubbo.rpc.protocol.rest.mvc.feign.FeignClientControllerImpl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;


public class FeignClientRestProtocolTest {
private Protocol protocol = ExtensionLoader.getExtensionLoader(Protocol.class).getExtension("rest");
private ProxyFactory proxy = ExtensionLoader.getExtensionLoader(ProxyFactory.class).getAdaptiveExtension();
private final ModuleServiceRepository repository = ApplicationModel.defaultModel().getDefaultModule().getServiceRepository();


@AfterEach
public void tearDown() {
protocol.destroy();
FrameworkModel.destroyAll();
}


@Test
void testRestProtocol() {
URL url = URL.valueOf("rest://127.0.0.1:" + NetUtils.getAvailablePort() + "/?version=1.0.0&interface=org.apache.dubbo.rpc.protocol.rest.mvc.feign.FeignClientController");

FeignClientController server = new FeignClientControllerImpl();

url = this.registerProvider(url, server, DemoService.class);

Exporter<FeignClientController> exporter = protocol.export(proxy.getInvoker(server, FeignClientController.class, url));

FeignClientController feignClientController = this.proxy.getProxy(protocol.refer(FeignClientController.class, url));

Assertions.assertEquals("hello, feign",feignClientController.hello());
exporter.unexport();
}


private URL registerProvider(URL url, Object impl, Class<?> interfaceClass) {
ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);
ProviderModel providerModel = new ProviderModel(
url.getServiceKey(),
impl,
serviceDescriptor,
null,
null);
repository.registerProvider(providerModel);
return url.setServiceModel(providerModel);
}
}
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.rpc.protocol.rest.mvc.feign;


import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(path = "/feign")
@RequestMapping("/context")
public interface FeignClientController {

@RequestMapping(value = "/hello", method = RequestMethod.GET)
String hello();
}
@@ -0,0 +1,27 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 org.apache.dubbo.rpc.protocol.rest.mvc.feign;



public class FeignClientControllerImpl implements FeignClientController{

@Override
public String hello() {
return "hello, feign";
}
}