1.Mysql-udf-http镜像已上传到dockerhub中
docker pull heidaodageshiwo/mysql-udf-http:v1
2.启动镜像(默认密码root1234)
docker run -tid -p 3306:3306 --name=mysql-udf-http --privileged=true heidaodageshiwo/mysql-udf-http:v1
3.命令
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
heidaodageshiwo/mysql-udf-http v1 fc2684ea3cec 11 months ago 5.36GB
[root@localhost ~]# docker start mysql
mysql
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e55e89d35e01 mysql-udf-http:v1 "/usr/sbin/init" 10 days ago Up 2 seconds 0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 9001/tcp mysql
[root@localhost ~]# ^C
[root@localhost ~]#
4.连接:root/root1234
5.运行
SELECT http_post('http://192.168.101.9:8054/udf/test2','a=xoyo333&b=0111111');
192.168.101.9:机器ip地址 8054项目端口 udf/test2接口 a=xoyo333&b=0111111入参
6.java代码:
controller
@RequestMapping("/udf")
@RestController
public class MysqlUdfHttpController {
@Resource
UdfMapper udfMapper;
@RequestMapping("/test2")
public String getMenuList12(String a,String b) {
return a+b;
}
@RequestMapping("/test1")
public List<Menu> test1(@RequestParam long pid) {
String s = udfMapper.httpGets("http://192.168.101.9:8054/udf/test11?pid=1" );
List<Menu> menus = JSONArray.parseArray(s, Menu.class);
return menus;
}
@RequestMapping("/test11")
public List<Menu> test11(@RequestParam long pid) {
List<Menu> menu = menuService.findMenu(pid);
return menu;
}
}
UdfMapper
package com.zq.mybatisexample.mapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface UdfMapper {
@Select("select http_get(#{url}, #{params}) as response from dual")
String httpGet(@Param("url") String url, @Param("params") String params);
@Select("select http_post(#{url}, #{params}) as response from dual")
String httpPost(@Param("url") String url, @Param("params") String params);
@Select("select http_get(#{url}) from dual")
String httpGets(@Param("url") String url );
}
7.测试:
navaicat中运行选择的sql即可调用到 代码中的代码:
8.例子2:实现( 请求java接口->mysql udf -> java接口 )
页面请求接口:http://localhost:8054/udf/test1?pid=1
解释:
页面调用:http://localhost:8054/udf/test1?pid=1
然后使用mysql udf的转发功能
String s = udfMapper.httpGets("http://192.168.101.9:8054/udf/test11?pid=1" );
这一句作用:让mysql udf数据库去调用
http://192.168.101.9:8054/udf/test11?pid=1 这个接口 ( 可结合使用上方例子1 SELECT http_post('http://192.168.101.9:8054/udf/test2','a=vvv&b=aaaaa'); 来理解即可 )
然后test11接口返回数据即可。