pom.xml
<!-- 配置注释处理器 @ConfigurationProperties yml使用list@Value获取会报错-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
application.yml
pass-uri:
not-check:
- hello
- getVCode
- checkUserId
- front
- login
- fileUpload
- fileDelete
- roundRobin
NotCheckConfig.java
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.List;
@Data
@Configuration
@ConfigurationProperties(prefix = "pass-uri") // yml配置文件的前缀
public class NotCheckConfig {
private List<String> notCheck;//yml list变量名(建议和yml配置的名称一致)
}
轮询调用
RoundRobinController.java
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class RoundRobinController {
@Resource
private NotCheckConfig notCheckConfig;
@Resource
private RedisTemplate<String, Integer> redisTemplate;
/**
* 轮询获取yml配置的list
* @return
*/
@GetMapping("roundRobin")
public String roundRobin() {
String INDEX = "index";
if (!redisTemplate.hasKey(INDEX)) redisTemplate.opsForValue().set(INDEX, 0);//如果key不存在就创建key,并设置下标初始值为0
int index = redisTemplate.opsForValue().get(INDEX);//有下标则直接获取
List<String> list = notCheckConfig.getNotCheck();//获取yml配置的集合
if (index >= list.size()) index = 0;//超过list集合的值就重新赋值(轮询)
String ip = list.get(index);//获取集合中第index个元素的值,每次调用获取list中的第index条数据
redisTemplate.opsForValue().set(INDEX, ++index);//利用redis单线程的特性存放全局index下标
return ip;
}
}
每访问一次获取其中的一条数据