前言
最近想着实现一个springboot集成dubbo的demo案例,在网上找了很多相关的资料,但是大多数都没有成功,慢慢的不断采坑才实现这个demo。现在将这个案例分享大家
- 先引入所需要的的包
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.kevinxiaoge</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.kevinxiaoge</groupId>
<artifactId>dubbo-interface</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--Dubbo集成SpringBoot起步依赖-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.7</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
2.创建公共的接口,单独拆分成一个工程
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
创建好公共模块后,将工程install安装到本地仓库
3.创建提供者模块,也需要将模块单独拆分成一个工程。引入的包如上所示,不要忘记将我们刚才的公共模块的包也引进来。在加入配置文件
server:
port: 8379
dubbo:
application:
name: springboot-dubbo-provider-demo
registry:
address: zookeeper://xx.xx.51.188
port: 2181
protocol:
name: dubbo
port: 20880
提供者需要实现公共接口里面的接口
@DubboService
public class UserServiceImpl implements UserService {
@Override
public String sayHi(String name) {
return "hello world!!!";
}
@Override
public Student getStudent(int id) {
return null;
}
}
添加注解 import org.apache.dubbo.config.annotation.DubboService;
注意需要在启动类上加@EnableDubbo的注解
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
4.创建消费者,引入相关的包
server:
port: 8380
dubbo:
application:
name: springboot-dubbo-consumer-demo
registry:
address: zookeeper://xx.xx.51.188
port: 2181
protocol:
name: dubbo
port: 20881
# 注意:dubbo暴露的端口不要和提供者的端口重复
@RestController
public class UserController {
@DubboReference
private UserService userService;
@GetMapping("/sayHello")
public String sayHello(){
return userService.sayHi("kevinxiaoge");
}
}
使用注解 import org.apache.dubbo.config.annotation.DubboReference;
将所需要的接口类的实现注入进来
消费者的启动类上也需要注解 @EnableDubbo
5.最后看启动项目的效果
版权声明:本文为weixin_44293488原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。