1.添加redis的配置
1.1 添加pom文件
一般是在业务层添加redis,初始情况就先进行了redis的引入。当然我们更理想的方式是把redis也做一个独立的服务挂在nacos上,这样我们什么时候需要用到redis,则只需要引入redis-api就可以通过接口调用了
<!--Redis依赖-->
<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>
1.2 yml增加配置
redis:
host: localhost
port: 6379
password: 123456
database: 0
lettuce:
pool:
max-active: 8 #最大连接数
max-idle: 8 #最大空闲连接
min-idle: 0 #最小空闲连接
max-wait: 100 #连接等待时间
1.3 示例
简单的使用redis的模版方法了
在学习黑马的redis课程中,老师推荐的是使用StringRedisTemplate,其实就是RedisTemplate的子类。不过这个的方法都是key,value都是string类型的。所以一般我们存储引用类型的数据需要自己转json,使用的时候再取出来转成对象
@RestController
@RequestMapping("/account")
public class AccountController {
Logger logger = LoggerFactory.getLogger(AccountController.class);
@Autowired
private StringRedisTemplate redisTemplate;
/*
根据手机号获取验证码
*/
@GetMapping("/getSMSCode")
public String getSMSCode(String mobile){
//todo 生成四位数验证码,然后供登录使用,需要存一份数据到redis.这样完成数据验证。
AccountDTO accountDTO = new AccountDTO();
accountDTO.setAge(22);
accountDTO.setName("zhangsan");
redisTemplate.opsForValue().set("account", JSON.toJSONString(accountDTO));
String account = redisTemplate.opsForValue().get("account");
logger.info("======>{}",account);
return "";
}
}
2.增加gateway服务
2.1 创建服务
参考我们创建module的过程,创建一个gateway服务即可
2.2 pom文件需要添加gateway依赖
这里需要排除一下web依赖,因为这个gateway的本质是路由转发,不是一个web服务
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>3.1.8</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
2.3 gateway服务的applicaiton.yml
server:
port: 9033
servlet:
context-path: /${spring.application.name}
spring:
application:
name: gateway
cloud:
nacos:
discovery:
server-addr: localhost:8848
namespace: c0753621-75f3-420c-bbf8-19c7493ed955
cluster-name: zs
gateway:
# 路由数组:指当请求满足什么样的断言时,转发到哪个服务上
routes:
# 路由标识,要求唯一,名称任意
- id: wanxinp2p-account
#lb://account #相当于localhost:30032/account
# uri: http://localhost:30032
uri: lb://account
# 设置断言,就是判断路由是否满足这里面的条件,满足就转发到这个路由地址,
predicates:
- Path=/gateway/account/**
# 去掉一个前缀,就是 /gateway
filters:
- StripPrefix=1
- id:
uri: http://localhost:30031
predicates:
- Path=/gateway/consumer/**
filters:
- StripPrefix=1
这里面有几个地方需要说一下:
2.3.1.servlet.context-path 就是你访问网关的的端口后面的一个路径 ,eg:localhost:9033/gateway
2.3.2因为我的顶层父接口里面有了nacos-discovery 的依赖了,所以我这个服务直接使用
2.3.3.routers 是一个数组 ,
id 唯一值就可以了。
uri:http://localhost:30031 。 我们把gateway 放到了nacos,我们肯定是想让gateway从nacos拉取服务,那么我们就可以写成 uri:lb://account 其中account就是你的服务注册在naocs的服务名
predicates: -Path 就是你访问网关的时候,uri匹配到你Path这个,则网关则会转发到这个服务
filters: stripprefix=1 eg: http://localhost:9033/gateway/account/account/getSMSCode 我们访问的时候,相当于把/gateway 去掉,访问就变成了
http://localhost:30032/account/account/getSMSCode
2.4 添加全局路由拦截
这个就是我们自定义的拦截器。然后gateway有好多内置的。不过我还没有学习,就先看自定义
@Component
public class LogGatewayFilter implements GlobalFilter, Ordered {
Logger logger = LoggerFactory.getLogger(LogGatewayFilter.class);
@Override
public int getOrder() {
// 数值越小,越先执行
return Integer.MIN_VALUE;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
HttpMethod method = request.getMethod();
String uri = request.getPath().pathWithinApplication().value();
logger.info("===访问方法==method=>{}==访问uri=>{}",method,uri);
// return chain.filter(exchange);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
ServerHttpResponse response = exchange.getResponse();
HttpStatus statusCode = response.getStatusCode();
logger.info("response==>{}",statusCode);
}));
}
}
2.4.1 需要被spring管理,添加component 注解
2.4.2 拦截器执行
其实就是这个拦截器会服务前后分别执行一部分,如果调用之后没有其他需求,这可以用注释掉的那部分代码。可以在两个logger加断电,和调用的服务account上加个端点,就可以看到效果了