首页 文章 精选 留言 我的

精选列表

搜索[整合],共10000篇文章
优秀的个人博客,低调大师

Spring Cloud 2.x系列之springcloud整合lettuce使用redis

Redis是一种nosql数据库,以键值对<key,value>的形式存储数据,其速度相比于MySQL之类的数据库,相当于内存读写与硬盘读写的差别,所以常常用作缓存,用于少写多读的场景下,直接从缓存拿数据比从数据库(数据库要I/O操作)拿要快得多。Redis目前几乎无处不在,大公司小公司都在用。 Spring cloud 2.x版本后默认Redis客户端连接池类型使用的是lettuce,而Sping cloud 1.5.x使用的jedis。Lettuce和Jedis的都是连接Redis Server的客户端程序。Jedis在实现上是直连redis server,多线程环境下非线程安全,除非使用连接池,为每个Jedis实例增加物理连接;Lettuce基于Netty的连接实例(StatefulRedisConnection),可以在多个线程间并发访问,且线程安全,满足多线程环境下的并发访问,同时它是可伸缩的设计,一个连接实例不够的情况也可以按需增加连接实例。 1、新建项目sc-redis,对应的pom.xml文件如下 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring-cloud</groupId> <artifactId>sc-redis</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>sc-redis</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.4.RELEASE</version> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> </project> 2、新建spring boot启动类 package sc.redis; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class RedisApplication { public static void main(String[] args) { SpringApplication.run(RedisApplication.class,args); } } 3、新建配置文件application.yml server: port: 9004 spring: application: name: sc-redis redis: host: 127.0.0.1 password: port: 6379 timeout: 10000 # 连接超时时间(毫秒) database: 0 # Redis默认情况下有16个分片,这里配置具体使用的分片,默认是0 lettuce: pool: max-active: 8 # 连接池最大连接数(使用负值表示没有限制)默认 8 max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)默认 -1 max-idle: 8 # 连接池中的最大空闲连接默认 8 min-idle: 0 # 连接池中的最小空闲连接默认 0 备注:reids对用的所有配置项可以在以下类查看 org.springframework.boot.autoconfigure.data.redis.RedisProperties 4、自定义Reids的Template 默认情况下的模板只能支持RedisTemplate<String, String>,也就是只能存入字符串,这在开发中是不友好的,所以自定义模板是很有必要的,当自定义了模板又想使用String存储这时候就可以使用StringRedisTemplate的方式,它们之间并不冲突。 package sc.redis.config; import java.io.Serializable; import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @AutoConfigureAfter(RedisAutoConfiguration.class) public class RedisCacheAutoConfiguration { @Bean public RedisTemplate<String, Serializable> redisCacheTemplate(LettuceConnectionFactoryredisConnectionFactory) { RedisTemplate<String, Serializable> template = newRedisTemplate<>(); //键的序列化方式 template.setKeySerializer(newStringRedisSerializer()); //值的序列化方式 template.setValueSerializer(newGenericJackson2JsonRedisSerializer()); template.setConnectionFactory(redisConnectionFactory); returntemplate; } } 5、新建一个模拟的Controller类 packagesc.redis.config; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import sc.redis.model.User; @RestController publicclassUserCacheController { //@Autowired //private StringRedisTemplate stringRedisTemplate; @Autowired privateRedisTemplate<String, Serializable> redisCacheTemplate; @GetMapping(value = "/cache/user/cacheUser") @ResponseBody public Map<String, Object> cacheUser() { Map<String,Object> result = new HashMap<String, Object>(); result.put("code", "000000"); result.put("msg", "success"); User u = new User(); u.setId(1L); u.setAge(23); u.setUserName("huangjinjin"); u.setPosition("cto"); result.put("body", u); redisCacheTemplate.opsForValue().set(String.valueOf(u.getId()), u); returnresult; } /** * 获取缓存信息 * @param id * @return */ @GetMapping(value = "/cache/user/getCacheUser/{id}") @ResponseBody public Map<String, Object> getCacheUser(@PathVariable Long id) { Map<String,Object> result = new HashMap<String, Object>(); result.put("code", "000000"); result.put("msg", "success"); User u = (User) redisCacheTemplate.opsForValue().get(String.valueOf(1)); System.out.println(u.getUserName()); result.put("body", u); returnresult; } } 6、启动sc-redis项目,并验证是否启动成功 7、使用postman访问接口 (1)存储值到redis http://127.0.0.1:9004/cache/user/cacheUser (2)从redis获取存贮的值 http://127.0.0.1:9004/cache/user/getCacheUser/1 使用redis客户端redis-cli查看是否把相关数据存贮到redis 下列的就是Redis其它类型所对应的操作方式: opsForValue:对应 String(字符串) opsForZSet:对应 ZSet(有序集合) opsForHash:对应 Hash(哈希) opsForList:对应 List(列表) opsForSet:对应 Set(集合) opsForGeo:对应 GEO(地理位置) 源码: https://gitee.com/hjj520/spring-cloud-2.x 原文发布时间:2018-9-24 本文作者:java乐园 本文来自云栖社区合作伙伴“java乐园”,了解相关信息可以关注“java乐园”

优秀的个人博客,低调大师

(十六) 整合spring cloud云架构 -使用spring cloud Bus刷新配置

我们使用spring cloud分布式微服务云架构做了b2b2c的电子商务系统,除了架构本身自带的系统服务外,我们将b2b2c的业务服务进行了细粒度拆分,做成了不同的业务微服务。 当我们的业务系统越来越庞大复杂的时候,各种配置也会随之增多。配置文件只要一修改,会对commonservice-config配置中心先停止服务,然后再重新启动,最后使配置生效。 如果服务少,我们可以手动方式来启动,但是对业务和系统的稳定性肯定有一定的影响。 如果是成百上千的服务都靠手动操作,我估计运维人员或技术人员会疯掉的。 针对以上问题,commonservice-config服务端和业务微服务分别做了相关的配置,服务端负责将git(svn或本地文件系统)中存储的配置文件进行配置化(我们使用的是本地配置方案,方便直接将配置文件更新到linux上), 业务微服务通过配置从服务端配置中心获取相关配置,如果配置文件变动了,通过刷新业务微服务的方式,将最新的配置信息获取。 spring cloud Bus通过一个轻量级消息代理连接分布式系统的节点。这可以用于广播状态更改(如配置更改)或其他管理指令。 接下来,我们就来实施通过spring cloud Bus方案,动态刷新服务端配置,具体步骤如下: 业务微服务配置(以honghu-member-servcie会员服务为例): pom文件配置: <span style="font-size: 16px;"> <dependency> <groupId>org.springframework.boot</groupId> <artifactId><span style="font-size: 16px;">spring-boot-starter-actuator</span></artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId><span style="font-size: 16px;">spring-cloud-starter-bus-amqp</span></artifactId> </dependency></span> yml文件配置: <span style="font-size: 16px;">server: port: 5012 spring: application: name: honghu-member-client profiles: active: dev,discoveryClient cloud: config: discovery: enabled: true service-id: commonservice-config-server <span style="color: #ff0000;"><strong>name: honghu-member profile: dev bus: trace: enabled: true #开启消息跟踪 </strong> <strong>rabbitmq: host: 192.168.1.254 port: 5672 username: honghu password: honghu</strong> </span> eureka: client: serviceUrl: defaultZone: http://honghu:123456@localhost:8761/eureka/ instance: prefer-ip-address: true logging: level: root: INFO org.springframework.security: INFO management: security: enabled: false security: basic: enabled: false</span> 查看注册中心,commonservice-config、honghu-member-service服务是否已经注册成功 访问一下profile,获取profile对应的配置信息(原配置): 访问http://localhost:7071/profile ==》 访问结果:123456 修改config配置中心的配置文件,将profile=123456修改为honghu123456 再次访问http://localhost:7071/profile ==》 访问结果:123456 使用spring cloud bus 刷新方案(使用post man测试工具进行测试) http://localhost:7071/bus/refresh 再次访问http://localhost:7071/profile ==》 访问结果:honghu123456 到此,整个commonservice-config配置中心动态刷新方案整理完毕! 欢迎大家和我一起学习spring cloud构建微服务云架构,我这边会将近期研发的spring cloud微服务云架构的搭建过程和精髓记录下来,帮助更多有兴趣研发spring cloud框架的朋友,大家来一起探讨spring cloud架构的搭建过程及如何运用于企业项目。 完整项目的源码来源 技术支持1791743380

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码