一、前置准备:可参考前两篇文章
三方包依赖(本项目使用到)
二、代码编写
提供者
1. 项目结构:红色框框重点
2.代码和配置
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 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.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>dubbo-springboot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<!--三方工具-->
<dependency>
<groupId>com.example</groupId>
<artifactId>dubbo-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
application.properties
server.port=8081
dubbo.application.name=dubbo-provider
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
启动类添加@EnableDubbo注解
controller
package com.example.dubbospringbootprovider.controller;
import com.example.dubboapi.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Autowired
private HelloService helloService;
@RequestMapping("/hello")
@ResponseBody
public String hello(@RequestParam("name") String name){
return helloService.sayHello(name);
}
}
service
package com.example.dubbospringbootprovider.rpc;
import com.alibaba.dubbo.config.annotation.Service;
import com.example.dubboapi.service.HelloService;
import org.springframework.stereotype.Component;
@Service
@Component
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
return "provider send :hello "+name;
}
}
消费者
1. 项目结构
2.编写和配置
pom.xml 和提供者一样
application.properties
server.port=8082
dubbo.application.name=dubbo-consumer
dubbo.registry.address=zookeeper://localhost:2181
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
dubbo.monitor.protocol=registry
controller
package com.example.dubbospringbootconsumer.controller;
import com.example.dubbospringbootconsumer.service.impl.ConsumerServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@Autowired
private ConsumerServiceImpl consumerService;
@ResponseBody
@RequestMapping("/hello")
public String sayHello(@RequestParam("name") String name){
return consumerService.sayHello(name);
}
}
service
package com.example.dubbospringbootconsumer.service;
public interface ConsumerService {
String sayHello(String name);
}
package com.example.dubbospringbootconsumer.service.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import com.example.dubboapi.service.HelloService;
import com.example.dubbospringbootconsumer.service.ConsumerService;
import org.springframework.stereotype.Service;
@Service
public class ConsumerServiceImpl implements ConsumerService{
@Reference
private HelloService helloService;
@Override
public String sayHello(String name){
return helloService.sayHello(name);
}
}
此外主启动类不要忘记添加@EnableDubbo注解
三、结果
打开控制中心