将SpringBoot工程集成Dubbo
1.创建父工程
2.创建子工程consumer,provider
3.初始化工程
4.引入依赖
在provider和consumer中引入dubbo依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.0.7</version>
</dependency>
5.新建common模块
在common模块创建User类
User
@Data
@AllArgsConstructor
public class User implements Serializable {
private String uid;
private String username;
}
在common模块创建接口UserService
public interface UserService {
public User getUser(String uid);
}
Provider
1.pom中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<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>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.hejiawang</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
2.添加UserServiceImpl
使用@DubboService并且指定版本
@DubboService(version = "1.0")
public class UserServiceImpl implements UserService {
public User getUser(String uid){
User hejiawang = new User(uid,"hejiawang");
return hejiawang;
}
}
3.添加UserServiceImpl2
使用@DubboService并且指定版本
@DubboService(version = "2.0")
public class UserServiceImpl2 implements UserService {
public User getUser(String uid){
User hejiawang = new User(uid,"hejiawang");
return hejiawang;
}
}
4.在启动类中添加@EnableDubbo
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
Consumer
1.pom中添加依赖
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.hejiawang</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
2.添加OrderService
OrderService
@Service
public class OrderService {
@DubboReference(version = "1.0")
private UserService userService;
public User getOrderService(String uid){
return userService.getUser(uid);
}
}
3.添加OrderController
OrderController
@RestController
public class OrderController {
@Autowired
OrderService orderService;
@GetMapping("/user/{uid}")
public User getUser(@PathVariable("uid") String uid) {
return orderService.getOrderService(uid);
}
}
4.启动类添加@EnableDubbo
@SpringBootApplication
@EnableDubbo
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
测试
1.启动本地zk
2.启动provider,consumer
3.调用consumer服务的controller
localhost:8081/user/123
通过dubbo返回结果