springboot 整合dubbo最佳实践 (使用redis作为注册中心)
2018-08-07 18:06
1351 查看
一 概述
Apache Dubbo (incubating) |ˈdʌbəʊ| 是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。
面向接口代理的高性能RPC调用
提供高性能的基于代理的远程调用能力,服务以接口为粒度,为开发者屏蔽远程调用底层细节。智能负载均衡
内置多种负载均衡策略,智能感知下游节点健康状况,显著减少调用延迟,提高系统吞吐量。服务自动注册与发现
支持多种注册中心服务,服务实例上下线实时感知。高度可扩展能力
遵循微内核+插件的设计原则,所有核心能力如Protocol、Transport、Serialization被设计为扩展点,平等对待内置实现和第三方实现。运行期流量调度
内置条件、脚本等路由策略,通过配置不同的路由规则,轻松实现灰度发布,同机房优先等功能。可视化的服务治理与运维
提供丰富服务治理、运维工具:随时查询服务元数据、服务健康状态及调用统计,实时下发路由策略、调整配置参数。二 整合dubbo
1 创建公共服务接口module : wj-dubbo-api
定义公共接口: SpringServicepackage example.service; /** * @author: wangjun * @create: 2018/8/7 **/ public interface SpringService { /** * dubbo调用测试 * @return */ String dubboExample(); }
2 创建服务提供方项目 wj-dubbo-server
添加依赖:<dependencies> <!--dubbo 公共接口依赖--> <dependency> <groupId>com.wj.project</groupId> <artifactId>wj-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <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>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> <!--<version>0.2.0</version>--> </dependency> <!--使用redis做注册中心--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
添加dubbo 配置类
package com.wj.project.server.common.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; /** * @author: wangjun * @create: 2018/8/7 **/ @Configuration @DubboComponentScan("com.wj.project.server.example") // 扫描 Dubbo 组件 public class ProviderConfiguration { /** * 当前应用配置 */ @Bean("dubbo-annotation-provider") public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("wj-dubbo-server"); return applicationConfig; } /** * 当前连接注册中心配置 */ @Bean("my-registry") public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); // registryConfig.setAddress("zookeeper://localhost:2181"); registryConfig.setAddress("redis://localhost:6379"); return registryConfig; } /** * 当前连接注册中心配置 */ @Bean("dubbo") public ProtocolConfig protocolConfig() { ProtocolConfig protocolConfig = new ProtocolConfig(); protocolConfig.setName("dubbo"); protocolConfig.setPort(12345); return protocolConfig; } }
定义服务实现类:
package com.wj.project.server.example.service; import com.alibaba.dubbo.config.annotation.Service; import example.service.SpringService; /** * @author: wangjun * @create: 2018/8/7 **/ @Service public class SpringServiceImpl implements SpringService { @Override public String dubboExample() { return "hello world ,hello dubbo"; } }
3 创建服务消费方项目 :wj-dubbo-consumer、
添加依赖:<dependencies> <!--dubbo 公共接口依赖--> <dependency> <groupId>com.wj.project</groupId> <artifactId>wj-dubbo-api</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <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>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.1.0</version> </dependency> <!--使用redis做注册中心--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
添加消费方配置:
package com.wj.project.consumer.common.dubbo; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan; import com.wj.project.consumer.example.service.SpringConsumerService; /** * @author: wangjun * @create: 2018/8/7 **/ @Configuration @DubboComponentScan //默认扫描当前包及子包 public class ConsumerConfiguration { /** * 当前应用配置 */ @Bean public ApplicationConfig applicationConfig() { ApplicationConfig applicationConfig = new ApplicationConfig(); applicationConfig.setName("wj-dubbo-consumer"); return applicationConfig; } /** * 当前连接注册中心配置 */ @Bean public RegistryConfig registryConfig() { RegistryConfig registryConfig = new RegistryConfig(); registryConfig.setAddress("redis://localhost:6379"); return registryConfig; } /** * 仅供测试时使用,非测试时可注释 * <p> * 注册 AnnotationDemoServiceConsumer,@DubboComponentScan 将处理其中 @Reference 字段。 * 如果 AnnotationDemoServiceConsumer 非 Spring Bean 的话, * 即使 @DubboComponentScan 指定 package 也不会进行处理,与 Spring @Autowired 同理 */ @Bean public SpringConsumerService SpringConsumerService() { return new SpringConsumerService(); } }
添加消费service:
@Service public class SpringConsumerService { @Reference(url = "dubbo://10.9.26.208:12345") private SpringService springService; public String dubboExample() { return springService.dubboExample(); } }
4 添加测试类
测试前需启动 服务方项目package com.wj.project.consumer.example.test; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.wj.project.consumer.common.dubbo.ConsumerConfiguration; import com.wj.project.consumer.example.service.SpringConsumerService; /** * @author: wangjun * @create: 2018/8/7 **/ public class ConsumerBootstrap { public static void main(String[] args) { // 启动并且返回服务消费方上下文 ApplicationContext consumerContext = startConsumerContext(); // 获取 AnnotationDemoServiceConsumer Bean SpringConsumerService consumer = consumerContext.getBean(SpringConsumerService.class); // 执行 doSayHello 方法 String message = consumer.dubboExample(); // 输出执行结果 System.out.println(message); } /** * 启动并且返回服务消费方上下文 * * @return AnnotationConfigApplicationContext */ private static ApplicationContext startConsumerContext() { // 创建服务消费方 Annotation 配置上下文 AnnotationConfigApplicationContext consumerContext = new AnnotationConfigApplicationContext(); // 注册服务消费方配置 Bean consumerContext.register(ConsumerConfiguration.class); // 启动服务消费方上下文 consumerContext.refresh(); // 返回服务消费方 Annotation 配置上下文 return consumerContext; } }
5 结果
相关文章推荐
- Spring整合Dubbo,使用zookeeper作为注册中心,进行远程调用及负载均衡、自动失效转移(何志雄)
- springboot整合dubbo(注册中心zookeeper)无xml
- spring boot dubbo redis注册中心
- zookeeper作为注册中心,使用dubbo
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(五)Spring中spring-data-redis的使用
- 【转】SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
- dubbo(分布式架构)+zookeeper(注册中心)+spring(provider和consumer) 基本使用
- SpringBoot学习笔记(7) SpringBoot整合Dubbo(使用yml配置)
- spring boot 整合 redis,使用@Cacheable,@CacheEvict,@CachePut,jedisPool操作redis数据库
- spring-boot整合redis作为缓存(3)——自定义key
- spring-boot 整合redis作为数据缓存
- Spring Boot整合Dubbo使用及开发笔记
- spring-boot 整合redis作为数据缓存
- spring-boot整合redis作为mysql二级缓存
- spring boot 最佳实践(二)--使用Bean Validation
- spring-boot整合redis作为缓存(4)——spring-boot引入Redis
- REDIS学习(3.2)spring boot 使用redis作为缓存
- dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb3.4-redis3.2整合(六)Spring中Redis的缓存的使用
- spring-boot整合redis作为缓存(2)——spring-boot的缓存
- Dubbo框架初探【用Spring配置声明暴露服务(可以使用multicast广播注册中心暴露服务地址或者使用zookeeper注册中心暴露服务地址)、加载Spring配置,启动服务】