centos7下springboot2.x集成mycat+postgresql

1.准备环境:

mycat1.6
postgresql9.6
springboot2.1.7

需要降级的pom依赖(不降级jdbc无法连接mycat成功)

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
 </dependency>

2.在地址[http://www.mycat.org.cn/]下载mycat1.6.7.3-release版本。
解压之后即可使用。
3.在192.168.1.235和192.168.1.236两台机器上都安装好pgsql9.6。
4.开始配置mycat的server.xml.配置信息如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- - - Licensed under the Apache License, Version 2.0 (the "License"); 
    - you may not use this file except in compliance with the License. - You 
    may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 
    - - Unless required by applicable law or agreed to in writing, software - 
    distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT 
    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the 
    License for the specific language governing permissions and - limitations 
    under the License. -->
<!DOCTYPE mycat:server SYSTEM "server.dtd">
<mycat:server xmlns:mycat="http://io.mycat/">
    <user name="postgres">
                <property name="password">postgres</property>
                <property name="schemas">public</property>
        </user>
</mycat:server>

值得注意的是,这里的 <property name="schemas">public</property> public代表pgsql的模式名,
和mysql有差异。因为mysql没有模式这一层。

5.开始配置mycat的schema.xml.配置如下:

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

 <schema name="public" checkSQLschema="false" sqlMaxLimit="100" >
 <table name="test_table" dataNode="mydn3"  />
</schema> 

<dataNode name="mydn3" dataHost="myhost3" database="test" />

               
     <dataHost name="myhost3" maxCon="100" minCon="10" balance="3" writeType="0" dbType="postgresql" dbDriver="jdbc">
                <heartbeat>select user</heartbeat>
                <writeHost host="hostM3" url="jdbc:postgresql://192.168.1.235:5432/test" user="postgres" password="postgres">
                        <readHost host="hostS3" url="jdbc:postgresql://192.168.1.236:5432/test" user="postgres" password="postgres"/>
                </writeHost>
        </dataHost>

</mycat:schema>

值得注意的是,<schema>标签下必须配置一个<table>标签,要不然mycat会启动失败。
还有,
6.rule.xml文件暂时用不到不用配置。需要分片规则的时候再用。

7.在运行mycat的时候 先用命令行 ./mycat console 这个,便于控制台直接打印出报错信息,并进行排错。出现如下图所示,代表mycat启动成功。


image.png

8.mycat启动成功后,要想连接上mycat的话,必须执行命令mysql -upostgres -ppostgres -h192.168.1.235 -P8066 才能连接上,不能用默认pgsql的 psql命令。因为mycat的逻辑库还是mysql。
那么问题来了,此时必须要有一台机器安装了mysql才行。我在192.168.1.244这台机器上装了mysql,然后就可以直接登录mysql了。

小朋友是不是有很多问号?坑才刚刚开始。

9.接下来,就到了springboot集成mycat了。我用的springboot2.1.7版本,它默认的依赖mysql和druid直接连接mycat的话,都是会报错的。原因就是新版本不兼容老的mycat的。那么此时,就要回退mysql-connect和druid的版本了。都剔除掉原来springboot的mysql和druid依赖。

       <exclusions>
                <exclusion>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                </exclusion>
               <exclusion>
                   <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                </exclusion>
            </exclusions>

然后引入旧版本的依赖。

<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.6</version>
 </dependency>

10.最后配置一下datasource链接属性。

spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.url=jdbc:mysql://192.168.1.235:8066/public?useSSL=false
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

11.最后就成功了。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。