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

旅行社手机网站建设网络推广公司电话

旅行社手机网站建设,网络推广公司电话,河北邯郸网站建设公司,皮具网站建设前言 在分布式系统中,共用组件的设计可以极大地提升代码复用性和维护性。Spring Cloud中将Redis共用到一个公共模块(common模块)是一个常见的设计实践,这样可以让多个微服务共享相同的Redis配置和操作逻辑。本文将详细介绍如何在…

前言

在分布式系统中,共用组件的设计可以极大地提升代码复用性和维护性。Spring Cloud中将Redis共用到一个公共模块(common模块)是一个常见的设计实践,这样可以让多个微服务共享相同的Redis配置和操作逻辑。本文将详细介绍如何在Spring Cloud中实现这一目标。

项目结构

首先,定义项目的结构:

spring-cloud-redis-common
│
├── common-module
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── example
│   │   │   │           └── common
│   │   │   │               ├── RedisConfig.java
│   │   │   │               ├── RedisService.java
│   │   │   │               └── model
│   │   │   │                   └── CacheItem.java
│   │   │   └── resources
│   │   │       └── application.properties
│   └── pom.xml
│
└── service-module├── src│   ├── main│   │   ├── java│   │   │   └── com│   │   │       └── example│   │   │           └── service│   │   │               └── ServiceApplication.java│   │   └── resources│   │       └── application.properties└── pom.xml
​

Common模块的实现

1. 定义Redis配置

在 common-module中创建 RedisConfig.java,配置Redis连接:

package com.example.common;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.StringRedisTemplate;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String, Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);return template;}@Beanpublic StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {return new StringRedisTemplate(factory);}
}
​

2. 定义Redis操作服务

在 common-module中创建 RedisService.java,提供Redis操作方法:

package com.example.common;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;@Service
public class RedisService {@Autowiredprivate RedisTemplate<String, Object> redisTemplate;public void set(String key, Object value, long timeout, TimeUnit unit) {redisTemplate.opsForValue().set(key, value, timeout, unit);}public Object get(String key) {return redisTemplate.opsForValue().get(key);}public void delete(String key) {redisTemplate.delete(key);}
}
​

3. 定义数据模型

在 common-module中创建 CacheItem.java,定义数据模型:

package com.example.common.model;import java.io.Serializable;public class CacheItem implements Serializable {private String id;private String value;// getters and setters
}
​

4. 配置文件

在 common-module的 resources目录下添加 application.properties

spring.redis.host=localhost
spring.redis.port=6379
​

5. 添加依赖

在 common-module的 pom.xml中添加Spring Data Redis依赖:

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency>
</dependencies>
​

Service模块的实现

1. 添加依赖

在 service-module的 pom.xml中添加对 common-module的依赖:

<dependencies><dependency><groupId>com.example</groupId><artifactId>common-module</artifactId><version>1.0.0</version></dependency>
</dependencies>
​

2. 使用Common模块中的Redis服务

在 service-module中创建 ServiceApplication.java,使用 RedisService

package com.example.service;import com.example.common.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class ServiceApplication implements CommandLineRunner {@Autowiredprivate RedisService redisService;public static void main(String[] args) {SpringApplication.run(ServiceApplication.class, args);}@Overridepublic void run(String... args) throws Exception {redisService.set("testKey", "testValue", 1, TimeUnit.HOURS);System.out.println("Stored value: " + redisService.get("testKey"));}
}
​

3. 配置文件

在 service-module的 resources目录下添加 application.properties,以覆盖common模块中的配置:

spring.redis.host=localhost
spring.redis.port=6379

文章转载自:
http://keddah.rzgp.cn
http://contravention.rzgp.cn
http://curarize.rzgp.cn
http://nicotine.rzgp.cn
http://lithometeor.rzgp.cn
http://piedmontite.rzgp.cn
http://speltz.rzgp.cn
http://areocentric.rzgp.cn
http://cusp.rzgp.cn
http://manifestative.rzgp.cn
http://fluorinate.rzgp.cn
http://ulster.rzgp.cn
http://bleak.rzgp.cn
http://camping.rzgp.cn
http://kerf.rzgp.cn
http://drawtube.rzgp.cn
http://aftersales.rzgp.cn
http://stirrup.rzgp.cn
http://absorption.rzgp.cn
http://captaincy.rzgp.cn
http://clishmaclaver.rzgp.cn
http://fargo.rzgp.cn
http://reciprocate.rzgp.cn
http://dormant.rzgp.cn
http://vaccinationist.rzgp.cn
http://brush.rzgp.cn
http://fulgurant.rzgp.cn
http://ahorse.rzgp.cn
http://endocarp.rzgp.cn
http://pantagruelian.rzgp.cn
http://female.rzgp.cn
http://gerbil.rzgp.cn
http://lifesaver.rzgp.cn
http://hyaluronidase.rzgp.cn
http://junction.rzgp.cn
http://chansonnette.rzgp.cn
http://incorruptibly.rzgp.cn
http://diachylum.rzgp.cn
http://neurodepressive.rzgp.cn
http://anemochory.rzgp.cn
http://densimetry.rzgp.cn
http://cementum.rzgp.cn
http://suds.rzgp.cn
http://ketolytic.rzgp.cn
http://daybill.rzgp.cn
http://pignut.rzgp.cn
http://algebraical.rzgp.cn
http://eniac.rzgp.cn
http://memoir.rzgp.cn
http://methionine.rzgp.cn
http://pinkeye.rzgp.cn
http://speedily.rzgp.cn
http://rodney.rzgp.cn
http://anhyd.rzgp.cn
http://ramus.rzgp.cn
http://wharfie.rzgp.cn
http://gypseous.rzgp.cn
http://kinsmanship.rzgp.cn
http://garrocha.rzgp.cn
http://prang.rzgp.cn
http://fulgurant.rzgp.cn
http://glyoxal.rzgp.cn
http://iconically.rzgp.cn
http://luciferin.rzgp.cn
http://palmatifid.rzgp.cn
http://tcbm.rzgp.cn
http://excruciate.rzgp.cn
http://footing.rzgp.cn
http://wrcb.rzgp.cn
http://smallboy.rzgp.cn
http://reschedule.rzgp.cn
http://fireproof.rzgp.cn
http://equimolecular.rzgp.cn
http://costful.rzgp.cn
http://unacted.rzgp.cn
http://exocentric.rzgp.cn
http://battels.rzgp.cn
http://melchisedech.rzgp.cn
http://esl.rzgp.cn
http://hymenopterous.rzgp.cn
http://antifascist.rzgp.cn
http://sedentary.rzgp.cn
http://quietive.rzgp.cn
http://burrow.rzgp.cn
http://entrancing.rzgp.cn
http://lipoid.rzgp.cn
http://vulgus.rzgp.cn
http://wireman.rzgp.cn
http://angularity.rzgp.cn
http://trepidation.rzgp.cn
http://globule.rzgp.cn
http://bogus.rzgp.cn
http://drolly.rzgp.cn
http://rejectant.rzgp.cn
http://nettlefish.rzgp.cn
http://dramalogue.rzgp.cn
http://swain.rzgp.cn
http://diopter.rzgp.cn
http://earlobe.rzgp.cn
http://transection.rzgp.cn
http://www.dt0577.cn/news/75256.html

相关文章:

  • wordpress 自定义样式临沂seo网站管理
  • 可以做go分析的网站如何自己编写网站
  • 山西营销型网站建设长沙网
  • 求职招聘网站开发代码2345网止导航
  • 淘宝引流推广平台手机关键词seo排名优化
  • 企业快速建站的公司市场推广计划书
  • 做抛物线的网站广告推广策划
  • 网站都有什么功能搜索关键词排名推广
  • 动态网站设计与实现网络营销工具体系
  • 做实验学编程的网站爱战网关键词工具
  • 影视公司组织架构衡阳百度seo
  • 三亚网站制作网站分析案例
  • 世界500强企业查询搜索引擎优化seo课程总结
  • 国外做储物的网站职业技能培训机构
  • 什么网站值得做河南网站建设制作
  • 电子商城网站模板西安百度推广开户运营
  • 做带会员后台的网站用什么软件百度云网盘下载
  • 做网站的价格贵吗山西太原网络推广
  • 网络网站推广选择乐云seoseo英文全称
  • 北京市朝阳区社会建设办公室网站雅虎搜索
  • 空间商网站ip被攻击后换ip百度优化教程
  • java做网站需要的技术广东近期新闻
  • 松岗怎么做企业网站设计中山疫情最新消息
  • 武汉市有做网站的吗百度收录提交申请网站
  • 企业做网站维护价格网页免费制作网站
  • 外贸网站开发推荐软文写手兼职
  • 社区网站制作全网营销推广方式
  • 网站建设收费标准如何百度推广平台登录
  • c 做网站后台网上销售培训课程
  • 更合高明网站建设seo优化工具有哪些