之前练习做过一个网上商城,采用SSM框架实现。该项目有一个商品排行榜模块没有实现,决定采用springboot+dubbo+redis实现该模块。
1.新建springboot项目。
本项目的springboot版本号为:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.6.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
引入dubbo及redis起始依赖。
<!--引入dubbo-stater--> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!--添加redis起始依赖--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.application.properties中配置如下信息
dubbo.application.name = baba-product-rank
dubbo.registry.address=127.0.0.1:2181 #zookeeper地址及端口号
dubbo.registry.protocol=zookeeper
dubbo.protocol.name=dubbo
dubbo.protocol.port=20881
#配置redis服务器
spring.redis.host=192.168.1.109 #redis服务器地址
spring.redis.port=6379
3.创建ProductRank类,并实现序列化
import java.io.Serializable; public class ProductRank implements Serializable { private String productId; private String productName; private String price; private String imgUrl; public String getProductId() { return productId; } public void setProductId(String productId) { this.productId = productId; } public String getProductName() { return productName; } public void setProductName(String productName) { this.productName = productName; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getImgUrl() { return imgUrl; } public void setImgUrl(String imgUrl) { this.imgUrl = imgUrl; } }
4.新建service接口,创建该service与ProductRank类时应注意类路径与之前项目路径一致(dubbo引用服务时要求)。
import cn.itcast.core.bean.ProductRank; import java.util.List; import java.util.Set; public interface ProductRankService { // 若没有该数据记录则添加该数据以及score 若有则直接给score加count public void zSetAdd(ProductRank productRank, double count); //获取(end-begin)个记录的排名 public List<Object> getSetRank(Long begin, Long end); }
5.创建redisconfg类,用来向容器中注入redisTemplate以及zSetOperations。
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.databind.ObjectMapper; @Configuration public class RedisConfig { @Bean @SuppressWarnings("all") public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<String, Object>(); template.setConnectionFactory(factory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper om = new ObjectMapper(); om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(om); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); // key采用String的序列化方式 template.setKeySerializer(stringRedisSerializer); // hash的key也采用String的序列化方式 template.setHashKeySerializer(stringRedisSerializer); // value序列化方式采用jackson template.setValueSerializer(jackson2JsonRedisSerializer); // hash的value序列化方式采用jackson template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
6.创建serviceimpl实现类。
import cn.itcast.core.bean.ProductRank; import com.alibaba.dubbo.config.annotation.Service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Set; @Service //暴露服务 @Component public class ProductRankServiceImpl implements ProductRankService{ @Autowired private RedisTemplate redisTemplate; @Autowired private ZSetOperations<String,Object> zSetOperations; @Override public void zSetAdd(ProductRank productRank,double count) { String sortSetName = "product:rank"; zSetOperations.incrementScore(sortSetName,productRank,count); } @Override public List<Object> getSetRank(Long begin, Long end) { List<Object> list = new ArrayList<>(); String sortSetName = "product:rank"; Set<Object> set= zSetOperations.reverseRange(sortSetName,begin,end); Iterator it = set.iterator(); while(it.hasNext()){ ProductRank p = (ProductRank)it.next(); list.add(p); } return list; } }
7.起始类上添加@EnableDubbo注解
@EnableDubbo @SpringBootApplication public class BabaProductRankApplication { public static void main(String[] args) { SpringApplication.run(BabaProductRankApplication.class, args); } }
8.在网上商城项目中引入dubbo依赖
<!-- 添加dubbo组件 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.6.2</version> </dependency> <!-- 注册中心使用的是zookeeper,引入操作zookeeper的客户端端 --> <dependency> <groupId>org.apache.curator</groupId> <artifactId>curator-framework</artifactId> <version>2.12.0</version> </dependency>
9.在与新建springboot项目中相同路径下复制service接口类与ProductRank类。
如图所示。
10.创建consumer.xml配置文件,引用服务。
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd"> <context:component-scan base-package="cn.itcast.core.alipay"></context:component-scan> <dubbo:application name="babasport"></dubbo:application> <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry> <!-- timeout="0" 默认是1000ms--> <!-- retries="":重试次数,不包含第一次调用,0代表不重试--> <!-- 幂等(设置重试次数)【查询、删除、修改】、非幂等(不能设置重试次数)【新增】 --> <dubbo:reference interface="cn.itcast.core.alipay.AlipayService" id="alipayService" timeout="300000000" retries="3" generic="false" check="false"> </dubbo:reference> <dubbo:reference interface="cn.itcast.core.service.ProductRankService" id="productRankService" generic="fasle" check="false"> </dubbo:reference> </beans>
11.使用注解注入,并调用,向redis中插入数据。
使用redis-manager软件,查看向redis中添加的数据,如图。
12.调用redis中的数据并显示在页面上。
在jsp页面上显示:
结果如下图。
参考:博客地址