上一篇环境搭建:https://blog.csdn.net/qq_41085151/article/details/107984842
1.添加pom依赖
<!-- springboot版本2.1.5 -->
<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>
<!--整合dubbo+zk-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
2.添加配置
server.port=8093
dubbo.application.name=provider
dubbo.registry.address=zookeeper://192.168.221.150:2181
dubbo.scan.base-packages=com.example.springbootprovider.service
3.编写service,注册到zk中去。
public interface UserService {
public String getUser();
}
@Component
@Service //dubbo里的service
public class UserServiceImpl implements UserService {
@Override
public String getUser() {
return "获取用户";
}
}
4.运行服务,可以到dubbo-admin中查看
5.再同目录创建Module消费者项目,添加依赖
<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>
<!--整合dubbo-->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>2.7.0</version>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.9</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.12.0</version>
</dependency>
<!-- 添加提供者的依赖-->
<dependency>
<groupId>com.example</groupId>
<artifactId>springboot-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
6.添加配置
server.port=8084
dubbo.application.name=consumer
dubbo.registry.address=zookeeper://192.168.221.150:2181
7.创建service调用
@Service //spring的service注解
public class TestService {
@Reference //远程调用
UserService userService;
public void buyUser(){
System.out.println(userService.getUser());
}
}
8.配置控制层调用,打印台就会拿到数据了
@RestController
public class TestController {
@Autowired
UserService userService;
@RequestMapping("/testdubbo")
public void test(){
userService.buyTicker();
}
}