diff --git a/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/service/impl/BusinessServiceImpl.java b/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/service/impl/BusinessServiceImpl.java index 3683339..092bd6d 100644 --- a/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/service/impl/BusinessServiceImpl.java +++ b/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/service/impl/BusinessServiceImpl.java @@ -16,8 +16,18 @@ import com.zhiLian.common.system.domain.SysUser; import com.zhiLian.common.system.remote.RemoteUserService; import org.apache.catalina.User; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.util.HashMap; import java.util.List; /** @@ -36,6 +46,9 @@ public class BusinessServiceImpl extends ServiceImpl @Autowired private RemoteUserService remoteUserService; + @Autowired + private RedisTemplate redisTemplate; + /** * 查询企业 * @@ -65,8 +78,6 @@ public class BusinessServiceImpl extends ServiceImpl return businessMapper.selectBusinessList(business); } business.setId(Long.valueOf(user.getUserType())); -// List businesses = this.selectBusinessList(business); -// System.out.println(businesses); return businessMapper.selectBusinessList(business); } @@ -94,9 +105,53 @@ public class BusinessServiceImpl extends ServiceImpl .userType(String.valueOf(business.getId())) .build(); Result add = remoteUserService.add(sysUser); - System.out.println(sysUser); + httpConnectMysql(business); + if (redisTemplate.hasKey(business.getId()+business.getName())){ + httpConnectMysql(business); + }else{ + redisTemplate.opsForValue().set(business.getId()+business.getName(),String.valueOf(3306+business.getId())); + } +// redisTemplate.opsForValue().set(business.getName()+""+business.getId(),); return i; } + public void httpConnectMysql(Business business){ + String postUrl="http://122.51.111.225:10006/webhook/%E6%96%B0%E5%BB%BA%E4%BC%81%E4%B8%9A%E6%95%B0%E6%8D%AE%E6%BA%90"; + HashMap hashMap = new HashMap<>(); + hashMap.put("businessId",business.getId()+business.getName()); + hashMap.put("mysqlPort",String.valueOf(3306+business.getId())); + String json = JSON.toJSONString(hashMap); + // 3.创建连接与设置连接参数 + URL urlObj = null; + try { + urlObj = new URL(postUrl); + HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection(); + httpConn.setRequestMethod("POST"); + httpConn.setRequestProperty("Charset", "UTF-8"); + // POST请求且JSON数据,必须设置 + httpConn.setRequestProperty("Content-Type", "application/json"); + // 打开输出流,默认是false + httpConn.setDoOutput(true); + // 打开输入流,默认是true,可省略 + httpConn.setDoInput(true); + // 4.从HttpURLConnection获取输出流和写数据 + OutputStream oStream = httpConn.getOutputStream(); + oStream.write(json.getBytes()); + oStream.flush(); + // 5.发起http调用(getInputStream触发http请求) + if (httpConn.getResponseCode() != 200) { + throw new Exception("调用服务端异常."); + } + // 6.从HttpURLConnection获取输入流和读数据 + BufferedReader br = new BufferedReader( + new InputStreamReader(httpConn.getInputStream())); + String resultData = br.readLine(); + System.out.println("从服务端返回结果: " + resultData); + // 7.关闭HttpURLConnection连接 + httpConn.disconnect(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } /** * 修改企业 diff --git a/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/text/DatabaseInitializer.java b/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/text/DatabaseInitializer.java index f2d711e..a478561 100644 --- a/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/text/DatabaseInitializer.java +++ b/zhiLian-business/zhiLian-data-service/src/main/java/com/zhiLian/business/text/DatabaseInitializer.java @@ -5,45 +5,56 @@ package com.zhiLian.business.text; /** * @ClassName text * @Date 2024/05/29 10:31 */ +import com.alibaba.fastjson.JSON; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; +import java.util.HashMap; public class DatabaseInitializer { public static void main(String[] args) { - // 数据库连接URL,格式为:jdbc:子协议:子名称 - String jdbcUrl = "jdbc:mysql://122.51.111.225:3306/day1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false"; - String username = "root"; - String password = "hbr@123"; - - Connection connection = null; - + String postUrl="http://122.51.111.225:10006/webhook/%E6%96%B0%E5%BB%BA%E4%BC%81%E4%B8%9A%E6%95%B0%E6%8D%AE%E6%BA%90"; + HashMap hashMap = new HashMap<>(); + hashMap.put("businessId",30+"sdftdfg"); + hashMap.put("mysqlPort",String.valueOf(3306+30)); + String json = JSON.toJSONString(hashMap); + // 3.创建连接与设置连接参数 + URL urlObj = null; try { - // 加载数据库驱动类(对于大多数数据库,这一步不是必需的,因为驱动会在第一次连接时自动加载) - Class.forName("com.mysql.cj.jdbc.Driver"); - - // 建立数据库连接 - connection = DriverManager.getConnection(jdbcUrl, username, password); - - // 连接成功,可以在此执行SQL操作 - System.out.println("数据库连接成功!"); - - } catch (ClassNotFoundException e) { - System.out.println("数据库驱动未找到!"); - e.printStackTrace(); - } catch (SQLException e) { - System.out.println("数据库连接失败!"); - e.printStackTrace(); - } finally { - // 确保在结束时关闭连接 - if (connection != null) { - try { - connection.close(); - } catch (SQLException e) { - e.printStackTrace(); - } + urlObj = new URL(postUrl); + HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection(); + httpConn.setRequestMethod("POST"); + httpConn.setRequestProperty("Charset", "UTF-8"); + // POST请求且JSON数据,必须设置 + httpConn.setRequestProperty("Content-Type", "application/json"); + // 打开输出流,默认是false + httpConn.setDoOutput(true); + // 打开输入流,默认是true,可省略 + httpConn.setDoInput(true); + // 4.从HttpURLConnection获取输出流和写数据 + OutputStream oStream = httpConn.getOutputStream(); + oStream.write(json.getBytes()); + oStream.flush(); + // 5.发起http调用(getInputStream触发http请求) + if (httpConn.getResponseCode() != 200) { + throw new Exception("调用服务端异常."); } + // 6.从HttpURLConnection获取输入流和读数据 + BufferedReader br = new BufferedReader( + new InputStreamReader(httpConn.getInputStream())); + String resultData = br.readLine(); + System.out.println("从服务端返回结果: " + resultData); + // 7.关闭HttpURLConnection连接 + httpConn.disconnect(); + } catch (Exception e) { + throw new RuntimeException(e); } } }