接口调用、客户端lb、嵌套JSON解析、慎用 foreach、新建类封装

接口调用出错

  1. 故障转移:重试选择其他可用节点,做好幂等性可用!!!
  2. 快速失败

推荐客户端负载均衡

服务端 负载均衡
在这里插入图片描述

客户端 负载均衡
在这里插入图片描述
抽象为现在 的微服务架构
在这里插入图片描述

多层JSON嵌套

根据 不同的情况,数据的结构会发生变化,定义各种各样的实体类不太好!!因为实体类太多不方便维护

<dependency>
    <groupId>ognl</groupId>
    <artifactId>ognl</artifactId>
    <version>3.1.1</version>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.13.2</version>
</dependency>

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10</version>
</dependency>

JsonUtils 工具类

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import ognl.Ognl;
import ognl.OgnlContext;

import java.util.Map;

public class JsonUtils {
    /**
     * 将指定JSON转为Map对象,Key固定为String,对应JSONkey
     * Value分情况:
     * 1. Value是字符串,自动转为字符串,例如:{"a","b"}
     * 2. Value是其他JSON对象,自动转为Map,例如:{"a":{"b":"2"}}}
     * 3. Value是数组,自动转为List<Map>,例如:{"a":[{"b":"2"},"c":"3"]}
     *
     * @param json 输入的JSON对象
     * @return 动态的Map集合
     */
    public static Map<String, Object> transferToMap(String json) {
        Gson gson = new Gson ();
        Map<String, Object> map = gson.fromJson (json,
                new TypeToken<Map<String, Object>> () {
                }.getType ());
        return map;
    }

    /**
     * 简化方法
     *
     * @param json  原始的JSON数据
     * @param path  OGNL规则表达式
     * @param clazz Value对应的目标类
     * @return clazz对应数据
     */
    public static <T> T getValue(String json, String path, Class<T> clazz) {
        try {
            Map<String, Object> map = transferToMap (json);
            OgnlContext context = new OgnlContext ();
            context.setRoot (map);
            T value = (T) Ognl.getValue (path, context, context.getRoot ());
            return value;
        } catch (Exception e) {
            throw new RuntimeException (e);
        }
    }

}

测试


import org.junit.Test;

import java.util.List;
import java.util.Map;

public class JsonCase {
    /**
     * 超多层级JSON嵌套的快速提取
     */

    @Test

    public void case0() {
        //language=JSON,获取 嵌套json中 e的值
        String text = "{\n" +
                "  \"a\": {\n" +
                "    \"b\": {\n" +
                "      \"c\": {\n" +
                "        \"d\": {\n" +
                "          \"e\": \"nothing\"\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        Map<String, Object> jsonMap = JsonUtils.transferToMap (json);

        String e = JsonUtils.getValue (text, "a.b.c.d.e", String.class);
        System.out.println (e);

    }


    //language=JSON
    private String json = "{\n" +
            "  \"showapi_res_error\": \"无\",\n" +
            "  \"showapi_res_id\": \"628cc9850de3769f06edbb49\",\n" +
            "  \"showapi_res_code\": 0,\n" +
            "  \"showapi_fee_num\": 1,\n" +
            "  \"showapi_res_body\": {\n" +
            "    \"ret_code\": 0,\n" +
            "    \"area\": \"南安\",\n" +
            "    \"areaid\": \"101230506\",\n" +
            "    \"areaCode\": \"350583\",\n" +
            "    \"hourList\": [\n" +
            "      {\n" +
            "        \"weather_code\": \"06\",\n" +
            "        \"time\": \"202205242000\",\n" +
            "        \"area\": \"南安\",\n" +
            "        \"wind_direction\": \"东南 风\",\n" +
            "        \" wind_power\": \"0-3级 微风<5.4m/s\",\n" +
            "        \" weather\": \" 小 雨\",\n" +
            "        \" areaid\": \"101230506\",\n" +
            "        \" temperature\": \"20\"\n" +
            "      },\n" +
            "      {\n" +
            "        \"weather_code\": \"07\",\n" +
            "        \"time\": \"202205242100\",\n" +
            "        \"area\": \"南安\",\n" +
            "        \"wind_direction\": \"东 风\",\n" +
            "        \" wind_power\": \"0-3级 微风<5.4m/s\",\n" +
            "        \" weather\": \" 小 雨\",\n" +
            "        \" areaid\": \"101230506\",\n" +
            "        \" temperature\": \"20\"\n" +
            "      }\n" +
            "    ]\n" +
            "  }\n" +
            "}";

    //将JSON转为标准Map结构
    @Test
    public void case1() {
        Map<String, Object> jsonMap = JsonUtils.transferToMap (json);
        System.out.println (jsonMap);
    }

    /**
     * OGNL直接提取数据,Value为子JSON对象的情况
     */
    @Test
    public void case2() {
        Map<String, Object> jsonMap = JsonUtils.transferToMap (json);
        Map resBody = JsonUtils.getValue (json, "showapi_res_body", Map.class);
        System.out.println (resBody);
    }

    /**
     * OGNL直接提取数据,Value为标准字符串的情况
     */
    @Test
    public void case3() {
        String value = JsonUtils.getValue (json, "showapi_res_body.area", String.class);
        System.out.println (value);
    }

    /**
     * OGNL直接提取数据,Value为标准字符串的情况,Value为数组的情况
     */
    @Test
    public void case4() {
        List<Map> hourList = JsonUtils.getValue (json, "showapi_res_body.hourList",
                List.class);


        // 每一个集合对象都是List,遍历这个集合
        for (Map hour : hourList) {
            System.out.println (hour);
        }
    }

    /**
     * 利用List语法获取第1个时点天气预报
     */
    @Test
    public void case5() {
        String area = JsonUtils.getValue (json,
                "showapi_res_body.hourList[0].weather_code", String.class);

        System.out.println (area);
    }
}

大数据量 插入慎用foreach

@Resource
private UserService userService;

// saveBatch 批量插入
userService.saveBatch (null);

只有把rewriteBatchedStatements参数置为true,
驱动才会帮你批量执行SQL (jdbc:mysql://ip:port/db?rewriteBatchedStatements=true)

使用Api时新建一个类封装、方便维护

变化放在盒子里

也就是 使用一个类来 封装原有的方法,解耦!
起初:

String pass = "123456";
String md5 = DigestUtils.md5DigestAsHex (pass.getBytes ());

有 更好的方法时,不得不更新所有的api,麻烦

使用一个类封装 时:

import org.springframework.util.DigestUtils;

public class MyDigestUtils {

    public static String md5Digest(String source) {

        return DigestUtils.md5DigestAsHex (source.getBytes ());
    }
  
}

调用:

String pass = "123456";
String md5 = MyDigestUtils.md5Digest (pass);

发生变化时,直接修改MyDigestUtils下的md5Digest 方法即可!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值