feat:http连接url 创建 mysql服务存入redis

master
hbr 2024-05-30 21:17:32 +08:00
parent b619c0341c
commit 0a69307158
2 changed files with 99 additions and 33 deletions

View File

@ -16,8 +16,18 @@ import com.zhiLian.common.system.domain.SysUser;
import com.zhiLian.common.system.remote.RemoteUserService; import com.zhiLian.common.system.remote.RemoteUserService;
import org.apache.catalina.User; import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service; 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; import java.util.List;
/** /**
@ -36,6 +46,9 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
@Autowired @Autowired
private RemoteUserService remoteUserService; private RemoteUserService remoteUserService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
/** /**
* *
* *
@ -65,8 +78,6 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
return businessMapper.selectBusinessList(business); return businessMapper.selectBusinessList(business);
} }
business.setId(Long.valueOf(user.getUserType())); business.setId(Long.valueOf(user.getUserType()));
// List<Business> businesses = this.selectBusinessList(business);
// System.out.println(businesses);
return businessMapper.selectBusinessList(business); return businessMapper.selectBusinessList(business);
} }
@ -94,9 +105,53 @@ public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business>
.userType(String.valueOf(business.getId())) .userType(String.valueOf(business.getId()))
.build(); .build();
Result add = remoteUserService.add(sysUser); 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; 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<String, String> 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);
}
}
/** /**
* *

View File

@ -5,45 +5,56 @@ package com.zhiLian.business.text; /**
* @ClassName text * @ClassName text
* @Date 2024/05/29 10:31 * @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.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.HashMap;
public class DatabaseInitializer { public class DatabaseInitializer {
public static void main(String[] args) { public static void main(String[] args) {
// 数据库连接URL格式为jdbc:子协议:子名称 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";
String jdbcUrl = "jdbc:mysql://122.51.111.225:3306/day1?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false"; HashMap<String, String> hashMap = new HashMap<>();
String username = "root"; hashMap.put("businessId",30+"sdftdfg");
String password = "hbr@123"; hashMap.put("mysqlPort",String.valueOf(3306+30));
String json = JSON.toJSONString(hashMap);
Connection connection = null; // 3.创建连接与设置连接参数
URL urlObj = null;
try { try {
// 加载数据库驱动类(对于大多数数据库,这一步不是必需的,因为驱动会在第一次连接时自动加载) urlObj = new URL(postUrl);
Class.forName("com.mysql.cj.jdbc.Driver"); HttpURLConnection httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setRequestMethod("POST");
// 建立数据库连接 httpConn.setRequestProperty("Charset", "UTF-8");
connection = DriverManager.getConnection(jdbcUrl, username, password); // POST请求且JSON数据,必须设置
httpConn.setRequestProperty("Content-Type", "application/json");
// 连接成功可以在此执行SQL操作 // 打开输出流,默认是false
System.out.println("数据库连接成功!"); httpConn.setDoOutput(true);
// 打开输入流,默认是true,可省略
} catch (ClassNotFoundException e) { httpConn.setDoInput(true);
System.out.println("数据库驱动未找到!"); // 4.从HttpURLConnection获取输出流和写数据
e.printStackTrace(); OutputStream oStream = httpConn.getOutputStream();
} catch (SQLException e) { oStream.write(json.getBytes());
System.out.println("数据库连接失败!"); oStream.flush();
e.printStackTrace(); // 5.发起http调用(getInputStream触发http请求)
} finally { if (httpConn.getResponseCode() != 200) {
// 确保在结束时关闭连接 throw new Exception("调用服务端异常.");
if (connection != null) { }
try { // 6.从HttpURLConnection获取输入流和读数据
connection.close(); BufferedReader br = new BufferedReader(
} catch (SQLException e) { new InputStreamReader(httpConn.getInputStream()));
e.printStackTrace(); String resultData = br.readLine();
} System.out.println("从服务端返回结果: " + resultData);
} // 7.关闭HttpURLConnection连接
httpConn.disconnect();
} catch (Exception e) {
throw new RuntimeException(e);
} }
} }
} }