2017年9月

spring boot 常见错误

1:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
在test里面指定入口类,例如:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class DemoApplicationTests {

@Test
public void contextLoads() {
}

}

其中Application要导入进来

2:No qualifying bean of type 'org.springframework.scheduling.TaskScheduler' available

@Bean
public TaskScheduler taskScheduler() {
    return new ConcurrentTaskScheduler(); //single threaded by default
}

3:SQLErrorCodesFactory:126 - SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase]
一般是由于字段类型mapping错误或者字段缺失
4:Failed to deserialize payload. Is the byte array a result of corresponding serialization for DefaultDeserializer?; nested exception is java.io.EOFException
由于字段值序列化错误,key和hashkey都要设置

redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);

5:/swagger-ui.html] not found,springboot 1.x整合swagger的时候找不到文档页面
在WebConfiguration添加:

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

利用URLConnection编写简单的FCM推送代码

需要推送的body为:

{
  "to":"cp9rjOYxFZ8:APA91bF0Ao5mq_9XsYMtu2DPov2yIH9minFrPEMLFm4OQA3Yk6mkgEXA5opqHo9I15-q7cwzF_iuQu7nja7pDvivDN4KBXznUZPnWYSqwy3qJFWt7r94JXQ4WcOYy7rC1G7z1PzjZ611",
  "data":{
    "notificationType":"11",
    "userName":"Henry",
    "baseName":"cute animals",
    "baseCode":"ciika",
    "userId":"cikka"
  }
}

URLConnection代码为:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class PostJson {
    public static void send(String urlPath, String content) {
        try {
            URL url = new URL(urlPath);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");
            connection.setUseCaches(false);
            connection.setInstanceFollowRedirects(true);
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setRequestProperty("Authorization", "key=AIzaSyCYFxP9cRIt1Z3BRVE55022QcQH2ledcde");
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.writeBytes(content);
            out.flush();
            out.close();
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String lines;
            StringBuffer sb = new StringBuffer("");
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                sb.append(lines);
            }
            System.out.println(sb);
            reader.close();
            // 断开连接
            connection.disconnect();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public static void main(String[] args) {
        String body = "见上面示例";
        send("https://fcm.googleapis.com/fcm/send", body);
    }

}

返回为:

{
    "multicast_id": 4746322810404192896,
    "success": 1,
    "failure": 0,
    "canonical_ids": 0,
    "results": [
        {
            "message_id": "0:1506080066562889%a9dfa1c2f9fd7ecd"
        }
    ]
}

利用notepad++进行Mysql语句ON DUPLICATE KEY UPDATE同行替换

使用场景:
在就行数据迁移的时候,有些数据语句存在,在使用insert的时候会报重复的key,那么就要利用insert .. ON DUPLICATE KEY(例如:INSERT INTO TABLE (a,c) VALUES (1,3) ON DUPLICATE KEY UPDATE c=c+1;)。

问题:
但是notepad++一般都是统一替换,没办法快速简单的达到目的,但是使用正则就可以办到。
原始的SQL语句为:

INSERT INTO `accounts` (`email`,`password`,`nickname`,`gender`,`created_at`,`id`,`status`,`birthday`) VALUES ('ysss68@gmail.com','ddd','yoni',1,'2017-09-13 00:12:10',1111,'2000-09-12');

最终的语句为:

INSERT INTO `accounts` (`email`,`password`,`nickname`,`gender`,`created_at`,`id`,`status`,`birthday`) VALUES ('ysss68@gmail.com','aaaa','yoni',1,'2017-09-13 00:12:10',111,'2000-09-12') ON DUPLICATE KEY UPDATE email='ysss68@gmail.com',password='aaaa',nickname='yoni';

设置查找目标为:
INSERT INTO accounts (email,password,nickname,gender,created_at,id,status,birthday) VALUES ('([^']+)','([^']+)','([^']+)',([^,]+),'([^']+)',([^,]+),([^,]+),'([^']+)');
设施替换目标为:
INSERT INTO accounts (email,password,nickname,gender,created_at,id,status,birthday) VALUES ('$1','$2','$3',$4,'$5',$6,$7,'$8') ON DUPLICATE KEY UPDATE email='$1',password='$2',nickname='$3';

Java使用thrift调用python服务

RPC的框架很多,有gRpc,thrift,腾讯的Tars,阿里dubbo,但是他们的性能各有差异,目前我个人觉得thrift比较纯粹。

thrift的简单示意图(盗图)
20160511151037053.jpg

实际测试过thrift和xmlrpc,不是很严格的测试,thrift有70%左右的提升
我使用最新的0.10.0失败了,通过thrift生成的java类文件多处报错,换回0.9.3问题解决

具体可参考:
http://blog.csdn.net/hrn1216/article/details/51363902
https://my.oschina.net/u/780876/blog/691293

Java使用XMLRPC调用python服务

相关概念:

rpc远程调用原理 :

比如 A调用B提供的remoteAdd方法:

首先A与B之间建立一个TCP连接;

然后A把需要调用的方法名(这里是remoteAdd)以及方法参数(10, 20)序列化成字节流发送出去;

B接受A发送过来的字节流,然后反序列化得到目标方法名,方法参数,接着执行相应的方法调用(可能是localAdd)并把结果30返回;

A接受远程调用结果,然后do()。

RPC框架也就是把上线说的具体的细节封装起来,给用户好用的API使用(提示:有些远程调用选择比较底层的socket协议,有些远程调用选择比较上层的HTTP协议);

XML-RPC:XML Remote Procedure Call,即XML远程方法调用,利用http+xml封装进行RPC调用。

python2.x服务端:

#coding=utf-8
from SimpleXMLRPCServer import SimpleXMLRPCServer
from SocketServer import ThreadingMixIn 
from xmlrpclib import ServerProxy 
import thread 
class ThreadXMLRPCServer(ThreadingMixIn, SimpleXMLRPCServer): 
    pass 
    
class RPCServer(): 
    def __init__(self, ip='127.0.0.1', port='8000'): 
        self.ip = ip 
        self.port = int(port) 
        self.svr = None 
        
    def start(self, func_lst): 
        thread.start_new_thread(self.service, (func_lst, 0,)) 
        
    def resume_service(self, v1, v2): 
        self.svr.serve_forever(poll_interval=0.001) 
        
    def service(self, func_lst, v1): 
        self.svr = ThreadXMLRPCServer((self.ip, self.port), allow_none=True) 
        for func in func_lst: 
            self.svr.register_function(func) 
        self.svr.serve_forever(poll_interval=0.001)
        
    def activate(self): 
        thread.start_new_thread(self.resume_service, (0, 0,)) 
        
    def shutdown(self): 
        try: 
            self.svr.shutdown() 
        except Exception, e: 
            print 'rpc_server shutdown:', str(e) 
            
class RPCClient(): 
    def __init__(self, ip='127.0.0.1', port='8000'): 
        self.svr = ServerProxy('http://'+ip+':'+port+'/', allow_none=True, use_datetime=True) 
        
    def get_svr(self): 
        return self.svr 
        
def get_hello(): 
    return 'hello!' 

if __name__ == "__main__": 
    r = RPCServer('0.0.0.0', '8061') 
    r.service([get_hello], 0) #这里仅仅载入get_hello函数

注意:以上是Python2.x,python3的简单对应关系是:

from SimpleXMLRPCServer import SimpleXMLRPCServer

import xmlrpc.server

from SocketServer import ThreadingMixIn

import socketserver

from xmlrpclib import ServerProxy

import xmlrpc

import thread

import _thread

java客户端:
首先下载jar包 ,在 https://archive.apache.org/dist/ws/xmlrpc/binaries/ 找到 apache-xmlrpc-3.1.3-bin.tar.gz 。目前还没有找到maven的版本

import java.net.URL;
import java.net.MalformedURLException;
import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.client.XmlRpcHttpTransportException;

public class Test {
    public static void main(String[] args) throws MalformedURLException,
    XmlRpcHttpTransportException {
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL("http://127.0.0.1:8061/RPC2"));
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(config);
        // 根据不同的python函数形式,构造参数
        // 两个整形参数
        //Object[] params = new Object[] {new Integer(1), new Integer(2)};
        
        // 单个字符串参数
        //Object[] params = new Object[] {new String("HELLO")};
        
        // 无参数
        Object[] params = null;
        try {
            // 返回的结果是字符串类型,强制转换res为String类型
            String res = (String) client.execute("get_hello", params);
            System.out.println(res);
        } catch (XmlRpcException e11) {
            e11.printStackTrace();
        }
    }
}