增加cloud。等待完善

This commit is contained in:
zhouhao
2017-10-10 18:59:47 +08:00
parent 7cf12cc16d
commit 002cbbc447
10 changed files with 184 additions and 57 deletions

View File

@@ -43,7 +43,7 @@ public interface Authentication extends Serializable {
* //如果权限信息不存在将抛出{@link NoSuchElementException}建议使用下面的方式获取
* Authentication auth=Authentication.current().orElse(null);
* //或者
* Authentication auth=Authentication.current().orElseThrow(AuthorizeException::new);
* Authentication auth=Authentication.current().orElseThrow(UnAuthorizedException::new);
* </pre>
*
* @return 返回Optional对象进行操作

View File

@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hsweb-authorization</artifactId>
<groupId>org.hswebframework.web</groupId>
<version>3.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>hsweb-authorization-cloud</artifactId>
<dependencies>
<dependency>
<groupId>org.hswebframework.web</groupId>
<artifactId>hsweb-authorization-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-core</artifactId>
<version>1.3.1.RELEASE</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,69 @@
package org.hswebframework.web.authorization.cloud.feign;
import org.hswebframework.web.authorization.token.TokenState;
import org.hswebframework.web.authorization.token.UserToken;
import org.hswebframework.web.authorization.token.UserTokenManager;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* @author zhouhao
* @since
*/
@FeignClient(name = "${hsweb.cloud.user-center.name:user-center}")
public interface FeignUserTokenManager extends UserTokenManager {
@Override
@GetMapping("/user-token/token/{token}")
UserToken getByToken(@PathVariable String token);
@Override
@GetMapping("/user-token/user/{userId}")
List<UserToken> getByUserId(@PathVariable String userId);
@Override
@GetMapping("/user-token/user/{userId}/logged")
boolean userIsLoggedIn(@PathVariable String userId);
@Override
@GetMapping("/user-token/token/{token}/logged")
boolean tokenIsLoggedIn(@PathVariable String token);
@Override
@GetMapping("/user-token/user/total")
long totalUser();
@Override
@GetMapping("/user-token/token/total")
long totalToken();
@Override
@GetMapping("/user-token}")
List<UserToken> allLoggedUser();
@Override
@DeleteMapping("/user-token/user/{userId}")
void signOutByUserId(@PathVariable String userId);
@Override
@DeleteMapping("/user-token/token/{token}")
void signOutByToken(@PathVariable String token);
@Override
@PutMapping("/user-token/user/{userId}/{state}")
void changeUserState(@PathVariable String userId, @PathVariable TokenState state);
@Override
@PutMapping("/user-token/token/{token}/{state}")
void changeTokenState(String token, TokenState state);
@Override
@PostMapping("/user-token/{token}/{userId}/{maxInactiveInterval}")
UserToken signIn(@PathVariable String token, @PathVariable String userId, @PathVariable long maxInactiveInterval);
@Override
@GetMapping("/user-token/{token}/touch")
void touch(String token);
}

View File

@@ -0,0 +1,12 @@
package org.hswebframework.web.authorization.cloud.redis;
import org.hswebframework.web.authorization.token.UserTokenManager;
/**
* TODO 完成注释
*
* @author zhouhao
* @since
*/
public abstract class RedisUserTokenManager implements UserTokenManager {
}

View File

@@ -16,6 +16,7 @@
<module>hsweb-authorization-oauth2</module>
<module>hsweb-authorization-basic</module>
<module>hsweb-authorization-jwt</module>
<module>hsweb-authorization-cloud</module>
</modules>

View File

@@ -33,6 +33,7 @@
<module>hsweb-examples-oauth2</module>
<module>hsweb-examples-custom-entity</module>
<module>hsweb-examples-workflow</module>
<module>hsweb-examples-cloud</module>
</modules>
<build>

View File

@@ -23,7 +23,7 @@ import java.util.function.Consumer;
import java.util.stream.Collectors;
/**
* TODO 完成注释
* 默认的事务执行器
*
* @author zhouhao
*/
@@ -51,6 +51,7 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
private volatile boolean running = false;
/* 线程循环开始等待sql进入的时候执行一次,sql进入的时候执行一次,然后唤醒线程开始执行sql */
private CyclicBarrier waitToReady = new CyclicBarrier(2);
private CountDownLatch waitClose = new CountDownLatch(1);
@@ -83,8 +84,10 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
try {
logger.debug("wait transaction {} close", transactionId);
if (!running) {
//先唤醒执行,继续执行任务
waitToReady.await();
}
//等待执行结束
waitClose.await();
} catch (Exception e) {
throw new RuntimeException(e);
@@ -136,18 +139,20 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
if (datasourceId != null) {
DataSourceHolder.switcher().use(datasourceId);
}
//开启事务
transactionStatus = transactionTemplate.getTransactionManager().getTransaction(transactionTemplate);
if (sqlRequestExecutor == null) {
buildDefaultSqlRequestExecutor();
}
while (!shutdown) {
logger.debug("wait sql execute request {}", transactionId);
waitToReady.await();
waitToReady.reset();
waitToReady.await();//等待有新的sql进来
waitToReady.reset();//重置,下一次循环继续等待
//执行sql
doExecute();
}
} catch (Exception e) {
rollback();
rollback();//回滚
logger.error("execute sql error {}", transactionId, e);
} finally {
try {
@@ -158,6 +163,7 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
logger.debug("Roll Back transaction {}", transactionId);
transactionTemplate.getTransactionManager().rollback(transactionStatus);
}
//结束事务
waitClose.countDown();
} finally {
DataSourceHolder.switcher().reset();
@@ -171,15 +177,18 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
running = true;
logger.debug("start execute sql {}", transactionId);
try {
List<SqlExecuteResult> requests = execution.request.getSql().stream()
List<SqlExecuteResult> requests = execution.request.getSql()
.stream()
.map(sqlInfo -> {
try {
//执行sql
return sqlRequestExecutor.apply(sqlExecutor, sqlInfo);
} catch (SQLException e) {
throw new RuntimeException(e);
}
})
.collect(Collectors.toList());
//通过回调返回执行结果
execution.callback.accept(requests);
} catch (Exception e) {
rollback();
@@ -194,9 +203,11 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
if (shutdown) {
throw new UnsupportedOperationException("transaction is close");
}
//执行倒计时,执行sql是异步的,通过此方式等待sql执行完毕
CountDownLatch countDownLatch = new CountDownLatch(1);
List<SqlExecuteResult> results = new ArrayList<>();
//异常信息
Exception[] exceptions = new Exception[1];
Execution execution = new Execution();
@@ -213,9 +224,12 @@ public class DefaultLocalTransactionExecutor implements TransactionExecutor {
logger.debug("submit sql execute job {}", transactionId);
executionQueue.add(execution);
try {
//当前没有在执行sql,说明现在正在等待新的sql进入,唤醒之
if (!running)
waitToReady.await();
//等待sql执行完毕
countDownLatch.await();
//判断是否有异常
Exception exception;
if ((exception = exceptions[0]) != null) {
if (exception instanceof RuntimeException) {

View File

@@ -95,6 +95,7 @@ public class SimpleDatabaseManagerService implements DatabaseManagerService {
@Override
public Map<ObjectMetadata.ObjectType, List<ObjectMetadata>> getMetas(String datasourceId) {
return null;
}
}

View File

@@ -6,8 +6,6 @@ import org.hswebframework.web.database.manager.SqlExecuteResult;
import java.util.List;
/**
* TODO 完成注释
*
* @author zhouhao
*/
public interface TransactionExecutor extends Runnable {

97
pom.xml
View File

@@ -43,8 +43,7 @@
<packaging>pom</packaging>
<name>${project.groupId}:${project.artifactId}</name>
<name>hsweb framework</name>
<url>http://www.hswebframework.org</url>
<description>hsweb (haʊs wɛb) 是一个用于快速搭建企业后台管理系统的基础项目,集成一揽子便捷功能如:通用增删改查,在线代码生成,权限管理(可控制到列和行),动态多数据源分布式事务,动态脚本,动态定时任务,在线数据库维护等等</description>
@@ -160,6 +159,18 @@
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>sonatype-releases</id>
<name>sonatype repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
</repository>
<snapshotRepository>
<id>sonatype-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
</profile>
</profiles>
@@ -190,36 +201,36 @@
</plugin>
<!--<plugin>-->
<!--<artifactId>maven-source-plugin</artifactId>-->
<!--<version>2.4</version>-->
<!--<configuration>-->
<!--<attach>true</attach>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>compile</phase>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--<artifactId>maven-source-plugin</artifactId>-->
<!--<version>2.4</version>-->
<!--<configuration>-->
<!--<attach>true</attach>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>compile</phase>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<!--<plugin>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-javadoc-plugin</artifactId>-->
<!--<version>2.9.1</version>-->
<!--<configuration>-->
<!--<aggregate>true</aggregate>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>deploy</phase>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--<groupId>org.apache.maven.plugins</groupId>-->
<!--<artifactId>maven-javadoc-plugin</artifactId>-->
<!--<version>2.9.1</version>-->
<!--<configuration>-->
<!--<aggregate>true</aggregate>-->
<!--</configuration>-->
<!--<executions>-->
<!--<execution>-->
<!--<phase>deploy</phase>-->
<!--<goals>-->
<!--<goal>jar</goal>-->
<!--</goals>-->
<!--</execution>-->
<!--</executions>-->
<!--</plugin>-->
<plugin>
@@ -375,29 +386,17 @@
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<!--<distributionManagement>-->
<!--<repository>-->
<!--<id>releases</id>-->
<!--<name>Nexus Release Repository</name>-->
<!--<url>http://nexus.hsweb.me/content/repositories/releases/</url>-->
<!--</repository>-->
<!--<snapshotRepository>-->
<!--<id>snapshots</id>-->
<!--<name>Nexus Snapshot Repository</name>-->
<!--<url>http://nexus.hsweb.me/content/repositories/snapshots/</url>-->
<!--</snapshotRepository>-->
<!--</distributionManagement>-->
<distributionManagement>
<repository>
<id>sonatype-releases</id>
<name>sonatype repository</name>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://nexus.hsweb.me/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>sonatype-snapshots</id>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
<url>http://nexus.hsweb.me/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
@@ -411,9 +410,9 @@
</snapshots>
</pluginRepository>
<pluginRepository>
<id>aliyun-nexus</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<id>aliyun-nexus</id>
<name>aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</project>