zookeeper 默认端口号 2181
公共项目
public interface UserService { public User queryUser(Integer id); }
提供者 pom.xml ,普通的maven项目,不是web项目
<dependencies> <!--加入公共项目的依赖,在springboot-interface-api模块中--> <dependency> <groupId>org.example</groupId> <artifactId>springboot-interface-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!--dubbo依赖--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- zookeeper--> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>3.0.4</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
提供者 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项目
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.example</groupId> <artifactId>springboot-interface-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.apache.dubbo</groupId> <artifactId>dubbo-dependencies-zookeeper</artifactId> <version>3.0.4</version> <type>pom</type> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
消费者 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