SpringBoot ES

查询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索引

 

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值