1、工程架构
2、创建模块
(1)创建父工程,引入公共依赖
pom.xml依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
</dependencies>
(2)创建common:公共接口模块(model,service,exception…)
作用:定义公共接口,也可以导入公共依赖
public interface UserService {
public String sayHello(String name);
}
(3)创建dubbo_provider子模块:生产者模块(对用户接口的实现)
1)pom.xml
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2)Service
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.common.UserService;
import org.springframework.stereotype.Component;
@Service //这个注解是dubbo的注解,表示对外暴露可以调用的接口
public class UserServiceImpl implements UserService {
public String sayHello(String name) {
return "hello:"+name;
}
}
3)创建启动类
@EnableDubbo //这个注解表示开启dubbo的注解扫描
@SpringBootApplication
public class ProApplication {
public static void main(String[] args) {
SpringApplication.run(ProApplication.class, args);
}
}
4)创建application.properties
dubbo.application.name=service-provide
dubbo.registry.address=192.168.200.130:2181
#dubbo.registry.address=zookeeper://192.168.200.130:2191?backup=192.168.200.130:2192,192.168.200.130:2193
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
(4)创建dubbo_consumer子模块:消费者模块(调用生产者模块)
1)pom.xml
<dependencies>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
2)创建controller,调用生产者
@RestController
@RequestMapping("/user")
public class UserController {
@Reference //这个注解表示远程调用dubbo
private UserService userService;
@GetMapping("{name}")
public String getData(@PathVariable String name) {
String value = userService.sayHello(name);
return value;
}
}
3)创建启动类
@EnableDubbo
@SpringBootApplication
public class ConApplication {
public static void main(String[] args) {
SpringApplication.run(ConApplication.class, args);
}
}
4)创建application.properties配置文件
server.port=8081
dubbo.application.name=boot-order-service-consumer
dubbo.registry.address=zookeeper://192.168.200.130:2181
#dubbo.registry.address=zookeeper://192.168.200.129:2191?backup=192.168.200.129:2192,192.168.200.129:2193
(5)测试
1)启动注册中心
2)启动生产者服务
3)启动消费者服务
4)服务消费者服务中的controller看结果