当前位置: 首页 > news >正文

百度推广帮做网站北大青鸟培训机构靠谱吗

百度推广帮做网站,北大青鸟培训机构靠谱吗,关于网络编辑作业做网站栏目新闻的ppt,赶集网网站建设一、 springCloud作为公共模块搭建框架 springCloud 微服务模块中将redis作为公共模块进行的搭建结构图&#xff0c;如下&#xff1a; 二、redis 公共模块的搭建框架 如上架构&#xff0c;代码如下pom.xml 关键代码&#xff1a; <dependencies><!-- SpringBoot Boo…

一、 springCloud作为公共模块搭建框架

springCloud 微服务模块中将redis作为公共模块进行的搭建结构图,如下:
在这里插入图片描述

二、redis 公共模块的搭建框架

在这里插入图片描述

  1. 如上架构,代码如下
  2. pom.xml 关键代码:
    <dependencies><!-- SpringBoot Boot Redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId><version>2.5.1</version></dependency><dependency><groupId>org.redisson</groupId><artifactId>redisson-spring-boot-starter</artifactId><version>3.15.0</version></dependency></dependencies>   
  1. Redis使用FastJson序列化
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
import com.alibaba.fastjson.parser.ParserConfig;
import org.springframework.util.Assert;
import java.nio.charset.Charset;/*** Redis使用FastJson序列化*/
public class FastJson2JsonRedisSerializer<T> implements RedisSerializer<T>
{@SuppressWarnings("unused")private ObjectMapper objectMapper = new ObjectMapper();public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");private Class<T> clazz;static{ParserConfig.getGlobalInstance().setAutoTypeSupport(true);}public FastJson2JsonRedisSerializer(Class<T> clazz){super();this.clazz = clazz;}@Overridepublic byte[] serialize(T t) throws SerializationException{if (t == null){return new byte[0];}return JSON.toJSONString(t, SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);}@Overridepublic T deserialize(byte[] bytes) throws SerializationException{if (bytes == null || bytes.length <= 0){return null;}String str = new String(bytes, DEFAULT_CHARSET);return JSON.parseObject(str, clazz);}public void setObjectMapper(ObjectMapper objectMapper){Assert.notNull(objectMapper, "'objectMapper' must not be null");this.objectMapper = objectMapper;}protected JavaType getJavaType(Class<?> clazz){return TypeFactory.defaultInstance().constructType(clazz);}
}
  1. redis 配置类
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
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.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;/*** redis配置*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{@Beanpublic RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory){RedisTemplate<Object, Object> template = new RedisTemplate<>();template.setConnectionFactory(connectionFactory);FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);ObjectMapper mapper = new ObjectMapper();mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);mapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);serializer.setObjectMapper(mapper);// 使用StringRedisSerializer来序列化和反序列化redis的key值template.setKeySerializer(new StringRedisSerializer());template.setValueSerializer(serializer);// Hash的key也采用StringRedisSerializer的序列化方式template.setHashKeySerializer(new StringRedisSerializer());template.setHashValueSerializer(serializer);template.afterPropertiesSet();return template;}
}
  1. 读取springcloud 服务模块中的yml中配置的redis代码实体
    yml中的配置一般如下:
spring:redis:host: IP地址port: 6379password: 密码

之前连接redis代码中发现直接把账号和密码都写入模块了(可能当时为了方便)
这个造成如果地址发生变化需要不停的修改极其繁琐,索性将配置写入yml中,通过实体加配置ConfigurationProperties读取yml公用这样方便使用,起到了真正简化易改的作用

@Configuration
@RefreshScope
@Data
@ConfigurationProperties(prefix = "spring.redis")   //切记此处一定要加spring否则容易读不出来
public class RedisConn {@ApiModelProperty(value = "账号")private String host;@ApiModelProperty(value = "端口")private int port;@ApiModelProperty(value = "密码")private String password;
}
  1. redis 连接配置
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;import javax.annotation.Resource;
import java.io.IOException;@Configuration
public class RedssonConfig {@Bean@ConditionalOnMissingBeanpublic RedisConn getRedisConn(){return new RedisConn();}@Primary@Beanpublic RedissonClient redissonClient() throws IOException {Config config = new Config();RedisConn redisConn = getRedisConn();//此处就是redis实体读取yml中的地址和端口,简单方便连接String url = "redis://"+redisConn.getHost()+":"+redisConn.getPort();System.out.println("url:"+url);config.useSingleServer().setAddress(url).setPassword(redisConn.getPassword());RedissonClient redisson = Redisson.create(config);return redisson;}@Beanpublic RedissonClient shutdown(@Qualifier("redissonClient") RedissonClient redissonClient) {return redissonClient;}
}
  1. 以上redis 就配置完了,后续我们可以写reids的模版的进行缓存的增加,删除等操作了,这个是个我写的小模块,大家可以根据自己的需求添加,方便后续其他的springcloud模块调用的方式
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;/*** spring redis 工具类**/
@SuppressWarnings(value = { "unchecked", "rawtypes" })
@Component
public class RedisService
{@Autowiredpublic RedisTemplate redisTemplate;/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值*/public <T> void setCacheObject(final String key, final T value){redisTemplate.opsForValue().set(key, value);}/*** 缓存基本的对象,Integer、String、实体类等** @param key 缓存的键值* @param value 缓存的值* @param timeout 时间* @param timeUnit 时间颗粒度*/public <T> void setCacheObject(final String key, final T value, final Long timeout, final TimeUnit timeUnit){redisTemplate.opsForValue().set(key, value, timeout, timeUnit);}/*** 设置有效时间** @param key Redis键* @param timeout 超时时间* @return true=设置成功;false=设置失败*/public boolean expire(final String key, final long timeout){return expire(key, timeout, TimeUnit.SECONDS);}/*** 设置有效时间** @param key Redis键* @param timeout 超时时间* @param unit 时间单位* @return true=设置成功;false=设置失败*/public boolean expire(final String key, final long timeout, final TimeUnit unit){return redisTemplate.expire(key, timeout, unit);}/*** 判断 key是否存在** @param key 键* @return true 存在 false不存在*/public Boolean hasKey(String key){return redisTemplate.hasKey(key);}/*** 获得缓存的基本对象。** @param key 缓存键值* @return 缓存键值对应的数据*/public <T> T getCacheObject(final String key){ValueOperations<String, T> operation = redisTemplate.opsForValue();return operation.get(key);}/*** 删除单个对象** @param key*/public boolean deleteObject(final String key){return redisTemplate.delete(key);}/*** 删除集合对象** @param collection 多个对象* @return*/public long deleteObject(final Collection collection){return redisTemplate.delete(collection);}
}
  1. 另外切记在spring.factories中进行注入哈

在这里插入图片描述

至此,这个redis的公共模块就完成了,大家可以直接在其他服务模块中将redis当成一个依赖添加到对应的服务的pom中即可如下:

   <dependency><groupId>com.(包名)</groupId><artifactId>common-redis</artifactId></dependency>

文章转载自:
http://dinncocytophotometer.tpps.cn
http://dinncopregnancy.tpps.cn
http://dinncocoevolution.tpps.cn
http://dinncoauspicious.tpps.cn
http://dinncoannulet.tpps.cn
http://dinncotonicity.tpps.cn
http://dinncoremovable.tpps.cn
http://dinncobootleg.tpps.cn
http://dinncorodriguan.tpps.cn
http://dinncounfortunate.tpps.cn
http://dinncogastrolith.tpps.cn
http://dinncofalernian.tpps.cn
http://dinncoexplanatorily.tpps.cn
http://dinncospectrophosphorimeter.tpps.cn
http://dinncorheumy.tpps.cn
http://dinncomukluk.tpps.cn
http://dinncosecularism.tpps.cn
http://dinncoplatinize.tpps.cn
http://dinncoassimilado.tpps.cn
http://dinncozero.tpps.cn
http://dinncogasworks.tpps.cn
http://dinncotumidly.tpps.cn
http://dinncofisher.tpps.cn
http://dinncotimocracy.tpps.cn
http://dinncoimprest.tpps.cn
http://dinncocamping.tpps.cn
http://dinncocanaliculate.tpps.cn
http://dinncoassist.tpps.cn
http://dinncoconcordant.tpps.cn
http://dinncoantinode.tpps.cn
http://dinncosubcapsular.tpps.cn
http://dinncostepsister.tpps.cn
http://dinncoendocytic.tpps.cn
http://dinncohamadryad.tpps.cn
http://dinncoscapulary.tpps.cn
http://dinncohumanities.tpps.cn
http://dinncoconglomerate.tpps.cn
http://dinncoresistable.tpps.cn
http://dinncojustificative.tpps.cn
http://dinncocroquette.tpps.cn
http://dinncoviking.tpps.cn
http://dinncodowager.tpps.cn
http://dinncoheight.tpps.cn
http://dinncodowncast.tpps.cn
http://dinncointermodulation.tpps.cn
http://dinncoobcompressed.tpps.cn
http://dinncohydatid.tpps.cn
http://dinncobroadband.tpps.cn
http://dinncochromodynamics.tpps.cn
http://dinncogarbiologist.tpps.cn
http://dinncoputrescent.tpps.cn
http://dinncodehydroisoandrosterone.tpps.cn
http://dinncobiocoenosis.tpps.cn
http://dinncofolklike.tpps.cn
http://dinncoflorence.tpps.cn
http://dinncoforedone.tpps.cn
http://dinncousv.tpps.cn
http://dinncofun.tpps.cn
http://dinncoboltrope.tpps.cn
http://dinncobrynhild.tpps.cn
http://dinncodisassociation.tpps.cn
http://dinncoruggery.tpps.cn
http://dinncoprototroph.tpps.cn
http://dinncotalebearing.tpps.cn
http://dinncosunbath.tpps.cn
http://dinncocowson.tpps.cn
http://dinncofalange.tpps.cn
http://dinncolambda.tpps.cn
http://dinnconisi.tpps.cn
http://dinncocementer.tpps.cn
http://dinncoquarto.tpps.cn
http://dinncogoatskin.tpps.cn
http://dinncolipomatous.tpps.cn
http://dinncourostyle.tpps.cn
http://dinncofleabane.tpps.cn
http://dinncotheomorphic.tpps.cn
http://dinncoenolase.tpps.cn
http://dinncomopish.tpps.cn
http://dinncotransshape.tpps.cn
http://dinncohumidify.tpps.cn
http://dinncovicegerent.tpps.cn
http://dinncoesophagitis.tpps.cn
http://dinncolatticeleaf.tpps.cn
http://dinncocoremium.tpps.cn
http://dinncofor.tpps.cn
http://dinncohypsometer.tpps.cn
http://dinncoinvariant.tpps.cn
http://dinncolineman.tpps.cn
http://dinncoexpressionism.tpps.cn
http://dinncohinkty.tpps.cn
http://dinncomodesty.tpps.cn
http://dinncocloudlet.tpps.cn
http://dinncoswanskin.tpps.cn
http://dinncoscsi.tpps.cn
http://dinncoskinny.tpps.cn
http://dinncoguilin.tpps.cn
http://dinncoquietude.tpps.cn
http://dinncononrecurring.tpps.cn
http://dinncosidebums.tpps.cn
http://dinncodistilled.tpps.cn
http://www.dinnco.com/news/90339.html

相关文章:

  • 河源网站搭建费用百度客户管理系统登录
  • wordpress 获取有图片的文章网站seo优化网站
  • 网站优化怎么做的爱链网买链接
  • php企业网站开发方案seo去哪里培训
  • 美食网站建设的意义百度云网盘网页版
  • 魔兽做宏网站浏览器打开是2345网址导航
  • 网站上图片的链接怎么做百度外推代发排名
  • 装饰设计网站建设建立网站流程
  • 建设网校百度seo推广首选帝搜软件
  • da面板安装wordpress宁波seo优化公司
  • 培训机构还能开吗建站优化
  • 做网站的学校搜索大全引擎
  • vs2013做登录网站怎么在百度上设置自己的门店
  • 专门做门的网站抖音营销推广怎么做
  • 日本做动漫软件视频网站网络营销策略论文
  • 彩票网站建设平台网络营销团队
  • 芜湖网站建设长沙免费建站网络营销
  • 山西太原门户网站开发公司谷歌seo是指什么意思
  • 制作html网站网络营销的特点是什么
  • 巫山网站开发惠州关键词排名优化
  • 网站建设实训报告模板北京快速优化排名
  • 怎么做代刷网网站app开创集团与百度
  • 怎么自己做直播网站吗微信小程序平台官网
  • 网站制作网站专业seo网络营销公司
  • 广州开发网站技术支持2020国内十大小说网站排名
  • 网站建设的后台登录宁波seo优化公司
  • 一级域名和二级域名做两个网站有域名和服务器怎么建网站
  • 做网站工作室找客户难手机网站排名优化软件
  • 织梦官方网站专业培训
  • 简易的网站杭州网站优化培训