From e8853ce7aca6938385980c1ec4bed172f08666b8 Mon Sep 17 00:00:00 2001 From: yovinchen Date: Mon, 4 Dec 2023 16:13:40 +0800 Subject: [PATCH] Initial commit --- 3-dubbo-spring-boot-demo/REDAME.md | 19 ++++++ .../dubbo-spring-boot-demo-consumer/pom.xml | 50 ++++++++++++++ .../demo/consumer/ConsumerApplication.java | 21 ++++++ .../dubbo/springboot/demo/consumer/Task.java | 39 +++++++++++ .../src/main/resources/application.yml | 8 +++ .../dubbo-spring-boot-demo-interface/pom.xml | 20 ++++++ .../dubbo/springboot/demo/DemoService.java | 12 ++++ .../dubbo-spring-boot-demo-provider/pom.xml | 50 ++++++++++++++ .../demo/provider/DemoServiceImpl.java | 20 ++++++ .../demo/provider/ProviderApplication.java | 20 ++++++ .../src/main/resources/application.yml | 8 +++ 3-dubbo-spring-boot-demo/pom.xml | 66 +++++++++++++++++++ dubbo-api-task/pom.xml | 44 +++++++++++++ .../dubbo/samples/api/GreetingsService.java | 13 ++++ .../dubbo/samples/client/Application.java | 33 ++++++++++ .../dubbo/samples/provider/Application.java | 32 +++++++++ .../provider/GreetingsServiceImpl.java | 17 +++++ 17 files changed, 472 insertions(+) create mode 100644 3-dubbo-spring-boot-demo/REDAME.md create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/pom.xml create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/ConsumerApplication.java create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/Task.java create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/resources/application.yml create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/pom.xml create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/src/main/java/org/apache/dubbo/springboot/demo/DemoService.java create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/pom.xml create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/DemoServiceImpl.java create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/ProviderApplication.java create mode 100644 3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/resources/application.yml create mode 100644 3-dubbo-spring-boot-demo/pom.xml create mode 100644 dubbo-api-task/pom.xml create mode 100644 dubbo-api-task/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java create mode 100644 dubbo-api-task/src/main/java/org/apache/dubbo/samples/client/Application.java create mode 100644 dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/Application.java create mode 100644 dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java diff --git a/3-dubbo-spring-boot-demo/REDAME.md b/3-dubbo-spring-boot-demo/REDAME.md new file mode 100644 index 0000000..6fcff8c --- /dev/null +++ b/3-dubbo-spring-boot-demo/REDAME.md @@ -0,0 +1,19 @@ +# 基于 Spring Boot Starter 开发微服务应用 + +基于 Apache Zookeeper 注册中心(仅限于测试环境) + +```she +Windows: +git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git +cd dubbo-samples +./mvnw.cmd clean compile exec:java -pl tools/embedded-zookeeper + +Linux / MacOS: +git clone --depth=1 --branch master git@github.com:apache/dubbo-samples.git +cd dubbo-samples +./mvnw clean compile exec:java -pl tools/embedded-zookeeper + +Docker: +docker run --name some-zookeeper -p 2181:2181 --restart always -d zookeeper +``` + diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/pom.xml b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/pom.xml new file mode 100644 index 0000000..5e104b7 --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.example + 3-dubbo-spring-boot-demo + 1.0-SNAPSHOT + + + dubbo-spring-boot-demo-consumer + + + 17 + 17 + UTF-8 + + + + + org.apache.dubbo + dubbo-spring-boot-starter + + + org.apache.dubbo + dubbo-dependencies-zookeeper-curator5 + pom + + + slf4j-reload4j + org.slf4j + + + + + + + org.springframework.boot + spring-boot-starter + + + org.example + dubbo-spring-boot-demo-interface + 1.0-SNAPSHOT + compile + + + + diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/ConsumerApplication.java b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/ConsumerApplication.java new file mode 100644 index 0000000..8aa4ace --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/ConsumerApplication.java @@ -0,0 +1,21 @@ +package org.apache.dubbo.springboot.demo.consumer; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * ClassName: Application + * Package: org.apache.dubbo.springboot.demo.consumer + * + * @author yovinchen + * @Create 2023/12/4 15:31 + */ +@SpringBootApplication +@EnableDubbo +public class ConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } +} diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/Task.java b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/Task.java new file mode 100644 index 0000000..c0589b8 --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/java/org/apache/dubbo/springboot/demo/consumer/Task.java @@ -0,0 +1,39 @@ +package org.apache.dubbo.springboot.demo.consumer; + +import org.apache.dubbo.config.annotation.DubboReference; +import org.apache.dubbo.springboot.demo.DemoService; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +import java.util.Date; + +/** + * ClassName: Task + * Package: org.apache.dubbo.springboot.demo.consumer + * + * @author yovinchen + * @Create 2023/12/4 15:33 + */ +@Component +public class Task implements CommandLineRunner { + @DubboReference + private DemoService demoService; + + @Override + public void run(String... args) throws Exception { + String result = demoService.sayHello("world"); + System.out.println("Receive result ======> " + result); + + new Thread(() -> { + while (true) { + try { + Thread.sleep(1000); + System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world")); + } catch (InterruptedException e) { + e.printStackTrace(); + Thread.currentThread().interrupt(); + } + } + }).start(); + } +} diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/resources/application.yml b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/resources/application.yml new file mode 100644 index 0000000..1484e0a --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-consumer/src/main/resources/application.yml @@ -0,0 +1,8 @@ +dubbo: + application: + name: dubbo-springboot-demo-consumer + protocol: + name: dubbo + port: -1 + registry: + address: zookeeper://${zookeeper.address:8.130.127.71}:2181 diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/pom.xml b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/pom.xml new file mode 100644 index 0000000..6223b4d --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/pom.xml @@ -0,0 +1,20 @@ + + + 4.0.0 + + org.example + 3-dubbo-spring-boot-demo + 1.0-SNAPSHOT + + + dubbo-spring-boot-demo-interface + + + 17 + 17 + UTF-8 + + + diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/src/main/java/org/apache/dubbo/springboot/demo/DemoService.java b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/src/main/java/org/apache/dubbo/springboot/demo/DemoService.java new file mode 100644 index 0000000..ba37a38 --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-interface/src/main/java/org/apache/dubbo/springboot/demo/DemoService.java @@ -0,0 +1,12 @@ +package org.apache.dubbo.springboot.demo; + +/** + * ClassName: DemoService + * Package: org.apache.dubbo.springboot.demo + * + * @author yovinchen + * @Create 2023/12/4 15:26 + */ +public interface DemoService { + String sayHello(String name); +} diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/pom.xml b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/pom.xml new file mode 100644 index 0000000..2e5f5ab --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.example + 3-dubbo-spring-boot-demo + 1.0-SNAPSHOT + + + dubbo-spring-boot-demo-provider + + + 17 + 17 + UTF-8 + + + + + org.apache.dubbo + dubbo-spring-boot-starter + + + org.apache.dubbo + dubbo-dependencies-zookeeper-curator5 + pom + + + slf4j-reload4j + org.slf4j + + + + + + + org.springframework.boot + spring-boot-starter + + + org.example + dubbo-spring-boot-demo-interface + 1.0-SNAPSHOT + compile + + + + diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/DemoServiceImpl.java b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/DemoServiceImpl.java new file mode 100644 index 0000000..62f05ad --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/DemoServiceImpl.java @@ -0,0 +1,20 @@ +package org.apache.dubbo.springboot.demo.provider; + +import org.apache.dubbo.config.annotation.DubboService; +import org.apache.dubbo.springboot.demo.DemoService; + +/** + * ClassName: DemoServiceImpl + * Package: org.apache.dubbo.springboot.demo.provider + * + * @author yovinchen + * @Create 2023/12/4 15:26 + */ +@DubboService +public class DemoServiceImpl implements DemoService { + + @Override + public String sayHello(String name) { + return "Hello " + name; + } +} diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/ProviderApplication.java b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/ProviderApplication.java new file mode 100644 index 0000000..b70d5e6 --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/java/org/apache/dubbo/springboot/demo/provider/ProviderApplication.java @@ -0,0 +1,20 @@ +package org.apache.dubbo.springboot.demo.provider; + +import org.apache.dubbo.config.spring.context.annotation.EnableDubbo; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * ClassName: Application + * Package: org.apache.dubbo.springboot.demo.provider + * + * @author yovinchen + * @Create 2023/12/4 15:31 + */ +@SpringBootApplication +@EnableDubbo +public class ProviderApplication { + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } +} diff --git a/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/resources/application.yml b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/resources/application.yml new file mode 100644 index 0000000..5ec3915 --- /dev/null +++ b/3-dubbo-spring-boot-demo/dubbo-spring-boot-demo-provider/src/main/resources/application.yml @@ -0,0 +1,8 @@ +dubbo: + application: + name: dubbo-springboot-demo-provider + protocol: + name: dubbo + port: -1 + registry: + address: zookeeper://${zookeeper.address:8.130.127.71}:2181 diff --git a/3-dubbo-spring-boot-demo/pom.xml b/3-dubbo-spring-boot-demo/pom.xml new file mode 100644 index 0000000..6af5cbd --- /dev/null +++ b/3-dubbo-spring-boot-demo/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + org.example + 3-dubbo-spring-boot-demo + 1.0-SNAPSHOT + pom + + dubbo-spring-boot-demo-interface + dubbo-spring-boot-demo-provider + dubbo-spring-boot-demo-consumer + + + + 3.2.0-beta.4 + 2.7.8 + 17 + 17 + UTF-8 + + + + + + + org.springframework.boot + spring-boot-dependencies + ${spring-boot.version} + pom + import + + + + + org.apache.dubbo + dubbo-bom + ${dubbo.version} + pom + import + + + + org.apache.dubbo + dubbo-dependencies-zookeeper-curator5 + ${dubbo.version} + pom + + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${spring-boot.version} + + + + + diff --git a/dubbo-api-task/pom.xml b/dubbo-api-task/pom.xml new file mode 100644 index 0000000..d068452 --- /dev/null +++ b/dubbo-api-task/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + org.example + dubbo-api-task + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + + org.apache.dubbo + dubbo + 3.2.0-beta.4 + + + + org.apache.curator + curator-x-discovery + 4.3.0 + + + org.apache.zookeeper + zookeeper + 3.8.0 + + + io.netty + netty-handler + + + io.netty + netty-transport-native-epoll + + + + + diff --git a/dubbo-api-task/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java new file mode 100644 index 0000000..a0fdfa9 --- /dev/null +++ b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/api/GreetingsService.java @@ -0,0 +1,13 @@ +package org.apache.dubbo.samples.api; + +/** + * ClassName: GreetingsService + * Package: org.apache.dubbo.samples.api + * + * @author yovinchen + * @Create 2023/12/4 16:04 + */ +public interface GreetingsService { + + String sayHi(String name); +} diff --git a/dubbo-api-task/src/main/java/org/apache/dubbo/samples/client/Application.java b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/client/Application.java new file mode 100644 index 0000000..74b60ca --- /dev/null +++ b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/client/Application.java @@ -0,0 +1,33 @@ +package org.apache.dubbo.samples.client; + +import org.apache.dubbo.config.ReferenceConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.samples.api.GreetingsService; + +import java.io.IOException; + +/** + * ClassName: Application + * Package: org.apache.dubbo.samples.client + * + * @author yovinchen + * @Create 2023/12/4 16:07 + */ + +public class Application { + public static void main(String[] args) throws IOException { + ReferenceConfig reference = new ReferenceConfig<>(); + reference.setInterface(GreetingsService.class); + + DubboBootstrap.getInstance() + .application("first-dubbo-consumer") + .registry(new RegistryConfig("zookeeper://8.130.127.71:2181")) + .reference(reference); + + GreetingsService service = reference.get(); + String message = service.sayHi("dubbo"); + System.out.println("Receive result ======> " + message); + System.in.read(); + } +} diff --git a/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/Application.java b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/Application.java new file mode 100644 index 0000000..50c5736 --- /dev/null +++ b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/Application.java @@ -0,0 +1,32 @@ +package org.apache.dubbo.samples.provider; + +import org.apache.dubbo.config.ProtocolConfig; +import org.apache.dubbo.config.RegistryConfig; +import org.apache.dubbo.config.ServiceConfig; +import org.apache.dubbo.config.bootstrap.DubboBootstrap; +import org.apache.dubbo.samples.api.GreetingsService; + +/** + * ClassName: Application + * Package: org.apache.dubbo.samples.provider + * + * @author yovinchen + * @Create 2023/12/4 16:05 + */ +public class Application { + public static void main(String[] args) { + // 定义具体的服务 + ServiceConfig service = new ServiceConfig<>(); + service.setInterface(GreetingsService.class); + service.setRef(new GreetingsServiceImpl()); + + // 启动 Dubbo + DubboBootstrap.getInstance() + .application("first-dubbo-provider") + .registry(new RegistryConfig("zookeeper://8.130.127.71:2181")) + .protocol(new ProtocolConfig("dubbo", -1)) + .service(service) + .start() + .await(); + } +} diff --git a/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java new file mode 100644 index 0000000..69ec350 --- /dev/null +++ b/dubbo-api-task/src/main/java/org/apache/dubbo/samples/provider/GreetingsServiceImpl.java @@ -0,0 +1,17 @@ +package org.apache.dubbo.samples.provider; + +import org.apache.dubbo.samples.api.GreetingsService; + +/** + * ClassName: GreetingsServiceImpl + * Package: org.apache.dubbo.samples.provider + * + * @author yovinchen + * @Create 2023/12/4 16:04 + */ +public class GreetingsServiceImpl implements GreetingsService { + @Override + public String sayHi(String name) { + return "hi, " + name; + } +}