zookeeper 默认端口号 2181
公共项目
public interface UserService {
public User queryUser(Integer id);
}
提供者 pom.xml ,普通的maven项目,不是web项目
org.example springboot-interface-api1.0-SNAPSHOT org.apache.dubbo dubbo-spring-boot-starter3.0.5 org.apache.dubbo dubbo-dependencies-zookeeper3.0.4 pom org.slf4j slf4j-log4j12org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest
提供者 service
@DubboService(interfaceClass = UserService.class,version = "1.0")
public class UserServiceImpl implements UserService {
@Override
public User queryUser(Integer id) {
User user = new User();
if(id == 1001){
user.setId(1001);
user.setName("张三");
user.setPwd("123");
}else if (id ==1002){
user.setId(1002);
user.setName("李四");
user.setPwd("1234");
}
return user;
}
}
提供者 启动类
@SpringBootApplication
@EnableDubbo //启用dubbo
public class SpringbootServiceProviderApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootServiceProviderApplication.class, args);
}
}
提供者 application.properties
#配置dubbo名字 dubbo:application-name spring.application.name=userservice-provider #配置扫描包 @扫描的@DubboService dubbo.scan.base-packages=com.abc.springbootserviceprovider.service dubbo.protocol.name=dubbo dubbo.protocol.port=28001 #注册中心 dubbo.registry.address=zookeeper://localhost:2181
消费者 pom.xml web项目
org.springframework.boot spring-boot-starter-weborg.example springboot-interface-api1.0-SNAPSHOT org.apache.dubbo dubbo-spring-boot-starter3.0.5 org.apache.dubbo dubbo-dependencies-zookeeper3.0.4 pom org.slf4j slf4j-log4j12org.springframework.boot spring-boot-starter-testtest
消费者 controller
@RestController
public class UserController {
@DubboReference(interfaceClass = UserService.class,version = "1.0")
private UserService userService;
@GetMapping("/getUser")
public String getUser(Integer id){
User user = userService.queryUser(id);
return user.toString();
}
}
消费者 启动类
@SpringBootApplication
@EnableDubbo
public class SpringbootConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootConsumerApplication.class, args);
}
}
消费者 application
#指定服务名称 spring.application.name=consumer-application dubbo.registry.address=zookeeper://localhost:2181



