一.服务提供模块
1.pom.xml依赖
<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>com.example</groupId>
<artifactId>dubbo-provider</artifactId>
<version>1.0.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- Dubbo Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring Boot Test Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jyw</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.提供服务接口
public interface HelloService {
String sayHello(String name);
}
import com.jyw.service.HelloService;
import org.apache.dubbo.config.annotation.DubboService;
@DubboService// 使用 DubboService 注解暴露服务
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return name+",你好!";
}
}
3.启动类
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@DubboComponentScan(basePackages = "com.jyw.service")
@SpringBootApplication
public class DubboProviderApplication {
public static void main(String[] args){
SpringApplication.run(DubboProviderApplication.class,args);
}
}
4.配置文件application.yml
server:
port: 8081
spring:
application:
name: dubbo-provider
dubbo:
application:
name: dubbo-provider
registry:
address: zookeeper://127.0.0.1:2181 # Zookeeper 注册中心地址
protocol:
name: dubbo
port: 20880 # Dubbo 服务端口
二.服务消费模块
1.pom.xml依赖
<?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>com.jyw</groupId>
<artifactId>dubbo-web</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<!-- Spring Boot Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Dubbo Starter -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.1.0</version>
</dependency>
<!-- Spring Boot Test Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jyw</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.消费服务接口
import com.jyw.service.HelloService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/user")
public class HelloController {
@DubboReference// 使用 DubboReference 注解引用远程服务
// 这里引用的方式是直接在消费服务里面新建一个一模一样的接口,也可以把接口都抽出来放到一个模块中
HelloService helloService;
@GetMapping("/sayHello")
public String sayHello(@RequestParam("name") String name){
return helloService.sayHello(name);
}
}`
public interface HelloService {
String sayHello(String name);
}
3.启动类
import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@DubboComponentScan(basePackages = "com.jyw.controller")
@SpringBootApplication
public class DubboConsumerApplication {
public static void main(String[] args){
SpringApplication.run(DubboConsumerApplication.class,args);
}
}
4.配置文件application.yml
server:
port: 8082
spring:
application:
name: dubbo-consumer
dubbo:
application:
name: dubbo-consumer
registry:
address: zookeeper://127.0.0.1:2181 # Zookeeper 注册中心地址
三.访问
http://localhost:8082/user/sayHello?name=xx
返回:“xx”,你好!
注意:消费者和提供者方法路径要一致