- 添加SpringBoot和Dubbo的相关依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.2.0</version>
</dependency>
</dependencies>
- 新建接口工程,新建一个实体类(如果需要使用dubbo传递,需实现Serializable接口)和一个服务接口
import java.io.Serializable;
public class User implements Serializable {
private String name;
private Integer age;
private Boolean gender;
private String description;
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;
}
public Boolean getGender() {
return gender;
}
public void setGender(Boolean gender) {
this.gender = gender;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
import java.util.concurrent.CompletableFuture;
public interface IHelloService {
String sayHi(String name);
CompletableFuture<String> sayHello(String name);
User save(User user);
}
- 创建服务的提供者,实现上述的服务接口,并注册到zookeeper上。
import com.sjb.entity.User;
import com.sjb.interfaces.IHelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Value;
import java.util.concurrent.CompletableFuture;
#这里的Service注解不再是SpringBoot提供的,而是由apache的dubbo提供
@Service
public class HelloService implements IHelloService {
@Value("${server.port}")
private Integer port;
@Override
public String sayHi(String name) {
return "Hi, " + name;
}
@Override
public CompletableFuture<String> sayHello(String name) {
return CompletableFuture.completedFuture(sayHi(name));
}
@Override
public User save(User user) {
user.setDescription(this.port.toString());
return user;
}
}
配置文件:
spring:
application:
name: provider
dubbo:
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://192.168.182.128:2181
consumer:
check: false
loadbalance: roundrobin
scan:
base-packages: com.wiki.dubbo.dubbo.service
server:
port: 9000
- 创建服务的消费者,并注册到zookeeper上
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Reference
private IHelloService helloService;
@RequestMapping
public String sayHi() {
return helloService.sayHi("pegasus");
}
@RequestMapping("sayHello")
public String sayHello() {
helloService.sayHello("异步").whenComplete((s, throwable) -> {
if (throwable == null) {
System.out.println(s);
} else {
throwable.printStackTrace();
}
});
return "执行中...";
}
@RequestMapping("save")
public User save() {
User user = new User();
user.setName("张三");
user.setAge(20);
user.setGender(true);
user.setDescription("");
return helloService.save(user);
}
}
配置文件:
spring:
application:
name: consumer
dubbo:
protocol:
name: dubbo
port: 20880
registry:
address: zookeeper://192.168.182.128:2181
consumer:
check: false
loadbalance: roundrobin
server:
port: 80
- git地址:https://gitee.com/PegasusKID_admin/dubbo.git