Skip to content

Commit

Permalink
Polish apache#3355 : fix the issues in samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 29, 2019
1 parent ebeb1dd commit 5026c70
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,12 @@
*/
package com.alibaba.dubbo.config.annotation;

import com.alibaba.dubbo.common.Constants;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;

/**
* Reference
*
Expand Down Expand Up @@ -119,10 +115,9 @@
/**
* The communication protocol of Dubbo Service
*
* @return the default value is "dubbo"
* @see Constants#DEFAULT_PROTOCOL
* @return the default value is ""
* @since 2.6.6
*/
String protocol() default DEFAULT_PROTOCOL;
String protocol() default "";

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.beans.PropertyEditorSupport;
import java.util.Map;

import static com.alibaba.dubbo.common.Constants.DEFAULT_PROTOCOL;
import static com.alibaba.dubbo.config.spring.util.BeanFactoryUtils.getOptionalBean;
import static com.alibaba.dubbo.config.spring.util.ObjectUtils.of;
import static org.springframework.util.StringUtils.commaDelimitedListToStringArray;
Expand Down Expand Up @@ -122,6 +123,22 @@ public void setAsText(String text) throws java.lang.IllegalArgumentException {
// Bind annotation attributes
dataBinder.bind(new AnnotationPropertyValuesAdapter(reference, applicationContext.getEnvironment(), IGNORE_FIELD_NAMES));

setProtocolIfAbsent(reference, referenceBean);
}

/**
* If ReferenceBean.protocol is empty and @Reference.protocol() is the default value,
* ReferenceBean.protocol is about to set the default protocol forcibly.
*
* @param reference {@link Reference} annotation
* @param referenceBean {@link ReferenceBean} Object
* @since 2.6.6
*/
private void setProtocolIfAbsent(Reference reference, ReferenceBean referenceBean) {
if (!StringUtils.hasText(referenceBean.getProtocol())
&& DEFAULT_PROTOCOL.equals(reference.protocol())) {
referenceBean.setProtocol(DEFAULT_PROTOCOL);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@

package com.alibaba.dubbo.config.spring.api;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Path("/hello-service")
public interface HelloService {
String sayHello(String name);

@GET
String sayHello(@QueryParam("name") String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,26 @@ static class ConsumerConfiguration {
@Reference(version = "${hello.service.version}", protocol = "dubbo")
private HelloService dubboHelloService;

// @Reference(version = "${hello.service.version}", protocol = "rest")
// private HelloService restHelloService;
@Reference(version = "${hello.service.version}", protocol = "rest")
private HelloService restHelloService;

// @Bean
// public ReferenceBean<HelloService> restReferenceBean(@Value("${hello.service.version}") String version) {
// ReferenceBean<HelloService> referenceBean = new ReferenceBean<HelloService>();
// referenceBean.setVersion(version);
// referenceBean.setProtocol("rest");
// referenceBean.setInterface(HelloService.class);
// return referenceBean;
// }

}

// @ImportResource("classpath:/META-INF/spring/dubbo-rest-consumer.xml")
// @Configuration
// static class ConsumerXMLConfiguration {
// }


public static void main(String[] args) {

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
Expand All @@ -54,7 +69,7 @@ public static void main(String[] args) {

ConsumerConfiguration configuration = context.getBean(ConsumerConfiguration.class);
System.out.println(configuration.dubboHelloService.sayHello("mercyblitz"));
// System.out.println(configuration.restHelloService.sayHello("mercyblitz"));
System.out.println(configuration.restHelloService.sayHello("mercyblitz"));

context.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,11 @@
import com.alibaba.dubbo.config.spring.api.HelloService;
import com.alibaba.dubbo.rpc.RpcContext;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@Service(version = "${hello.service.version}", protocol = {"dubbo"})
@Path("/hello-service")
@Service(version = "${hello.service.version}", protocol = {"dubbo", "rest"})
public class DefaultHelloService implements HelloService {

@Override
@GET
public String sayHello(@QueryParam("name") String name) {
public String sayHello(String name) {
return String.format("[%s] Hello , %s", RpcContext.getContext().getUrl(), name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package com.alibaba.dubbo.config.spring.beans.factory.annotation.multiple.provider;

import com.alibaba.dubbo.config.spring.beans.factory.annotation.multiple.EmbeddedZooKeeper;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand All @@ -35,8 +34,8 @@
public class MultipleProtocolsServiceProvider {

public static void main(String[] args) throws IOException {
EmbeddedZooKeeper embeddedZooKeeper = new EmbeddedZooKeeper(2181, false);
embeddedZooKeeper.start();
// EmbeddedZooKeeper embeddedZooKeeper = new EmbeddedZooKeeper(2181, false);
// embeddedZooKeeper.start();

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(MultipleProtocolsServiceProvider.class);
Expand All @@ -46,6 +45,6 @@ public static void main(String[] args) throws IOException {
System.in.read();

context.close();
embeddedZooKeeper.stop();
// embeddedZooKeeper.stop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">

<dubbo:application name="dubbo-rest-consumer"/>

<dubbo:registry address="zookeeper://127.0.0.1:2181"/>

<dubbo:reference id="restReferenceBean" interface="com.alibaba.dubbo.config.spring.api.HelloService"
version="1.0.0"
protocol="rest"/>

</beans>

0 comments on commit 5026c70

Please sign in to comment.