使用redis时需要cmd redis-server.exe redis.conf
1.创建Feign的接口和fallback的方法(写在一个common-parent的子模块common-interface中)
备注:因为我们创建的是公共的Feign,所以为了使用Feign不重复写,使用直接导入这个子模块的包,即可使用这个模块所有的方法,避免重复写,因为都是差不多一样的代码。
//COMMON-SERVICE在注册中心的名字,也就是下面的controller所在的模块,我们需要调用这个服务模块中的方法。 //因为这个接口是所有服务都可以用的公共模块,所以当使用Feign时,我们为了避免重写方法,让其他模块直接引用这个模块包 //也就实现了接收端的服务都写了需要调用的方法。(使用Feign,接收端需要打注解,并且重写方法) @FeignClient(value = "COMMON-SERVICE",fallbackFactory = RedisClientFallbackFactory.class )//服务提供者的名称 public interface RedisClient { //也可以@PostMapping("/redis") @RequestMapping(value = "/redis",method = RequestMethod.POST) AjaxResult set(@RequestParam("key")String key, @RequestParam("value")String value); @RequestMapping(value = "/redis",method = RequestMethod.GET) AjaxResult get(@RequestParam("key")String key); }
@Component //交给spring管理,实现一个FallbackFactory接口,然后覆写里面的方法 public class RedisClientFallbackFactory implements FallbackFactory<RedisClient>{ @Override public RedisClient create(Throwable throwable) { return new RedisClient() { @Override public AjaxResult set(String key, String value) { return AjaxResult.myAjaxResult().setSuccess(false).setMessage("失败"); } @Override public AjaxResult get(String key) { return AjaxResult.myAjaxResult().setSuccess(false).setMessage("失败"); } }; } }
2.(发送端,使用jedis工具类,在下面)
创建一个服务的controller(写在一个common-parent的子模块common-service中)
主方法注解@SpringBootApplication @EnableEurekaClient就可以了,Feign的注解都是打在接收端的
备注:如果文件太大的话,需要在这个服务的yml中添加 ,因为其他服务需要访问的是这个服务
server: max-http-header-size: 10240000 #配置中心在注册中心的名字
//因为其他模块使用Feign时,需要在自己的模块写一个接口,并且方法名和内容都一样。 //为了方便,我们直接实现那个上面那个公共接口就可以了,其他模块引入上面那个接口就可以使用 @RestController public class RedisController implements RedisClient { @PostMapping(value = "/redis") @Override //都是String,需要用@RequestParam注解,设置值时就是 key="productType"&value="字符串",相当于redis命令( set productType 字符串 ) public AjaxResult set(@RequestParam("key")String key, @RequestParam("value")String value) { try { System.out.println(value); RedisUtils.INSTANCE.set(key, value); return AjaxResult.myAjaxResult(); } catch (Exception e) { e.printStackTrace(); return AjaxResult.myAjaxResult().setSuccess(false).setMessage("失败"+e.getMessage()); } } @GetMapping(value = "/redis") @Override public AjaxResult get(@RequestParam("key")String key) { try { String jsonStr = RedisUtils.INSTANCE.get(key); return AjaxResult.myAjaxResult().setRestObj(jsonStr); } catch (Exception e) { e.printStackTrace(); return AjaxResult.myAjaxResult().setSuccess(false).setMessage("失败"+e.getMessage()); } } }
3.RedisUtils的工具类,直接拷贝即可,需要导入Jedis的包,可以直接操作redis
public enum RedisUtils { INSTANCE; static JedisPool jedisPool = null; static { //1 创建连接池配置对象 JedisPoolConfig config = new JedisPoolConfig(); //2 进行配置-四个配置 config.setMaxIdle(1);//最小连接数 config.setMaxTotal(11);//最大连接数 config.setMaxWaitMillis(10 * 1000L);//最长等待时间 config.setTestOnBorrow(true);//测试连接时是否畅通 //3 通过配置对象创建连接池对象 jedisPool = new JedisPool(config, "127.0.0.1", 6379, 10 * 1000, "123456"); } //获取连接 public Jedis getSource() { return jedisPool.getResource(); } //关闭资源 public void closeSource(Jedis jedis) { if (jedis != null) { jedis.close(); } } /** * 设置字符值 * * @param key * @param value */ public void set(String key, String value) { Jedis jedis = getSource(); jedis.set(key, value); closeSource(jedis); } /** * 设置字符值 * * @param key */ public String get(String key) { Jedis jedis = getSource(); try { return jedis.get(key); } catch (Exception e) { e.printStackTrace(); } finally { closeSource(jedis); } return null; } }
4.(接收端)
4.1 主方法注解
@SpringBootApplication//开启springboot @EnableEurekaClient//加入注册中心 @EnableSwagger2//开启Swagger2 @MapperScan("cn.itsource.ccgo.mapper")//使用mybatisplus时,扫描的那个包 @EnableFeignClients(basePackages = "cn.itsource.common")//开启Feign,因为导入了上面公共接口的包,所以将它的包名放入就可以了
4.2 service层,导入fastjson包
@Service public class ProductTypeServiceImpl extends ServiceImpl<ProductTypeMapper, ProductType> implements IProductTypeService { @Autowired//导入的公共接口,使用Feign调用远程服务的方法 private RedisClient redisClient; @Override public List<ProductType> getTypeTree() { try { AjaxResult productTypes = redisClient.get("productTypes");//先通过远程服务器的方法去查redis数据库,获得productTypes这个key的值,它的返回结果是AjaxResult String stringTypes = (String) productTypes.getRestObj();//获得对象,因为redis返回的都是String,所以可以强转 List<ProductType> types = JSON.parseArray(stringTypes, ProductType.class);//调用fastjson转为List //如果为空,则调用下面的方法返回集合,返回放入redis后的List //前面必须判读==null,前面为true则返回,不然报错 if (types==null||types.size()<=0) { types = getProductTypeTree();//这里这个方法是查询所有的类型,返回值是List类型的 String jsonString = JSON.toJSONString(types); //List<ProductType> productTypeTree = getProductTypeTree(); RedisUtils.INSTANCE.set("productTypes", jsonString); return types; } else { return types; } } catch (Exception e) { e.printStackTrace(); return null; } }
4.3 通过controller层调用service层就可以了