暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

每天五分钟写K8(四):SpringBoot与mysql整合

CodingWithFun 2019-09-17
695

在第一节中我们创建了mysql的Service,上一节我们运行了Spring-boot的项目。这一节我们将两个项目结合起来,让spring-boot访问mysql服务。


一、给Spring-boot项目的Deployment添加环境变量

Kubernetes运行着大量的Service,那么必然需要服务发现机制。由于一个Service的Cluster IP不会发生改变,那么Kubenetes就使用了环境变量来解决了这个问题。使用时将要使用的服务添加到环境变量即可。在上节的yaml文件中修改spec.template.spec.containers项即可。

    containers:
    - name: springboot-demo
    image: k8s-boot:2.0
    imagePullPolicy: Never
    ports:
    - containerPort: 8080
    env:
        - name: MYSQL_SERVICE_HOST
    value: 'mysql'
    - name: MYSQL_SERVICE_PORT
    value: '3306'


    二、准备可访问数据的Spring-boot项目

    将上节spring-boot项目中加入mybatis,然后写个访问数据的controller即可。注意配置中的jdbcUrl要特殊处理一下,需要从环境变量中获取IP地址,mysql地址写成${MYSQL_SERVICE_HOST:localhost},即:

      spring:
      datasource:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://${MYSQL_SERVICE_HOST:localhost}:${MYSQL_SERVICE_PORT:3306}/test?useUnicode=true&characterEncoding=utf-8
      username: root
      password: 123456


      三、按上节部署方式进行部署

      上传镜像是一个比较繁琐的工作,我提供了上传到指定主机的脚本image_deploy.py在github上。


      部署完成后在浏览器中测试:http://<node IP>:30000/user/findAll。最终会返回以下错误:

        java.sql.SQLSyntaxErrorException: Unknown database 'test'

        这是因为我们的数据库服务尚未进行正确的初始化。


        四、初始化数据库

        初始化数据库有很多种方式,例如:

        • 暴露Service的NodePort,然后用图形化客户端连接,执行SQL语句

        • 在Pod中运行mysql客户端,执行SQL语句

        • 借助ConfigMap,启动Pod时获取初始化SQL语句并执行

        第一种咱们已经接触过了,第三种尚未学习。今天我们就采用第二种初始化。


        第一步要获取mysql pod的名称:

          kubectl get pod  -l app=mysql
          NAME READY STATUS RESTARTS AGE
          mysql-qkfcd 1/1 Running 0 4d22h

          第二步进入容器:

            $ kubectl exec -it mysql-qkfcd /bin/bash
            root@mysql-qkfcd:/#

            第三步连接mysql服务,执行SQL

              root@mysql-qkfcd:/$ mysql -uroot -p
              Enter password:
              Welcome to the MySQL monitor. Commands end with ; or \g.
              Your MySQL connection id is 18
              Server version: 8.0.17 MySQL Community Server - GPL


              Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.


              Oracle is a registered trademark of Oracle Corporation and/or its
              affiliates. Other names may be trademarks of their respective
              owners.


              Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.


              mysql> create database test;
              Query OK, 1 row affected (0.00 sec)


              mysql> use test;
              Database changed
              mysql> CREATE TABLE `user` (
              -> `id` int(11) NOT NULL AUTO_INCREMENT,
              -> `user_name` varchar(45) NOT NULL,
              -> `password` varchar(45) NOT NULL,
              -> PRIMARY KEY (`id`)
              -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
              Query OK, 0 rows affected, 1 warning (0.01 sec)


              mysql> exit

              最后退出即可。


              代码下载:

              https://github.com/loveoobaby/blog_code


              文章转载自CodingWithFun,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

              评论