-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,444 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.huifer.fzjh.service; | ||
|
||
import com.huifer.fzjh.bean.RequestEntity; | ||
import com.huifer.fzjh.bean.ServerWeight; | ||
import com.huifer.fzjh.exception.LoadBalanceException; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* 负载均衡算法实现: 平滑加权算轮询法 | ||
*/ | ||
@Slf4j | ||
public class SmoothnessWeightRandomLoadBalance extends AbstractLoadBalance { | ||
private int count = -1; | ||
private RequestEntity requestEntity; | ||
|
||
private List<ServerWeight> serverWeights; | ||
|
||
public SmoothnessWeightRandomLoadBalance(RequestEntity requestEntity, List<ServerWeight> serverWeights) { | ||
super(requestEntity, serverWeights); | ||
this.count = serverWeights.size(); | ||
this.requestEntity = requestEntity; | ||
this.serverWeights = serverWeights; | ||
} | ||
|
||
private static String getServiceIndex(HashMap<Integer, ServerWeight> serverMap) { | ||
ServerWeight maxWeightServer = null; | ||
int allWeight = serverMap.values().stream().mapToInt(ServerWeight::getWeight).sum(); | ||
for (Map.Entry<Integer, ServerWeight> item : serverMap.entrySet()) { | ||
ServerWeight currentServer = item.getValue(); | ||
if (maxWeightServer == null || currentServer.getCurrentWeight() > maxWeightServer.getCurrentWeight()) { | ||
maxWeightServer = currentServer; | ||
} | ||
} | ||
maxWeightServer.setCurrentWeight(maxWeightServer.getCurrentWeight() - allWeight); | ||
for (Map.Entry<Integer, ServerWeight> item : serverMap.entrySet()) { | ||
ServerWeight currentServer = item.getValue(); | ||
currentServer.setCurrentWeight(currentServer.getCurrentWeight() + currentServer.getWeight()); | ||
} | ||
return maxWeightServer.getIp() + ":" + maxWeightServer.getPort(); | ||
} | ||
|
||
@Override | ||
public String loadBalance() { | ||
if (count < 0) { | ||
throw new LoadBalanceException("机器数量不能小于0"); | ||
} | ||
|
||
HashMap<Integer, ServerWeight> serverHashMap = new HashMap<>(); | ||
for (int i = 0; i < serverWeights.size(); i++) { | ||
serverHashMap.put(i, serverWeights.get(i)); | ||
} | ||
String hostPost = getServiceIndex(serverHashMap); | ||
log.info("当前请求信息={},负载均衡计算后的机器hostPost={}", requestEntity, hostPost); | ||
return hostPost; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/** | ||
* Copyright 2009-2019 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.apache.ibatis.type; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Date; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.mockito.Mockito.*; | ||
|
||
class TimeOnlyTypeHandlerTest extends BaseTypeHandlerTest { | ||
|
||
private static final TypeHandler<Date> TYPE_HANDLER = new TimeOnlyTypeHandler(); | ||
private static final Date DATE = new Date(); | ||
private static final java.sql.Time SQL_TIME = new java.sql.Time(DATE.getTime()); | ||
|
||
@Override | ||
@Test | ||
public void shouldSetParameter() throws Exception { | ||
TYPE_HANDLER.setParameter(ps, 1, DATE, null); | ||
verify(ps).setTime(1, SQL_TIME); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultFromResultSetByName() throws Exception { | ||
when(rs.getTime("column")).thenReturn(SQL_TIME); | ||
assertEquals(DATE, TYPE_HANDLER.getResult(rs, "column")); | ||
verify(rs, never()).wasNull(); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultNullFromResultSetByName() throws Exception { | ||
when(rs.getTime("column")).thenReturn(null); | ||
assertNull(TYPE_HANDLER.getResult(rs, "column")); | ||
verify(rs, never()).wasNull(); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultFromResultSetByPosition() throws Exception { | ||
when(rs.getTime(1)).thenReturn(SQL_TIME); | ||
assertEquals(DATE, TYPE_HANDLER.getResult(rs, 1)); | ||
verify(rs, never()).wasNull(); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultNullFromResultSetByPosition() throws Exception { | ||
when(rs.getTime(1)).thenReturn(null); | ||
assertNull(TYPE_HANDLER.getResult(rs, 1)); | ||
verify(rs, never()).wasNull(); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultFromCallableStatement() throws Exception { | ||
when(cs.getTime(1)).thenReturn(SQL_TIME); | ||
assertEquals(DATE, TYPE_HANDLER.getResult(cs, 1)); | ||
verify(cs, never()).wasNull(); | ||
} | ||
|
||
@Override | ||
@Test | ||
public void shouldGetResultNullFromCallableStatement() throws Exception { | ||
when(cs.getTime(1)).thenReturn(null); | ||
assertNull(TYPE_HANDLER.getResult(cs, 1)); | ||
verify(cs, never()).wasNull(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright 2009-2015 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
package org.apache.ibatis.submitted.nested_query_cache; | ||
|
||
import org.apache.ibatis.domain.blog.Author; | ||
|
||
public interface AuthorMapper { | ||
|
||
Author selectAuthor(int id); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.huifer.rbac.entity.db; | ||
|
||
import com.baomidou.mybatisplus.annotation.IdType; | ||
import com.baomidou.mybatisplus.annotation.TableField; | ||
import com.baomidou.mybatisplus.annotation.TableId; | ||
import com.baomidou.mybatisplus.annotation.TableLogic; | ||
import com.baomidou.mybatisplus.annotation.TableName; | ||
import java.io.Serializable; | ||
import java.time.LocalDateTime; | ||
|
||
import com.baomidou.mybatisplus.annotation.Version; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* 角色和菜单的关系表 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@TableName(value = "rbac.middle_role_menu") | ||
public class MiddleRoleMenu implements Serializable { | ||
@TableId(value = "id", type = IdType.AUTO) | ||
private Long id; | ||
|
||
@TableField(value = "role_id") | ||
private Long roleId; | ||
|
||
@TableField(value = "menu_id") | ||
private Long menuId; | ||
|
||
/** | ||
* 创建时间 | ||
*/ | ||
@TableField(value = "create_time") | ||
private LocalDateTime createTime; | ||
|
||
/** | ||
* 创建人 | ||
*/ | ||
@TableField(value = "create_user") | ||
private Long createUser; | ||
|
||
/** | ||
* 修改时间 | ||
*/ | ||
@TableField(value = "update_time") | ||
private LocalDateTime updateTime; | ||
|
||
/** | ||
* 修改人 | ||
*/ | ||
@TableField(value = "update_user") | ||
private Long updateUser; | ||
|
||
/** | ||
* 版本 | ||
*/ | ||
@TableField(value = "version") | ||
@Version | ||
private Long version; | ||
|
||
/** | ||
* 是否删除标记 | ||
*/ | ||
@TableField(value = "is_delete") | ||
@TableLogic | ||
private Integer isDelete; | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
public static final String COL_ID = "id"; | ||
|
||
public static final String COL_ROLE_ID = "role_id"; | ||
|
||
public static final String COL_MENU_ID = "menu_id"; | ||
|
||
public static final String COL_CREATE_TIME = "create_time"; | ||
|
||
public static final String COL_CREATE_USER = "create_user"; | ||
|
||
public static final String COL_UPDATE_TIME = "update_time"; | ||
|
||
public static final String COL_UPDATE_USER = "update_user"; | ||
|
||
public static final String COL_VERSION = "version"; | ||
|
||
public static final String COL_IS_DELETE = "is_delete"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.huifer.idgen.my.service.populator; | ||
|
||
import com.huifer.idgen.my.service.bean.Id; | ||
import com.huifer.idgen.my.service.bean.IdMeta; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* @author: wang | ||
* @description: | ||
*/ | ||
public class LockIdPropulator extends BasePopulator { | ||
|
||
private ReentrantLock lock = new ReentrantLock(); | ||
|
||
@Override | ||
public void populatorId(Id id, IdMeta idMeta) { | ||
lock.lock(); | ||
try { | ||
super.populatorId(id, idMeta); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
package com.huifer.rmi.rpc.client; | ||
|
||
import java.io.IOException; | ||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.net.Socket; | ||
|
||
/** | ||
* <p>Title : MyTCPTransport </p> | ||
* <p>Description : </p> | ||
* | ||
* @author huifer | ||
* @date 2019-06-11 | ||
*/ | ||
public class MyTCPTransport { | ||
|
||
private String host; | ||
private int port; | ||
|
||
public MyTCPTransport(String host, int port) { | ||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
/** | ||
* 创建一个socket | ||
* | ||
* @return | ||
*/ | ||
private Socket newSocket() { | ||
Socket socket; | ||
try { | ||
socket = new Socket(this.host, this.port); | ||
return socket; | ||
} catch (Exception e) { | ||
|
||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 远程信息交互 | ||
* | ||
* @param rpcRequest | ||
* @return | ||
* @throws IOException | ||
*/ | ||
public Object send(RpcRequest rpcRequest) throws IOException { | ||
Socket socket = null; | ||
try { | ||
socket = newSocket(); | ||
ObjectOutputStream oos = new ObjectOutputStream( | ||
socket.getOutputStream()); | ||
|
||
oos.writeObject(rpcRequest); | ||
oos.flush(); | ||
|
||
ObjectInputStream ois = new ObjectInputStream( | ||
socket.getInputStream() | ||
); | ||
Object o = ois.readObject(); | ||
ois.close(); | ||
oos.close(); | ||
return o; | ||
|
||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; | ||
} finally { | ||
if (socket != null) { | ||
socket.close(); | ||
} | ||
} | ||
} | ||
|
||
public String getHost() { | ||
return host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public int getPort() { | ||
return port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
} |
Oops, something went wrong.