Skip to content

Commit

Permalink
6.3 v2.11 整合spring和mybatis-plus 将自己的用户中心改造一块 当作rpc的监控中心 同时进行一些判断逻辑上的优…
Browse files Browse the repository at this point in the history
…化,解决日志乱码问题
  • Loading branch information
zzzzzzzzyt committed Jun 3, 2022
1 parent 104f72c commit c1786e4
Show file tree
Hide file tree
Showing 30 changed files with 102 additions and 134 deletions.
4 changes: 4 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.

This file was deleted.

This file was deleted.

This file was deleted.

28 changes: 0 additions & 28 deletions .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ private static Customer start0() {
NettyConsumerBootStrap20.main(new String[]{"127.0.0.1", String.valueOf(6668)});
return null;
case "2.1":
return NettyConsumerBootStrap21.main(null);
return NettyConsumerBootStrap21.main();
case "2.2":
return NettyConsumerBootStrap22.main(null);
return NettyConsumerBootStrap22.main();
case "2.4": //这个版本各种序列化工具的使用
case "2.5":
case "2.6":
case "2.7":
case "2.8":
case "2.9":
return NettyConsumerBootStrap24.main(null);
return NettyConsumerBootStrap24.main();
default:
try {
throw new RpcException("该版本还没出呢,你如果有想法可以私信我,或者提个pr");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package provider.monitor;
package monitor;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
Expand Down
59 changes: 59 additions & 0 deletions zyt-rpc-common/src/main/java/monitor/RpcMonitorOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package monitor;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import exception.RpcException;
import lombok.extern.slf4j.Slf4j;
import monitor.mapper.RpcMonitorMapper;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* @Author 祝英台炸油条
* @Time : 2022/6/3 19:47
**/
@Slf4j
public class RpcMonitorOperator {

static RpcMonitorMapper rpcMonitorMapper;

static {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml");
rpcMonitorMapper = context.getBean(RpcMonitorMapper.class);
}



/**
* 作用 就是每次开启服务提供商的时候 删除掉所有的对应项目
*/
public void deleteAll() {
rpcMonitorMapper.delete(null);
}

/**
* 每次开启服务的时候 就进行添加对应的服务 当然 起始调用次数为0
*/
public void addServer(RpcMonitor rpcMonitorServer) {
rpcMonitorMapper.insert(rpcMonitorServer);
}

/**
* 更新相应的服务 将相应的服务调用次数+1 同时update的时候 会自动的更改调用时间 做这个的时候 要上锁 防止线程冲突
*/
public synchronized void updateServer(String methodAddress) {
QueryWrapper<RpcMonitor> wrapper = new QueryWrapper<>();
// 没问题 查询的时候 是跟对应的列名进行比较
wrapper.eq("method_name",methodAddress);
RpcMonitor rpcMonitor = rpcMonitorMapper.selectOne(wrapper);
if (rpcMonitor==null) try {
throw new RpcException("监控中心出现错误");
} catch (RpcException e) {
log.error(e.getMessage(),e);
return;
}
rpcMonitor.setCallNum(rpcMonitor.getCallNum()+1);
rpcMonitorMapper.update(rpcMonitor,new QueryWrapper<RpcMonitor>().eq("id",rpcMonitor.getId()));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package provider.monitor.mapper;
package monitor.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import provider.monitor.RpcMonitor;
import monitor.RpcMonitor;

/**
* @Author 祝英台炸油条
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- Import Properties -->
<!--1.关联数据库配置文件 读取配置文件-->
<!--<context:property-placeholder location="classpath:database.properties"/>-->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="provider.monitor.mapper"/>
<property name="basePackage" value="monitor.mapper"/>
</bean>

<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author 祝英台炸油条
*/
public class NettyConsumerBootStrap21 {
public static Customer main(String[] args) {
public static Customer main() {
ClientProxyTool proxy = new ClientProxyTool();
return (Customer) proxy.getBean(Customer.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author 祝英台炸油条
*/
public class NettyConsumerBootStrap22 {
public static Customer main(String[] args) {
public static Customer main() {
ClientProxyTool proxy = new ClientProxyTool();
return (Customer) proxy.getBean(Customer.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @author 祝英台炸油条
*/
public class NettyConsumerBootStrap24 {
public static Customer main(String[] args) {
public static Customer main() {
ClientProxyTool proxy = new ClientProxyTool();
return (Customer) proxy.getBean(Customer.class);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import consumer.service_discovery.ZkServiceDiscovery;
import exception.RpcException;
import lombok.extern.slf4j.Slf4j;
import monitor.RpcMonitorOperator;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
Expand Down Expand Up @@ -52,6 +53,11 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy me
log.error(e.getMessage(), e);
}
assert methodAddress != null;

//每次调用时 更新对应方法的调用次数和调用方法
RpcMonitorOperator rpcMonitorOperator = new RpcMonitorOperator();
rpcMonitorOperator.updateServer(methodAddress);

String[] split = methodAddress.split(":");
return NettyClient.callMethod(split[0], Integer.parseInt(split[1]), args[0], method);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import consumer.service_discovery.ZkServiceDiscovery;
import exception.RpcException;
import lombok.extern.slf4j.Slf4j;
import monitor.RpcMonitorOperator;

import java.lang.reflect.Proxy;

Expand Down Expand Up @@ -36,6 +37,11 @@ public Object getBean(final Class<?> serviceClass) {
log.error(e.getMessage(), e);
}
assert methodAddress != null;

//每次调用时 更新对应方法的调用次数和调用方法
RpcMonitorOperator rpcMonitorOperator = new RpcMonitorOperator();
rpcMonitorOperator.updateServer(methodAddress);

String[] strings = methodAddress.split(":");
String hostName = strings[0];
int port = Integer.parseInt(strings[1]);
Expand Down
Loading

0 comments on commit c1786e4

Please sign in to comment.