查询ip ip信息存在ES mysql redis 各一份
用apifox里的高德地图api 先去高德地图开发者申请key 申请时选择web服务 查看ip定位文档运行
搜索界面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script>
$(function(){
})
//https://restapi.amap.com/v3/ip?key=2ce75851b86a98730224bfae5cee6cfd&ip=http%3A%2F%2F60.205.188.112%2F
function search(){
var ip=$("#ipid").val();
$.ajax({
url:"https://restapi.amap.com/v3/ip?key=2ce75851b86a98730224bfae5cee6cfd&ip="+ip,
type:"get",
success:function(res){
console.log(res)
res.ip=ip;
if(res.status==1){//查询成功
$.ajax({
url:"/getData",
data:res,
type:"post",
success:function(xx){
console.log(xx)
}
})
}
}
})
}
</script>
</head>
<body>
<input name="ip" placeholder="请输入ip" id="ipid"><button onclick="search()">查询</button>
</body>
</html>
搜索的dao
@Controller
public class SearchController {
@Autowired
private AddService addService;
@RequestMapping("/search")
public String search(){
return "searchAdd";
}
@RequestMapping("/getData")
public String getData(AddSource addSource){
System.out.println(addSource);
addService.saveAdd(addSource);
return "ok";
}
}
AddService
@Service
public class AddServiceImpl implements AddService {
@Autowired
private AddDao addDao;
//redis es mysql
public void saveAdd(AddSource addSource){
//存mysql
Integer i = addDao.add(addSource);
//存es
EsClient.esSave(addSource);
//存redis
JedisAddr.redisSave(addSource);
}
}
ES工具类
public class EsClient {//es工具类
public static RestHighLevelClient getClient(){
HttpHost httpHost = new HttpHost("192.168.59.12", 9200);
RestClientBuilder builder = RestClient.builder(httpHost);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
return restHighLevelClient;
}
public static void esSave(AddSource addSource){
ObjectMapper mapper = new ObjectMapper();
try {
IndexRequest indexRequest = new IndexRequest("addr", "addr", addSource.getId().toString()).source(mapper.writeValueAsString(addSource), XContentType.JSON);
RestHighLevelClient client = getClient();
client.index(indexRequest,RequestOptions.DEFAULT);
} catch (JsonProcessingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
jedis工具类
public class JedisAddr {
public static void redisSave(AddSource addSource){
Jedis jedis = new Jedis("192.168.59.12", 6379);
jedis.auth("123");
ObjectMapper mapper = new ObjectMapper();
Map<String, String> map = new HashMap<>();
map.put("id",addSource.getId().toString());
map.put("province",addSource.getProvince());
map.put("city",addSource.getCity());
map.put("rectangle",addSource.getRectangle());
jedis.hset("ip:"+addSource.getId().toString(),map);
//ip:10.70.60.11
//ip:119.11.50.60
// --get--> key ---> ip:*
}
}
mysql mapper文件 springboot链接数据库在配置文件里配置了就
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qianfeng.day1124.dao.AddDao">
<insert id="add" parameterType="AddSource">
<selectKey resultType="integer" keyProperty="id" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into addr (id,province,city,ip,rectangle) values (#{id},#{province},#{city},#{ip},#{rectangle})
</insert>
</mapper>
实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddSource implements Serializable {
private Integer id;
private String province;
private String city;
private String ip;
private String rectangle;
}
创建addr索引