如何用java 5分钟实现一个最简单的mysql代理服务器

如何用java 5分钟实现一个最简单的mysql代理服务器,第1张

如何用java 5分钟实现一个最简单的mysql代理服务器

首先,准备开发工具套件,我们并不会引入过多工具包,仅仅需要:

java8

vert.x 3

如果你是用maven做为项目管理工具,请将vert.x 3引入:

1

2

3

4

5

<dependency>

<groupId>io.vertx</groupId>

<artifactId>vertx-core</artifactId>

<version>3.3.2</version>

</dependency>

代码实现:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

package

com.maxleap.mysqlproxy

import

io.vertx.core.AbstractVerticle

import

io.vertx.core.Vertx

import

io.vertx.core.logging.Logger

import

io.vertx.core.logging.LoggerFactory

import

io.vertx.core.net.NetClient

import

io.vertx.core.net.NetServer

import

io.vertx.core.net.NetSocket

/**

*

@author sneaky

*

@since 1.0.0

*/

public

class

MysqlProxyServer

{

private

static

final

Logger

logger

=

LoggerFactory.getLogger(MysqlProxyServer.class)

public

static

void

main(String[]

args)

{

Vertx.vertx().deployVerticle(new

MysqlProxyServerVerticle())

}

public

static

class

MysqlProxyServerVerticle

extends

AbstractVerticle

{

private

final

int

port

=

3306

private

final

String

mysqlHost

=

"10.10.0.6"

@Override

public

void

start()

throws

Exception

{

NetServer

netServer

=

vertx.createNetServer()//创建代理服务器

NetClient

netClient

=

vertx.createNetClient()//创建连接mysql客户端

netServer.connectHandler(socket

->

netClient.connect(port,

mysqlHost,

result

->

{

//响应来自客户端的连接请求,成功之后,在建立一个与目标mysql服务器的连接

if

(result.succeeded())

{

//与目标mysql服务器成功连接连接之后,创造一个MysqlProxyConnection对象,并执行代理方法

new

MysqlProxyConnection(socket,

result.result()).proxy()

MySQL Community Server 社区版本,免费,不提供官方技术支持

MySQL Enterprise Server 企业版服务器 ,掏钱,还有电话技术支持

MySQL Cluster 2台以上 mysql集群服务器

MySQL Proxy: 应该是做代理服务器的工具,跟sqlserver agent一样。

MySQL Workbench有两个版本:设计数据库的

MySQL Workbench Community Edition(又叫MySQL Workbench OSS,社区版)和MySQL Workbench Standard Edition(又叫MySQL Workbench SE,商业版)。MySQL Workbench OSS是在GPL证书下发布的开源社区版本,而MySQL Workbench SE则是按年收费的商业版本。其功能方面的差异见下表(不得不说,数据库/模型同步这一重要的功能竟然只在收费的MySQL Workbench SE中可用,而在DBDesigner4中这却是基本功能,这种“继任”方式实在让人恶心。):


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8716259.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-20
下一篇 2023-04-20

发表评论

登录后才能评论

评论列表(0条)

保存