Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions 2-advanced/dubbo-samples-stub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Dubbo-Samples-Stub
[本地存根相关文档](https://dubbo.apache.org/zh-cn/docs3-v2/java-sdk/advanced-features-and-usage/service/local-stub/)
* 该案例是关于实现本地存根的。远程服务后,客户端通常只剩下接口,而实现全在服务器端,但提供方有些时候想在客户端也执行部分逻辑。
* 该案例的实现方式就是在interface包中定义DemoServiceStub,之后关键是在consumer端的service类声明的@DubboReference注解中设置stub属性为定义的stub类,设置interfaceName属性为interface的name。

33 changes: 27 additions & 6 deletions 2-advanced/dubbo-samples-stub/case-configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from: app-builtin-zookeeper.yml
services:
zookeeper:
image: zookeeper:latest

props:
project_name: dubbo-samples-stub
main_class: org.apache.dubbo.samples.stub.StubProvider
zookeeper_port: 2181
dubbo_port: 20880
provider:
type: app
basedir: dubbo-samples-stub-provider
mainClass: org.apache.dubbo.samples.stub.provider.StubProvider
systemProps:
- zookeeper.address=zookeeper
waitPortsBeforeRun:
- zookeeper:2181
checkPorts:
- 20880
checkLog: "Current Spring Boot Application is await..."

test:
type: test
basedir: dubbo-samples-stub-consumer
tests:
- "**/*IT.class"
systemProps:
- zookeeper.address=zookeeper
waitPortsBeforeRun:
- zookeeper:2181
- provider:20880
depends_on:
- provider

94 changes: 94 additions & 0 deletions 2-advanced/dubbo-samples-stub/dubbo-samples-stub-consumer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-samples-stub</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dubbo-samples-stub-consumer</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>

<!-- spring starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-samples-stub-interface</artifactId>
<version>${project.parent.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
<type>pom</type>
<exclusions>
<exclusion>
<artifactId>slf4j-reload4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-maven-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <goals>-->
<!-- <goal>repackage</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* 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.samples.stub.consumer;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.samples.stub.DemoService;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
@EnableDubbo
public class StubConsumer {
private static Logger logger = LoggerFactory.getLogger(StubConsumer.class);

@DubboReference(check=false, stub="org.apache.dubbo.samples.stub.DemoServiceStub", interfaceName = "org.apache.dubbo.samples.stub.DemoService")
private DemoService demoService;

public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(StubConsumer.class, args);
DemoService demoService = context.getBean(DemoService.class);
logger.info("result: " + demoService.sayHello("dubbo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.

spring:
application:
name: dubbo-stub-demo-consumer

dubbo:
application:
name: ${spring.application.name}
qos-port: 33333
protocol:
name: dubbo
port: -1
registry:
address: zookeeper://${zookeeper.address:127.0.0.1}:2181
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,18 @@
*
*/

package org.apache.dubbo.samples.stub;
package org.apache.dubbo.samples.stub.test;

import org.apache.dubbo.samples.stub.api.DemoService;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class StubConsumer {
private static Logger logger = LoggerFactory.getLogger(StubConsumer.class);
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {

public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/stub-consumer.xml");
context.start();
DemoService demoService = context.getBean("demoService", DemoService.class);
logger.info("result: " + demoService.sayHello("dubbo"));
SpringApplication.run(ConsumerApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
* 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.samples.stub.test;

import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.samples.stub.DemoService;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ConsumerIT {
@DubboReference(check=false, stub="org.apache.dubbo.samples.stub.DemoServiceStub", interfaceName = "org.apache.dubbo.samples.stub.DemoService")
private DemoService demoService;

@Test
public void test() {
String result = demoService.sayHello("world");
Assert.assertEquals("stub - greeting world", result);
}
}
45 changes: 45 additions & 0 deletions 2-advanced/dubbo-samples-stub/dubbo-samples-stub-interface/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-samples-stub</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dubbo-samples-stub-interface</artifactId>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*
*/

package org.apache.dubbo.samples.stub.api;
package org.apache.dubbo.samples.stub;

public interface DemoService {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
*
*/

package org.apache.dubbo.samples.stub.api;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package org.apache.dubbo.samples.stub;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;

public class DemoServiceStub implements DemoService {
private static Logger logger = LoggerFactory.getLogger(DemoServiceStub.class);

private final DemoService demoService;

public DemoServiceStub(DemoService demoService) {
Expand Down
Loading