Initial commit
This commit is contained in:
commit
e8853ce7ac
|
@ -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
|
||||
```
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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.example</groupId>
|
||||
<artifactId>3-dubbo-spring-boot-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dubbo-spring-boot-demo-consumer</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- dubbo -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
|
||||
<type>pom</type>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-reload4j</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- spring boot starter -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,20 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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.example</groupId>
|
||||
<artifactId>3-dubbo-spring-boot-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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);
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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.example</groupId>
|
||||
<artifactId>3-dubbo-spring-boot-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>dubbo-spring-boot-demo-provider</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<!-- dubbo -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
|
||||
<type>pom</type>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>slf4j-reload4j</artifactId>
|
||||
<groupId>org.slf4j</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<!-- spring boot starter -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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
|
|
@ -0,0 +1,66 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>3-dubbo-spring-boot-demo</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>pom</packaging>
|
||||
<modules>
|
||||
<module>dubbo-spring-boot-demo-interface</module>
|
||||
<module>dubbo-spring-boot-demo-provider</module>
|
||||
<module>dubbo-spring-boot-demo-consumer</module>
|
||||
</modules>
|
||||
|
||||
<properties>
|
||||
<dubbo.version>3.2.0-beta.4</dubbo.version>
|
||||
<spring-boot.version>2.7.8</spring-boot.version>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<!-- Spring Boot -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-dependencies</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Dubbo -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-bom</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
|
||||
<version>${dubbo.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
</project>
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>dubbo-api-task</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
<version>3.2.0-beta.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-x-discovery</artifactId>
|
||||
<version>4.3.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-handler</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-transport-native-epoll</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
|
@ -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);
|
||||
}
|
|
@ -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<GreetingsService> 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();
|
||||
}
|
||||
}
|
|
@ -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<GreetingsService> 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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue