前言
服务提供者
- 添加依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
- 服务接口
public interface IUserService {
public List<User> getUserAll();
}
- 服务实现类
@com.alibaba.dubbo.config.annotation.Service // 发布服务要用dubbo下面的注解
public class UserServieImpl implements IUserService {
@Override
public List<User> getUserAll() {
System.out.println("UserServieImpl.getUserAll 4 1 2");
return null;
}
}
- dubbo配置
spring:
datasource:
password: root
username: root
driver-class-name: com.MysqL.jdbc.Driver
url: jdbc:MysqL://localhost:3306/web_1
mybatis-plus:
type-aliases-package: com.qf.entity
dubbo:
application:
name: user-service
registry:
address: zookeeper://192.168.148.200:2181
protocol:
port: -1
- 主启动类
@SpringBootApplication(scanBasePackages = "com.qf")
//暴露的服务要让dubbo扫到
@dubboComponentScan(basePackages = "com.qf.service.impl")
public class UserServiceUserApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceUserApplication.class, args);
}
}
服务消费者
- 添加依赖
<dependency>
<groupId>com.alibaba.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>0.2.0</version>
</dependency>
- Controller中调用
@Controller
public class UserController {
@Reference
private IUserService userService;
@RequestMapping(value = "/getUserList")
public String getUserList(Model model) throws Exception {
System.out.println("UserController.getUserList");
// userService.getUserAll();
return "userList";
}
}
- 主启动类上面加上忽略数据源的,因为数据源服务提供者层配了,不关消费者的事情
@SpringBootApplication(scanBasePackages = “com.qf”,exclude = DataSourceAutoConfiguration.class) - 服务消费者配置
dubbo:
application:
name: user-web
registry:
address: zookeeper://192.168.148.200:2181
consumer:
check: false # 关闭所有服务的启动时检查:(默认没有提供者时报错) 写在定义服务消费者一方
retries: 3 #失败重试次数
运行项目
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。