如何从源码分析Dubbo与SpringBoot整合以及进行Dubbo启动
Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,是阿里巴巴SOA服务化治理方案的核心框架,提供了Java语言版本的服务治理框架。
Dubbo与SpringBoot整合,可以使用SpringBoot的注解方式来配置和使用Dubbo。
1. 在pom.xml中引入依赖
2. 在application.yml中配置dubbo
server: port: 8081 spring: application: name: spring-boot-dubbo-provider dubbo: application: name: spring-boot-dubbo-provider # 服务提供方注册中心地址 registry: address: zookeeper://127.0.0.1:2181 # 服务提供方协议配置 protocol: name: dubbo port: 20881 # 服务提供方监控中心配置 monitor: protocol: registry: address: zookeeper://127.0.0.1:2181 # 服务提供方配置 provider: timeout: 1000 # 服务消费方配置 consumer: timeout: 1000
3. 在启动类中添加@EnableDubboConfiguration注解
@SpringBootApplication @EnableDubboConfiguration public class SpringBootDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDubboProviderApplication.class, args); } }
4. 在服务提供方实现类上添加@Service注解
@Service public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
5. 在服务提供方接口上添加@Service注解
@Service public interface UserService { User getUserById(Long id); }
6. 在服务提供方接口实现类上添加@Service注解
@Service public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
7. 在服务消费方实现类上添加@Reference注解
@Reference public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
8. 在服务消费方接口上添加@Reference注解
@Reference public interface UserService { User getUserById(Long id); }
9. 在服务消费方接口实现类上添加@Reference注解
@Reference public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
10. 启动类中添加@EnableDubboConfiguration注解
@SpringBootApplication @EnableDubboConfiguration public class SpringBootDubboProviderApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDubboProviderApplication.class, args); } }
11. 在application.yml中配置dubbo
server: port: 8081 spring: application: name: spring-boot-dubbo-consumer dubbo: application: name: spring-boot-dubbo-consumer # 服务消费方注册中心地址 registry: address: zookeeper://127.0.0.1:2181 # 服务消费方协议配置 protocol: name: dubbo port: 20882 # 服务消费方监控中心配置 monitor: protocol: registry: address: zookeeper://127.0.0.1:2181 # 服务消费方配置 consumer: timeout: 1000
12. 在启动类中添加@EnableDubboConfiguration注解
@SpringBootApplication @EnableDubboConfiguration public class SpringBootDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDubboConsumerApplication.class, args); } }
13. 在服务消费方实现类上添加@Service注解
@Service public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
14. 在服务消费方接口上添加@Service注解
@Service public interface UserService { User getUserById(Long id); }
15. 在服务消费方接口实现类上添加@Service注解
@Service public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
16. 在服务消费方实现类上添加@Reference注解
@Reference public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
17. 在服务消费方接口上添加@Reference注解
@Reference public interface UserService { User getUserById(Long id); }
18. 在服务消费方接口实现类上添加@Reference注解
@Reference public class UserServiceImpl implements UserService { @Override public User getUserById(Long id) { User user = new User(); user.setId(id); user.setUsername("张三"); user.setPassword("123456"); return user; } }
19. 启动类中添加@EnableDubboConfiguration注解
@SpringBootApplication @EnableDubboConfiguration public class SpringBootDubboConsumerApplication { public static void main(String[] args) { SpringApplication.run(SpringBootDubboConsumerApplication.class, args); } }
通过以上步骤,Dubbo与SpringBoot整合完成。
相关文章