├── LICENSE ├── README.md ├── live-auth ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── auth │ │ ├── AuthApplication.java │ │ ├── cache │ │ ├── ShiroRedisCache.java │ │ └── ShiroRedisCacheManager.java │ │ ├── config │ │ ├── ShiroConfig.java │ │ └── UserShiroRealm.java │ │ ├── controller │ │ └── AuthController.java │ │ ├── interceptor │ │ ├── AccessLogInterceptor.java │ │ └── WebInterceptor.java │ │ ├── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ │ └── service │ │ ├── AuthService.java │ │ └── impl │ │ └── AuthServiceImpl.java │ └── resources │ ├── application.yml │ ├── banner.txt │ ├── bootstrap.yml │ └── logback-spring.xml ├── live-common ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── easy │ └── live │ └── streaming │ └── common │ ├── config │ └── Constants.java │ ├── exception │ ├── Exceptions.java │ └── GlobalException.java │ └── util │ ├── Digests.java │ ├── Encodes.java │ ├── HttpClientUtil.java │ ├── Reflections.java │ ├── Servlets.java │ ├── SysDate.java │ └── jpa │ ├── Collections3.java │ ├── DynamicSpecifications.java │ ├── PageUtil.java │ ├── SearchFilter.java │ └── SerializeUtils.java ├── live-config ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── gateway │ │ ├── ConfigApplication.java │ │ └── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ └── resources │ ├── banner.txt │ ├── bootstrap.yml │ └── config │ ├── dev │ ├── application.yml │ ├── gateway-server-dev.yml │ ├── user-server-dev.yml │ └── video-server-dev.yml │ ├── prod │ └── application-prod.yml │ └── test │ └── application-test.yml ├── live-data ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── easy │ └── live │ └── streaming │ └── data │ ├── bean │ └── BaseOutput.java │ ├── cache │ ├── SimpleCacheService.java │ ├── ThreadLocalUserCache.java │ └── UserCache.java │ ├── config │ └── SwaggerConfig.java │ ├── entity │ ├── AbstractEntity.java │ ├── BaseEntity.java │ ├── Entity.java │ ├── user │ │ ├── LiveUser.java │ │ ├── SystemDic.java │ │ ├── SystemMenu.java │ │ ├── SystemRole.java │ │ └── SystemUser.java │ └── video │ │ ├── VideoLiveCate.java │ │ ├── VideoLiveRoom.java │ │ ├── VideoLiveRoomTag.java │ │ └── VideoLiveTag.java │ ├── filter │ └── AcquireUserInfoInterceptor.java │ ├── repository │ ├── BaseRepository.java │ ├── user │ │ ├── LiveUserRepository.java │ │ ├── SystemDicRepository.java │ │ ├── SystemMenuRepository.java │ │ ├── SystemRoleRepository.java │ │ └── SystemUserRepository.java │ └── video │ │ ├── VideoLiveCateRepository.java │ │ ├── VideoLiveRoomRepository.java │ │ ├── VideoLiveRoomTagRepository.java │ │ └── VideoLiveTagRepository.java │ └── service │ ├── BaseService.java │ ├── user │ ├── LiveUserService.java │ ├── SystemDicService.java │ ├── SystemMenuService.java │ ├── SystemRoleService.java │ └── SystemUserService.java │ └── video │ ├── VideoLiveCateService.java │ ├── VideoLiveRoomService.java │ ├── VideoLiveRoomTagService.java │ └── VideoLiveTagService.java ├── live-dependencies └── pom.xml ├── live-gateway ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── gateway │ │ ├── GatewayApplication.java │ │ ├── controller │ │ └── HandleErrorController.java │ │ ├── filter │ │ ├── AccessLogFilter.java │ │ ├── HandleZuulExceptionFilter.java │ │ └── LoginFilter.java │ │ ├── handler │ │ └── GlobalExceptionHandler.java │ │ └── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ └── resources │ ├── application.yml │ ├── banner.txt │ ├── bootstrap.yml │ └── logback-spring.xml ├── live-registry ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── registry │ │ ├── RegistryApplication.java │ │ └── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ └── resources │ ├── application.yml │ ├── banner.txt │ └── logback.xml ├── live-servants ├── pom.xml └── src │ └── main │ └── java │ └── com │ └── easy │ └── live │ └── streaming │ └── servants │ ├── api │ ├── auth │ │ ├── fallback │ │ │ └── AuthServantFallback.java │ │ └── servant │ │ │ └── AuthServant.java │ ├── user │ │ ├── fallback │ │ │ └── UserServantFallback.java │ │ └── servant │ │ │ └── UserServant.java │ └── video │ │ ├── fallback │ │ └── VideoRoomServantFallback.java │ │ └── servant │ │ └── VideoRoomServant.java │ └── protocol │ ├── input │ ├── user │ │ └── UserInput.java │ └── video │ │ └── VideoRoomInput.java │ └── output │ ├── user │ └── UserOutput.java │ └── video │ └── VideoRoomOutput.java ├── live-user ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── user │ │ ├── UserApplication.java │ │ ├── controller │ │ └── UserController.java │ │ ├── interceptor │ │ ├── AccessLogInterceptor.java │ │ └── WebInterceptor.java │ │ ├── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ │ └── service │ │ ├── UserService.java │ │ └── impl │ │ └── UserServiceImpl.java │ └── resources │ ├── application.yml │ ├── banner.txt │ ├── bootstrap.yml │ └── logback-spring.xml ├── live-video ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── easy │ │ └── live │ │ └── streaming │ │ └── video │ │ ├── VideoApplication.java │ │ ├── controller │ │ └── VideoController.java │ │ ├── inteceptor │ │ └── WebInterceptor.java │ │ ├── intergration │ │ ├── ApplicationDestroy.java │ │ ├── AutoConfigure.java │ │ └── StartupRunner.java │ │ └── service │ │ ├── VideoService.java │ │ └── impl │ │ └── VideoServiceImpl.java │ └── resources │ ├── application.yml │ ├── banner.txt │ └── bootstrap.yml └── pom.xml /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 liangfuz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring cloud实现直播搭建 2 | 3 | 目前服务包括注册中心,配置中心,网关服务,鉴权服务,用户服务以及视频直播间服务 4 | 注册中心使用eureka,网关使用zuul,鉴权使用shiro,ORM框架采用hibernate 5 | 直播采用rtmp协议(具体搭建请看我的博客Nginx+RTMP搭建流媒体直播服务器 6 | 7 | PS:有空逐步集成服务治理zipkin,elk,docker打包等 8 | ``` 9 | ├─ live-common //基础工具 10 | ├─ live-config //配置中心服务 port:8002 11 | │ │ └─ resources 12 | │ │ └─ config //本地配置 13 | │ │ ├─ dev //开发环境 14 | │ │ │ ├─ application.yml 15 | │ │ │ ├─ gateway-server-dev.yml 16 | │ │ │ ├─ ... 17 | │ │ ├─ prod 18 | │ │ │ └─ ... 19 | │ │ └─ test 20 | │ │ └─ ... 21 | ├─ live-data //数据模型 22 | │ │ ├─ bean 23 | │ │ ├─ cache 24 | │ │ ├─ config 25 | │ │ ├─ entity 26 | │ │ ├─ repository 27 | │ │ └─ service 28 | ├─ live-dependencies //公共依赖包 29 | │ └─ pom.xml 30 | ├─ live-gateway //网关服务 port:8003 31 | │ │ ├─ controller //全局错误处理 32 | │ │ │ └─ HandleErrorController.java 33 | │ │ └─ filter //网关过滤器 34 | │ │ ├─ AccessLogFilter.java 35 | │ │ ├─ HandleZuulExceptionFilter.java 36 | │ │ └─ LoginFilter.java 37 | ├─ live-registry //注册中心服务 port:8001 38 | ├─ live-auth //鉴权服务 port:8010 39 | ├─ live-servants //API模块 40 | │ │ ├─ user 41 | │ │ ├─ video 42 | │ │ └─... 43 | ├─ live-user //用户服务 port:8011 44 | └─ live-video //视频直播服务 port:8012 45 | ``` 46 | 47 | 启动步骤,修改数据源配置,redis配置之后按注册中心,配置中心,网关,鉴权,用户以及视频服务启动(先启动注册而后配置中心,之后随便启动其他的) 48 | -------------------------------------------------------------------------------- /live-auth/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.easy 13 | live-auth 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-eureka 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-feign 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-config 27 | 28 | 29 | mysql 30 | mysql-connector-java 31 | 32 | 33 | org.apache.shiro 34 | shiro-core 35 | 36 | 37 | org.apache.shiro 38 | shiro-spring 39 | 40 | 41 | com.easy 42 | live-servants 43 | 44 | 45 | com.easy 46 | live-data 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.springframework.boot 54 | spring-boot-maven-plugin 55 | 1.5.2.RELEASE 56 | 57 | 58 | 59 | repackage 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/AuthApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.autoconfigure.domain.EntityScan; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.cloud.client.SpringCloudApplication; 7 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 8 | import org.springframework.cloud.netflix.feign.EnableFeignClients; 9 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 10 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 11 | import org.springframework.context.annotation.ComponentScan; 12 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 13 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 14 | 15 | /** 16 | * @Description:鉴权服务 17 | * @Author: zhangliangfu 18 | * @Create on: 2019-08-14 14:02 19 | */ 20 | 21 | @ComponentScan(basePackages={"com.easy"}) 22 | @EnableJpaRepositories(basePackages = "com.easy") 23 | @EntityScan(basePackages = "com.easy") 24 | @SpringBootApplication 25 | @EnableDiscoveryClient 26 | @EnableSwagger2 27 | public class AuthApplication { 28 | public static void main(String[] args) { 29 | new SpringApplicationBuilder(AuthApplication.class).web(true).run(args); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/cache/ShiroRedisCache.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.cache; 2 | 3 | import com.easy.live.streaming.common.util.jpa.SerializeUtils; 4 | import com.google.common.collect.Sets; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.apache.commons.collections.CollectionUtils; 7 | import org.apache.shiro.cache.Cache; 8 | import org.apache.shiro.cache.CacheException; 9 | import org.springframework.data.redis.core.RedisTemplate; 10 | import org.springframework.data.redis.core.ValueOperations; 11 | 12 | import java.util.*; 13 | 14 | /** 15 | * Description: Shiro缓存 16 | * Author: zhangliangfu 17 | * Create on: 2019-07-15 15:13 18 | */ 19 | 20 | @Slf4j 21 | public class ShiroRedisCache implements Cache { 22 | 23 | private RedisTemplate redisTemplate; 24 | private String prefix = "SHIRO_CACHE:"; 25 | 26 | public ShiroRedisCache(RedisTemplate redisTemplate) { 27 | this.redisTemplate = redisTemplate; 28 | } 29 | 30 | public ShiroRedisCache(RedisTemplate redisTemplate, String prefix) { 31 | this(redisTemplate); 32 | this.prefix = prefix; 33 | } 34 | 35 | 36 | public V get(K key) throws CacheException { 37 | if(log.isDebugEnabled()) { 38 | log.debug("Key: {}", key); 39 | } 40 | if(key == null) { 41 | return null; 42 | } 43 | 44 | byte[] bkey = getByteKey(key); 45 | return redisTemplate.opsForValue().get(bkey); 46 | } 47 | 48 | 49 | public V put(K key, V value) throws CacheException { 50 | if(log.isDebugEnabled()) { 51 | log.debug("Key: {}, value: {}", key, value); 52 | } 53 | 54 | if(key == null || value == null) { 55 | return null; 56 | } 57 | 58 | byte[] bkey = getByteKey(key); 59 | redisTemplate.opsForValue().set(bkey, value); 60 | return value; 61 | } 62 | 63 | 64 | public V remove(K key) throws CacheException { 65 | if(log.isDebugEnabled()) { 66 | log.debug("Key: {}", key); 67 | } 68 | 69 | if(key == null) { 70 | return null; 71 | } 72 | 73 | byte[] bkey = getByteKey(key); 74 | ValueOperations vo = redisTemplate.opsForValue(); 75 | V value = vo.get(bkey); 76 | redisTemplate.delete(bkey); 77 | return value; 78 | } 79 | 80 | 81 | public void clear() throws CacheException { 82 | byte[] bkey = (prefix+"*").getBytes(); 83 | Set set = redisTemplate.keys(bkey); 84 | if(!CollectionUtils.isEmpty(set)) { 85 | for(byte[] key:set) { 86 | redisTemplate.delete(key); 87 | } 88 | } 89 | } 90 | 91 | public int size() { 92 | byte[] bkey = (prefix+"*").getBytes(); 93 | Set set = redisTemplate.keys(bkey); 94 | return set.size(); 95 | } 96 | 97 | public Set keys() { 98 | byte[] bkey = (prefix+"*").getBytes(); 99 | Set set = redisTemplate.keys(bkey); 100 | Set result = Sets.newHashSet(); 101 | 102 | if(CollectionUtils.isEmpty(set)) { 103 | return Collections.emptySet(); 104 | } 105 | 106 | for(byte[] key: set) { 107 | result.add((K)key); 108 | } 109 | return result; 110 | } 111 | 112 | 113 | public Collection values() { 114 | Set keys = keys(); 115 | List values = new ArrayList(keys.size()); 116 | for(K k: keys) { 117 | byte[] bkey = getByteKey(k); 118 | values.add(redisTemplate.opsForValue().get(bkey)); 119 | } 120 | return values; 121 | } 122 | 123 | private byte[] getByteKey(K key){ 124 | if(key instanceof String){ 125 | String preKey = this.prefix + key; 126 | return preKey.getBytes(); 127 | }else{ 128 | return SerializeUtils.serialize(key); 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/cache/ShiroRedisCacheManager.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.cache; 2 | 3 | import org.apache.shiro.cache.AbstractCacheManager; 4 | import org.apache.shiro.cache.Cache; 5 | import org.apache.shiro.cache.CacheException; 6 | import org.springframework.data.redis.core.RedisTemplate; 7 | 8 | /** 9 | * Description: Shiro缓存管理 10 | * Author: zhangliangfu 11 | * Create on: 2019-07-15 15:28 12 | */ 13 | public class ShiroRedisCacheManager extends AbstractCacheManager { 14 | private RedisTemplate redisTemplate; 15 | 16 | public ShiroRedisCacheManager(RedisTemplate redisTemplate) { 17 | this.redisTemplate = redisTemplate; 18 | } 19 | 20 | @Override 21 | protected Cache createCache(String name) throws CacheException { 22 | return new ShiroRedisCache(redisTemplate, name); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/config/ShiroConfig.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.config; 2 | 3 | import com.easy.live.streaming.auth.cache.ShiroRedisCacheManager; 4 | import com.google.common.collect.Maps; 5 | import org.apache.shiro.mgt.SecurityManager; 6 | import org.apache.shiro.spring.LifecycleBeanPostProcessor; 7 | import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; 8 | import org.apache.shiro.spring.web.ShiroFilterFactoryBean; 9 | import org.apache.shiro.web.mgt.DefaultWebSecurityManager; 10 | import org.springframework.boot.web.servlet.FilterRegistrationBean; 11 | import org.springframework.context.annotation.Bean; 12 | import org.springframework.context.annotation.Configuration; 13 | import org.springframework.context.annotation.DependsOn; 14 | import org.springframework.data.redis.core.RedisTemplate; 15 | import org.springframework.web.filter.DelegatingFilterProxy; 16 | 17 | import javax.servlet.DispatcherType; 18 | import javax.servlet.Filter; 19 | import java.util.Map; 20 | 21 | 22 | /** 23 | * @Description: Shiro配置 24 | * @Author: zhangliangfu 25 | * @Create on: 2019-07-12 13:42 26 | */ 27 | @Configuration 28 | public class ShiroConfig { 29 | 30 | @Bean 31 | public FilterRegistrationBean filterRegistrationBean() { 32 | FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); 33 | filterRegistration.setFilter(new DelegatingFilterProxy("shiroFilterFactoryBean")); 34 | filterRegistration.setEnabled(true); 35 | filterRegistration.addUrlPatterns("/*"); 36 | filterRegistration.setDispatcherTypes(DispatcherType.REQUEST); 37 | return filterRegistration; 38 | } 39 | 40 | @Bean(name = "shiroFilterFactoryBean") 41 | public ShiroFilterFactoryBean shiroFilterFactoryBean(ShiroRedisCacheManager shiroRedisCacheManager) { 42 | ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); 43 | shiroFilterFactoryBean.setSecurityManager(securityManager(shiroRedisCacheManager)); 44 | Map filters = Maps.newHashMap(); 45 | shiroFilterFactoryBean.setFilters(filters); 46 | Map chains = Maps.newHashMap(); 47 | chains.put("/**", "authc,perms"); 48 | chains.put("/open/**", "anon"); 49 | chains.put("/swagger-resources/**", "anon"); 50 | chains.put("/swagger-ui.html", "anon"); 51 | chains.put("/v2/**", "anon"); 52 | chains.put("/webjars/**", "anon"); 53 | shiroFilterFactoryBean.setFilterChainDefinitionMap(chains); 54 | return shiroFilterFactoryBean; 55 | } 56 | 57 | @Bean(name="securityManager") 58 | public DefaultWebSecurityManager securityManager(ShiroRedisCacheManager shiroRedisCacheManager) { 59 | DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); 60 | securityManager.setRealm(userShiroRealm()); 61 | securityManager.setCacheManager(shiroRedisCacheManager); 62 | return securityManager; 63 | } 64 | 65 | @Bean 66 | public UserShiroRealm userShiroRealm() { 67 | UserShiroRealm userShiroRealm = new UserShiroRealm(); 68 | return userShiroRealm; 69 | } 70 | 71 | @Bean(name = "shiroRedisCacheManager") 72 | @DependsOn(value = {"lifecycleBeanPostProcessor","redisTemplate"}) 73 | public ShiroRedisCacheManager shiroRedisCacheManager(RedisTemplate redisTemplate){ 74 | ShiroRedisCacheManager shiroRedisCacheManager = new ShiroRedisCacheManager(redisTemplate); 75 | return shiroRedisCacheManager; 76 | } 77 | 78 | @Bean 79 | public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { 80 | AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); 81 | authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); 82 | return authorizationAttributeSourceAdvisor; 83 | } 84 | 85 | @Bean 86 | public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { 87 | return new LifecycleBeanPostProcessor(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/config/UserShiroRealm.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.config; 2 | 3 | import com.easy.live.streaming.data.entity.user.LiveUser; 4 | import com.easy.live.streaming.data.service.user.LiveUserService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.apache.shiro.authc.*; 7 | import org.apache.shiro.authz.AuthorizationInfo; 8 | import org.apache.shiro.authz.SimpleAuthorizationInfo; 9 | import org.apache.shiro.realm.AuthorizingRealm; 10 | import org.apache.shiro.subject.PrincipalCollection; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.stereotype.Component; 13 | 14 | /** 15 | * @Description:shiro realm 16 | * @Author: zhangliangfu 17 | * @Create on: 2019-07-12 13:42 18 | */ 19 | 20 | @Slf4j 21 | @Component 22 | public class UserShiroRealm extends AuthorizingRealm { 23 | 24 | @Autowired 25 | private LiveUserService liveUserService; 26 | 27 | @Override 28 | protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { 29 | log.debug("用户权限认证"); 30 | return new SimpleAuthorizationInfo(); 31 | } 32 | 33 | @Override 34 | protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { 35 | log.debug("用户身份认证"); 36 | UsernamePasswordToken userToken = (UsernamePasswordToken) token; 37 | String username = userToken.getUsername(); 38 | LiveUser user = liveUserService.findLiveUserByName(username); 39 | if (user!=null){ 40 | return new SimpleAuthenticationInfo(username, user.getPassword(), getName()); 41 | }else { 42 | log.error("查询用户出错或用户不存在"); 43 | return null; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/controller/AuthController.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.controller; 2 | 3 | import com.easy.live.streaming.auth.service.AuthService; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.data.cache.UserCache; 6 | import com.easy.live.streaming.servants.api.auth.servant.AuthServant; 7 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.web.bind.annotation.RestController; 10 | 11 | /** 12 | * Description: 鉴权控制器 13 | * Author: zhangliangfu 14 | * Create on: 2019-08-14 14:48 15 | */ 16 | 17 | @RestController 18 | public class AuthController implements AuthServant { 19 | 20 | @Autowired 21 | private AuthService authService; 22 | 23 | @Override 24 | public BaseOutput login(UserInput input) { 25 | return authService.login(input); 26 | } 27 | 28 | @Override 29 | public BaseOutput logout() { 30 | return authService.logout(); 31 | } 32 | 33 | @Override 34 | public BaseOutput isLogin() { 35 | return authService.isLogin(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/interceptor/AccessLogInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.interceptor; 2 | 3 | import com.easy.live.streaming.data.cache.ThreadLocalUserCache; 4 | import com.easy.live.streaming.data.cache.UserCache; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | /** 12 | * Description: 访问记录拦截器 13 | * Author: zhangliangfu 14 | * Create on: 2019-07-31 13:49 15 | */ 16 | @Slf4j 17 | public class AccessLogInterceptor extends HandlerInterceptorAdapter { 18 | 19 | @Override 20 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ 21 | String sessionId = request.getHeader("sessionId"); 22 | UserCache userCache = new UserCache(); 23 | userCache.setSessionId(sessionId); 24 | ThreadLocalUserCache.setUserCache(userCache); 25 | return true; 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/interceptor/WebInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.interceptor; 2 | 3 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 8 | 9 | /** 10 | * Description: WEB拦截器 11 | * Author: zhangliangfu 12 | * Create on: 2019-07-26 14:57 13 | */ 14 | 15 | @Configuration 16 | @AutoConfigureAfter(WebMvcConfigurerAdapter.class) 17 | public class WebInterceptor extends WebMvcConfigurerAdapter { 18 | 19 | @Bean 20 | public AccessLogInterceptor accessLogInterceptor(){ 21 | return new AccessLogInterceptor(); 22 | } 23 | 24 | @Override 25 | public void addInterceptors(InterceptorRegistry registry) { 26 | registry.addInterceptor(accessLogInterceptor()).addPathPatterns("/**") 27 | .excludePathPatterns("/swagger-resources/**"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [Auth] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.CommandLineRunner; 7 | 8 | public class StartupRunner implements CommandLineRunner { 9 | 10 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 11 | 12 | @Value("${spring.profiles.active:dev}") 13 | private String env; 14 | 15 | @Value("${spring.cloud.config.profile:dev}") 16 | private String configEnv; 17 | 18 | @Override 19 | public void run(String... args) { 20 | logger.info("spring.profiles.active:{}",env); 21 | logger.info("spring.cloud.config.profile:{}",configEnv); 22 | logger.info("鉴权服务启动成功"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/service/AuthService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.service; 2 | 3 | import com.easy.live.streaming.data.bean.BaseOutput; 4 | import com.easy.live.streaming.data.cache.UserCache; 5 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 6 | 7 | /** 8 | * Description: 鉴权service 9 | * Author: zhangliangfu 10 | * Create on: 2019-08-14 14:49 11 | */ 12 | public interface AuthService { 13 | 14 | /** 15 | * 用户登录 16 | * 17 | * @param input 18 | * @return 19 | */ 20 | BaseOutput login(UserInput input); 21 | 22 | /** 23 | * 用户登录 24 | * 25 | * @return 26 | */ 27 | BaseOutput logout(); 28 | 29 | /** 30 | * 用户是否登陆登录 31 | * 32 | * @return 33 | */ 34 | BaseOutput isLogin(); 35 | } 36 | -------------------------------------------------------------------------------- /live-auth/src/main/java/com/easy/live/streaming/auth/service/impl/AuthServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.auth.service.impl; 2 | 3 | import com.easy.live.streaming.auth.service.AuthService; 4 | import com.easy.live.streaming.common.config.Constants; 5 | import com.easy.live.streaming.data.bean.BaseOutput; 6 | import com.easy.live.streaming.data.cache.SimpleCacheService; 7 | import com.easy.live.streaming.data.cache.ThreadLocalUserCache; 8 | import com.easy.live.streaming.data.cache.UserCache; 9 | import com.easy.live.streaming.data.entity.user.LiveUser; 10 | import com.easy.live.streaming.data.service.user.LiveUserService; 11 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 12 | import org.apache.shiro.SecurityUtils; 13 | import org.apache.shiro.authc.AuthenticationException; 14 | import org.apache.shiro.authc.UsernamePasswordToken; 15 | import org.apache.shiro.subject.Subject; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | 19 | /** 20 | * Description: 鉴权Service实现 21 | * Author: zhangliangfu 22 | * Create on: 2019-08-14 14:49 23 | */ 24 | @Service 25 | public class AuthServiceImpl implements AuthService { 26 | 27 | @Autowired 28 | private SimpleCacheService simpleCacheService; 29 | @Autowired 30 | private LiveUserService userService; 31 | 32 | /** 33 | * 用户登录 34 | * 35 | * @param input 36 | * @return 37 | */ 38 | @Override 39 | public BaseOutput login(UserInput input) { 40 | UsernamePasswordToken upt = new UsernamePasswordToken(input.getName(), input.getPassword()); 41 | Subject subject = SecurityUtils.getSubject(); 42 | try { 43 | subject.login(upt); 44 | String sessionId = ThreadLocalUserCache.getUserCache().getSessionId(); 45 | UserCache userCache = new UserCache(); 46 | LiveUser user = userService.findLiveUserByName(input.getName()); 47 | userCache.setUserId(user.getId()); 48 | simpleCacheService.add(Constants.LOGIN_CACHE+sessionId, userCache, Constants.LOGIN_CACHE_TIME); 49 | } catch (AuthenticationException e) { 50 | e.printStackTrace(); 51 | return new BaseOutput(Constants.RetMsg.FAIL.code,"用户名或密码不正确!"); 52 | } 53 | return new BaseOutput(Constants.RetMsg.SUCCESS.code,"登录成功!"); 54 | } 55 | 56 | /** 57 | * 用户登出 58 | * 59 | * @return 60 | */ 61 | @Override 62 | public BaseOutput logout() { 63 | Subject subject = SecurityUtils.getSubject(); 64 | try { 65 | subject.logout(); 66 | String sessionId = ThreadLocalUserCache.getUserCache().getSessionId(); 67 | simpleCacheService.del(Constants.LOGIN_CACHE+sessionId); 68 | } catch (AuthenticationException e) { 69 | e.printStackTrace(); 70 | return new BaseOutput(Constants.RetMsg.FAIL.code,"登出失败!"); 71 | } 72 | return new BaseOutput(Constants.RetMsg.SUCCESS.code,"登出成功!"); 73 | } 74 | 75 | /** 76 | * 用户登出 77 | * 78 | * @return 79 | */ 80 | @Override 81 | public BaseOutput isLogin() { 82 | Subject subject = SecurityUtils.getSubject(); 83 | boolean authenticated = subject.isAuthenticated(); 84 | if (authenticated){ 85 | String sessionId = subject.getSession().getId().toString(); 86 | UserCache userCache = simpleCacheService.get(sessionId, UserCache.class); 87 | return new BaseOutput(Constants.RetMsg.LOGIN_YES.code, Constants.RetMsg.LOGIN_YES.msg, userCache); 88 | }else { 89 | return new BaseOutput(Constants.RetMsg.LOGIN_NO.code, Constants.RetMsg.LOGIN_NO.msg); 90 | } 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /live-auth/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: auth-server 4 | 5 | server: 6 | port: 8010 -------------------------------------------------------------------------------- /live-auth/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-auth/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | easy: 2 | eurekaServer: http://live.registry.com:8001/eureka/ 3 | 4 | eureka: 5 | instance: 6 | lease-expiration-duration-in-seconds: 15 7 | lease-renewal-interval-in-seconds: 5 8 | preferIpAddress: true 9 | hostname: live.registry.com 10 | metadataMap: 11 | cluster: MAIN 12 | client: 13 | registryFetchIntervalSeconds: 5 14 | serviceUrl: 15 | defaultZone: ${easy.eurekaServer} 16 | 17 | spring: 18 | cloud: 19 | config: 20 | profile: dev 21 | discovery: 22 | enabled: true 23 | serviceId: config-server 24 | 25 | 26 | -------------------------------------------------------------------------------- /live-auth/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ${FILE_LOG_PATTERN} 13 | UTF-8 14 | 15 | 16 | 17 | ${LOG_HOME}/gateway-server-%d{yyyy-MM-dd}.%i.log 18 | 19 | 20MB 20 | 60 21 | 20GB 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /live-common/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.easy 13 | live-common 14 | 15 | 16 | 17 | 18 | org.projectlombok 19 | lombok 20 | 21 | 22 | org.slf4j 23 | slf4j-api 24 | 25 | 26 | org.apache.shiro 27 | shiro-core 28 | 29 | 30 | com.google.guava 31 | guava 32 | 33 | 34 | org.springframework.data 35 | spring-data-redis 36 | 37 | 38 | org.springframework.session 39 | spring-session 40 | 41 | 42 | redis.clients 43 | jedis 44 | 45 | 46 | org.apache.commons 47 | commons-lang3 48 | 49 | 50 | org.springframework.boot 51 | spring-boot-starter-data-jpa 52 | 53 | 54 | org.apache.tomcat.embed 55 | tomcat-embed-core 56 | 57 | 58 | commons-codec 59 | commons-codec 60 | 61 | 62 | org.apache.httpcomponents 63 | httpcore 64 | 65 | 66 | org.apache.httpcomponents 67 | httpclient 68 | 69 | 70 | com.aliyun.oss 71 | aliyun-sdk-oss 72 | 73 | 74 | org.springframework 75 | spring-webmvc 76 | 77 | 78 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/config/Constants.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.config; 2 | 3 | /** 4 | * Created by ZLF on 2017/6/21. 5 | */ 6 | public class Constants { 7 | 8 | public static final String LIVE_URL_PREFIX = "rtmp://192.168.10.110:1935/live/room"; 9 | 10 | public static final String LOGIN_CACHE = "LOGIN_CACHE_"; 11 | public static final int LOGIN_CACHE_TIME = 600; //十分钟 12 | 13 | public enum RetMsg{ 14 | SUCCESS(200, "操作成功"), 15 | 16 | FAIL(500, "操作失败"), 17 | LOGIN_YES(401, "已登录"), 18 | LOGIN_NO(402, "未登录"), 19 | 20 | EXCEPTION_LOGIN(301, "未登录异常"); 21 | 22 | public Integer code; 23 | 24 | public String msg; 25 | 26 | RetMsg(Integer retCode, String retMsg) { 27 | this.code = retCode; 28 | this.msg = retMsg; 29 | } 30 | 31 | public Integer getCode() { 32 | return code; 33 | } 34 | 35 | public String getMsg() { 36 | return msg; 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/exception/Exceptions.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.exception; 2 | import java.io.StringWriter; 3 | 4 | /** 5 | * 关于异常的工具类. 6 | * 7 | */ 8 | public class Exceptions 9 | { 10 | 11 | private Exceptions() 12 | { 13 | super(); 14 | } 15 | 16 | /** 17 | * 将CheckedException转换为UncheckedException. 18 | */ 19 | public static RuntimeException unchecked(Exception e) 20 | { 21 | if (e instanceof RuntimeException) 22 | { 23 | return (RuntimeException)e; 24 | } 25 | else 26 | { 27 | return new RuntimeException(e); 28 | } 29 | } 30 | 31 | /** 32 | * 将ErrorStack转化为String. 33 | */ 34 | public static String getStackTraceAsString(Exception e) 35 | { 36 | StringWriter stringWriter = new StringWriter(); 37 | return stringWriter.toString(); 38 | } 39 | 40 | /** 41 | * 判断异常是否由某些底层的异常引起. 42 | */ 43 | public static boolean isCausedBy(Exception ex, Class... causeExceptionClasses) 44 | { 45 | Throwable cause = ex; 46 | while (cause != null) 47 | { 48 | for (Class causeClass : causeExceptionClasses) 49 | { 50 | if (causeClass.isInstance(cause)) 51 | { 52 | return true; 53 | } 54 | } 55 | cause = cause.getCause(); 56 | } 57 | return false; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/exception/GlobalException.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.exception; 2 | 3 | import lombok.Getter; 4 | import lombok.Setter; 5 | 6 | /** 7 | * Description: 全局异常 8 | * Author: zhangliangfu 9 | * Create on: 2019-08-01 14:57 10 | */ 11 | @Getter 12 | @Setter 13 | public class GlobalException extends Exception { 14 | private Integer code; 15 | private String message; 16 | 17 | public GlobalException(Integer code, String message){ 18 | this.code = code; 19 | this.message = message; 20 | } 21 | 22 | @Override 23 | public String getMessage(){ 24 | return message; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/Digests.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2005-2014 leelong.cn 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | */ 6 | package com.easy.live.streaming.common.util; 7 | import com.easy.live.streaming.common.exception.Exceptions; 8 | import org.apache.commons.lang3.Validate; 9 | 10 | import java.io.IOException; 11 | import java.io.InputStream; 12 | import java.security.GeneralSecurityException; 13 | import java.security.MessageDigest; 14 | import java.security.SecureRandom; 15 | 16 | /** 17 | * 支持SHA-1/MD5消息摘要的工具类. 18 | * 19 | * 返回ByteSource,可进一步被编码为Hex, Base64或UrlSafeBase64 20 | * 21 | */ 22 | public class Digests 23 | { 24 | 25 | private static final String SHA1 = "SHA-1"; 26 | 27 | private static final String MD5 = "MD5"; 28 | 29 | private static SecureRandom random = new SecureRandom(); 30 | 31 | /** 32 | * 对输入字符串进行sha1散列. 33 | */ 34 | public static byte[] sha1(byte[] input) 35 | { 36 | return digest(input, SHA1, null, 1); 37 | } 38 | 39 | public static byte[] sha1(byte[] input, byte[] salt) 40 | { 41 | return digest(input, SHA1, salt, 1); 42 | } 43 | 44 | public static byte[] sha1(byte[] input, byte[] salt, int iterations) 45 | { 46 | return digest(input, SHA1, salt, iterations); 47 | } 48 | 49 | /** 50 | * 对字符串进行散列, 支持md5与sha1算法. 51 | */ 52 | private static byte[] digest(byte[] input, String algorithm, byte[] salt, int iterations) 53 | { 54 | try 55 | { 56 | MessageDigest digest = MessageDigest.getInstance(algorithm); 57 | 58 | if (salt != null) 59 | { 60 | digest.update(salt); 61 | } 62 | 63 | byte[] result = digest.digest(input); 64 | 65 | for (int i = 1; i < iterations; i++) 66 | { 67 | digest.reset(); 68 | result = digest.digest(result); 69 | } 70 | return result; 71 | } 72 | catch (GeneralSecurityException e) 73 | { 74 | throw Exceptions.unchecked(e); 75 | } 76 | } 77 | 78 | /** 79 | * 生成随机的Byte[]作为salt. 80 | * 81 | * @param numBytes byte数组的大小 82 | */ 83 | public static byte[] generateSalt(int numBytes) 84 | { 85 | Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes); 86 | 87 | byte[] bytes = new byte[numBytes]; 88 | random.nextBytes(bytes); 89 | return bytes; 90 | } 91 | 92 | /** 93 | * 对文件进行md5散列. 94 | */ 95 | public static byte[] md5(InputStream input) 96 | throws IOException 97 | { 98 | return digest(input, MD5); 99 | } 100 | 101 | /** 102 | * 对文件进行sha1散列. 103 | */ 104 | public static byte[] sha1(InputStream input) 105 | throws IOException 106 | { 107 | return digest(input, SHA1); 108 | } 109 | 110 | private static byte[] digest(InputStream input, String algorithm) 111 | throws IOException 112 | { 113 | try 114 | { 115 | MessageDigest messageDigest = MessageDigest.getInstance(algorithm); 116 | int bufferLength = 8 * 1024; 117 | byte[] buffer = new byte[bufferLength]; 118 | int read = input.read(buffer, 0, bufferLength); 119 | 120 | while (read > -1) 121 | { 122 | messageDigest.update(buffer, 0, read); 123 | read = input.read(buffer, 0, bufferLength); 124 | } 125 | 126 | return messageDigest.digest(); 127 | } 128 | catch (GeneralSecurityException e) 129 | { 130 | throw Exceptions.unchecked(e); 131 | } 132 | } 133 | 134 | } 135 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/Encodes.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2005-2014 leelong.cn 3 | *

4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | */ 6 | package com.easy.live.streaming.common.util; 7 | import com.easy.live.streaming.common.exception.Exceptions; 8 | import org.apache.commons.codec.DecoderException; 9 | import org.apache.commons.codec.binary.Base64; 10 | import org.apache.commons.codec.binary.Hex; 11 | import org.apache.commons.lang3.StringEscapeUtils; 12 | 13 | import java.io.UnsupportedEncodingException; 14 | import java.net.URLDecoder; 15 | import java.net.URLEncoder; 16 | 17 | /** 18 | * 封装各种格式的编码解码工具类. 19 | * 20 | * 1.Commons-Codec的 hex/base64 编码 21 | * 2.自制的base62 编码 22 | * 3.Commons-Lang的xml/html escape 23 | * 4.JDK提供的URLEncoder 24 | * 25 | */ 26 | public class Encodes { 27 | 28 | private static final String DEFAULT_URL_ENCODING = "UTF-8"; 29 | private static final char[] BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray(); 30 | 31 | /** 32 | * Hex编码. 33 | */ 34 | public static String encodeHex(byte[] input) { 35 | return Hex.encodeHexString(input); 36 | } 37 | 38 | /** 39 | * Hex解码. 40 | */ 41 | public static byte[] decodeHex(String input) { 42 | try { 43 | return Hex.decodeHex(input.toCharArray()); 44 | } catch (DecoderException e) { 45 | throw Exceptions.unchecked(e); 46 | } 47 | } 48 | 49 | /** 50 | * Base64编码. 51 | */ 52 | public static String encodeBase64(byte[] input) { 53 | return Base64.encodeBase64String(input); 54 | } 55 | 56 | /** 57 | * Base64编码, URL安全(将Base64中的URL非法字符'+'和'/'转为'-'和'_', 见RFC3548). 58 | */ 59 | public static String encodeUrlSafeBase64(byte[] input) { 60 | return Base64.encodeBase64URLSafeString(input); 61 | } 62 | 63 | /** 64 | * Base64解码. 65 | */ 66 | public static byte[] decodeBase64(String input) { 67 | return Base64.decodeBase64(input); 68 | } 69 | 70 | /** 71 | * Base62编码。 72 | */ 73 | public static String encodeBase62(byte[] input) { 74 | char[] chars = new char[input.length]; 75 | for (int i = 0; i < input.length; i++) { 76 | chars[i] = BASE62[((input[i] & 0xFF) % BASE62.length)]; 77 | } 78 | return new String(chars); 79 | } 80 | 81 | /** 82 | * Html 转码. 83 | */ 84 | public static String escapeHtml(String html) { 85 | return StringEscapeUtils.escapeHtml4(html); 86 | } 87 | 88 | /** 89 | * Html 解码. 90 | */ 91 | public static String unescapeHtml(String htmlEscaped) { 92 | return StringEscapeUtils.unescapeHtml4(htmlEscaped); 93 | } 94 | 95 | /** 96 | * Xml 转码. 97 | */ 98 | public static String escapeXml(String xml) { 99 | return StringEscapeUtils.escapeXml(xml); 100 | } 101 | 102 | /** 103 | * Xml 解码. 104 | */ 105 | public static String unescapeXml(String xmlEscaped) { 106 | return StringEscapeUtils.unescapeXml(xmlEscaped); 107 | } 108 | 109 | /** 110 | * URL 编码, Encode默认为UTF-8. 111 | */ 112 | public static String urlEncode(String part) { 113 | try { 114 | return URLEncoder.encode(part, DEFAULT_URL_ENCODING); 115 | } catch (UnsupportedEncodingException e) { 116 | throw Exceptions.unchecked(e); 117 | } 118 | } 119 | 120 | /** 121 | * URL 解码, Encode默认为UTF-8. 122 | */ 123 | public static String urlDecode(String part) { 124 | 125 | try { 126 | return URLDecoder.decode(part, DEFAULT_URL_ENCODING); 127 | } catch (UnsupportedEncodingException e) { 128 | throw Exceptions.unchecked(e); 129 | } 130 | } 131 | } 132 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/Servlets.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2005-2014 leelong.cn 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | */ 6 | package com.easy.live.streaming.common.util; 7 | 8 | import com.google.common.net.HttpHeaders; 9 | import org.apache.commons.lang3.Validate; 10 | 11 | import javax.servlet.ServletRequest; 12 | import javax.servlet.http.HttpServletRequest; 13 | import javax.servlet.http.HttpServletResponse; 14 | import java.io.UnsupportedEncodingException; 15 | import java.util.*; 16 | import java.util.Map.Entry; 17 | 18 | /** 19 | * Http与Servlet工具类. 20 | * 21 | */ 22 | public class Servlets 23 | { 24 | 25 | // -- 常用数值定义 --// 26 | public static final long ONE_YEAR_SECONDS = 60 * 60 * 24 * 365; 27 | private static final String DATE_END_WITH = "_DATE"; 28 | 29 | /** 30 | * 设置客户端缓存过期时间 的Header. 31 | */ 32 | public static void setExpiresHeader(HttpServletResponse response, long expiresSeconds) 33 | { 34 | // Http 1.0 header, set a fix expires date. 35 | response.setDateHeader(HttpHeaders.EXPIRES, System.currentTimeMillis() 36 | + (expiresSeconds * 1000)); 37 | // Http 1.1 header, set a time after now. 38 | response.setHeader(HttpHeaders.CACHE_CONTROL, "private, max-age=" + expiresSeconds); 39 | } 40 | 41 | /** 42 | * 设置禁止客户端缓存的Header. 43 | */ 44 | public static void setNoCacheHeader(HttpServletResponse response) 45 | { 46 | // Http 1.0 header 47 | response.setDateHeader(HttpHeaders.EXPIRES, 1L); 48 | response.addHeader(HttpHeaders.PRAGMA, "no-cache"); 49 | // Http 1.1 header 50 | response.setHeader(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, max-age=0"); 51 | } 52 | 53 | /** 54 | * 设置LastModified Header. 55 | */ 56 | public static void setLastModifiedHeader(HttpServletResponse response, long lastModifiedDate) 57 | { 58 | response.setDateHeader(HttpHeaders.LAST_MODIFIED, lastModifiedDate); 59 | } 60 | 61 | /** 62 | * 设置Etag Header. 63 | */ 64 | public static void setEtag(HttpServletResponse response, String etag) 65 | { 66 | response.setHeader(HttpHeaders.ETAG, etag); 67 | } 68 | 69 | /** 70 | * 根据浏览器If-Modified-Since Header, 计算文件是否已被修改. 71 | * 72 | * 如果无修改, checkIfModify返回false ,设置304 not modify status. 73 | * 74 | * @param lastModified 75 | * 内容的最后修改时间. 76 | */ 77 | public static boolean checkIfModifiedSince(HttpServletRequest request, 78 | HttpServletResponse response, long lastModified) 79 | { 80 | long ifModifiedSince = request.getDateHeader(HttpHeaders.IF_MODIFIED_SINCE); 81 | if ((ifModifiedSince != -1) && (lastModified < (ifModifiedSince + 1000))) 82 | { 83 | response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 84 | return false; 85 | } 86 | return true; 87 | } 88 | 89 | /** 90 | * 根据浏览器 If-None-Match Header, 计算Etag是否已无效. 91 | * 92 | * 如果Etag有效, checkIfNoneMatch返回false, 设置304 not modify status. 93 | * 94 | * @param etag 95 | * 内容的ETag. 96 | */ 97 | public static boolean checkIfNoneMatchEtag(HttpServletRequest request, 98 | HttpServletResponse response, String etag) 99 | { 100 | String headerValue = request.getHeader(HttpHeaders.IF_NONE_MATCH); 101 | if (headerValue != null) 102 | { 103 | boolean conditionSatisfied = false; 104 | if (!"*".equals(headerValue)) 105 | { 106 | StringTokenizer commaTokenizer = new StringTokenizer(headerValue, ","); 107 | 108 | while (!conditionSatisfied && commaTokenizer.hasMoreTokens()) 109 | { 110 | String currentToken = commaTokenizer.nextToken(); 111 | if (currentToken.trim().equals(etag)) 112 | { 113 | conditionSatisfied = true; 114 | } 115 | } 116 | } 117 | else 118 | { 119 | conditionSatisfied = true; 120 | } 121 | 122 | if (conditionSatisfied) 123 | { 124 | response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); 125 | response.setHeader(HttpHeaders.ETAG, etag); 126 | return false; 127 | } 128 | } 129 | return true; 130 | } 131 | 132 | /** 133 | * 设置让浏览器弹出下载对话框的Header. 134 | * 135 | * @param fileName 136 | * 下载后的文件名. 137 | */ 138 | public static void setFileDownloadHeader(HttpServletResponse response, String fileName) 139 | { 140 | try 141 | { 142 | // 中文文件名支持 143 | String encodedfileName = new String(fileName.getBytes(), "ISO8859-1"); 144 | response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" 145 | + encodedfileName + "\""); 146 | } 147 | catch (UnsupportedEncodingException e) 148 | { 149 | } 150 | } 151 | 152 | /** 153 | * 取得带相同前缀的Request Parameters, copy from spring WebUtils. 154 | * 155 | * 返回的结果的Parameter名已去除前缀. 156 | */ 157 | public static Map getParametersStartingWith(ServletRequest request, 158 | String prefix) 159 | { 160 | Validate.notNull(request, "Request must not be null"); 161 | Enumeration paramNames = request.getParameterNames(); 162 | Map params = new TreeMap(); 163 | if (prefix == null) 164 | { 165 | prefix = ""; 166 | } 167 | while ((paramNames != null) && paramNames.hasMoreElements()) 168 | { 169 | String paramName = (String) paramNames.nextElement(); 170 | if ("".equals(prefix) || paramName.startsWith(prefix)) 171 | { 172 | String unprefixed = paramName.substring(prefix.length()); 173 | String[] values = request.getParameterValues(paramName); 174 | if ((values == null) || (values.length == 0)) 175 | { 176 | // Do nothing, no values found at all. 177 | } 178 | else if (values.length > 1) 179 | { 180 | params.put(unprefixed, values); 181 | } 182 | else if (unprefixed.endsWith(DATE_END_WITH)) 183 | { // 如果搜索为时间转换成date类型 184 | unprefixed = unprefixed.replace(DATE_END_WITH, ""); 185 | params.put(unprefixed, SysDate.getDate(values[0], "yyyy-MM-dd HH:mm")); 186 | } 187 | else 188 | { 189 | params.put(unprefixed, values[0]); 190 | } 191 | } 192 | } 193 | return params; 194 | } 195 | 196 | /** 197 | * 组合Parameters生成Query String的Parameter部分, 并在paramter name上加上prefix. 198 | * 199 | * @see #getParametersStartingWith 200 | */ 201 | public static String encodeParameterStringWithPrefix(Map params, String prefix) 202 | { 203 | if ((params == null) || (params.size() == 0)) 204 | { 205 | return ""; 206 | } 207 | 208 | if (prefix == null) 209 | { 210 | prefix = ""; 211 | } 212 | 213 | StringBuilder queryStringBuilder = new StringBuilder(); 214 | Iterator> it = params.entrySet().iterator(); 215 | while (it.hasNext()) 216 | { 217 | Entry entry = it.next(); 218 | queryStringBuilder.append(prefix).append(entry.getKey()).append('=') 219 | .append(entry.getValue()); 220 | if (it.hasNext()) 221 | { 222 | queryStringBuilder.append('&'); 223 | } 224 | } 225 | return queryStringBuilder.toString(); 226 | } 227 | 228 | /** 229 | * 客户端对Http Basic验证的 Header进行编码. 230 | */ 231 | public static String encodeHttpBasic(String userName, String password) 232 | { 233 | String encode = userName + ":" + password; 234 | return "Basic " + Encodes.encodeBase64(encode.getBytes()); 235 | } 236 | 237 | } 238 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/jpa/Collections3.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2005-2014 leelong.cn 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | */ 6 | package com.easy.live.streaming.common.util.jpa; 7 | 8 | import com.easy.live.streaming.common.util.Reflections; 9 | import org.apache.commons.beanutils.PropertyUtils; 10 | import org.apache.commons.lang3.StringUtils; 11 | 12 | import java.util.*; 13 | 14 | /** 15 | * Collections工具集. 16 | * 17 | * 在JDK的Collections和Guava的Collections2后, 命名为Collections3. 18 | * 19 | * 函数主要由两部分组成,一是自反射提取元素的功能,二是源自Apache Commons Collection, 争取不用在项目里引入它。 20 | * 21 | */ 22 | public class Collections3 { 23 | 24 | /** 25 | * 提取集合中的对象的两个属性(通过Getter函数), 组合成Map. 26 | * 27 | * @param collection 来源集合. 28 | * @param keyPropertyName 要提取为Map中的Key值的属性名. 29 | * @param valuePropertyName 要提取为Map中的Value值的属性名. 30 | */ 31 | public static Map extractToMap(final Collection collection, final String keyPropertyName, 32 | final String valuePropertyName) { 33 | Map map = new HashMap(collection.size()); 34 | 35 | try { 36 | for (Object obj : collection) { 37 | map.put(PropertyUtils.getProperty(obj, keyPropertyName), 38 | PropertyUtils.getProperty(obj, valuePropertyName)); 39 | } 40 | } catch (Exception e) { 41 | throw Reflections.convertReflectionExceptionToUnchecked(e); 42 | } 43 | 44 | return map; 45 | } 46 | 47 | /** 48 | * 提取集合中的对象的一个属性(通过Getter函数), 组合成List. 49 | * 50 | * @param collection 来源集合. 51 | * @param propertyName 要提取的属性名. 52 | */ 53 | public static List extractToList(final Collection collection, final String propertyName) { 54 | List list = new ArrayList(collection.size()); 55 | 56 | try { 57 | for (Object obj : collection) { 58 | list.add(PropertyUtils.getProperty(obj, propertyName)); 59 | } 60 | } catch (Exception e) { 61 | throw Reflections.convertReflectionExceptionToUnchecked(e); 62 | } 63 | 64 | return list; 65 | } 66 | 67 | /** 68 | * 提取集合中的对象的一个属性(通过Getter函数), 组合成由分割符分隔的字符串. 69 | * 70 | * @param collection 来源集合. 71 | * @param propertyName 要提取的属性名. 72 | * @param separator 分隔符. 73 | */ 74 | public static String extractToString(final Collection collection, final String propertyName, final String separator) { 75 | List list = extractToList(collection, propertyName); 76 | return StringUtils.join(list, separator); 77 | } 78 | 79 | /** 80 | * 转换Collection所有元素(通过toString())为String, 中间以 separator分隔。 81 | */ 82 | public static String convertToString(final Collection collection, final String separator) { 83 | return StringUtils.join(collection, separator); 84 | } 85 | 86 | /** 87 | * 转换Collection所有元素(通过toString())为String, 每个元素的前面加入prefix,后面加入postfix,如

mymessage
。 88 | */ 89 | public static String convertToString(final Collection collection, final String prefix, final String postfix) { 90 | StringBuilder builder = new StringBuilder(); 91 | for (Object o : collection) { 92 | builder.append(prefix).append(o).append(postfix); 93 | } 94 | return builder.toString(); 95 | } 96 | 97 | /** 98 | * 判断是否为空. 99 | */ 100 | public static boolean isEmpty(Collection collection) { 101 | return ((collection == null) || collection.isEmpty()); 102 | } 103 | 104 | /** 105 | * 判断是否为空. 106 | */ 107 | public static boolean isNotEmpty(Collection collection) { 108 | return ((collection != null) && !(collection.isEmpty())); 109 | } 110 | 111 | /** 112 | * 取得Collection的第一个元素,如果collection为空返回null. 113 | */ 114 | public static T getFirst(Collection collection) { 115 | if (isEmpty(collection)) { 116 | return null; 117 | } 118 | 119 | return collection.iterator().next(); 120 | } 121 | 122 | /** 123 | * 获取Collection的最后一个元素 ,如果collection为空返回null. 124 | */ 125 | public static T getLast(Collection collection) { 126 | if (isEmpty(collection)) { 127 | return null; 128 | } 129 | 130 | // 当类型为List时,直接取得最后一个元素 。 131 | if (collection instanceof List) { 132 | List list = (List) collection; 133 | return list.get(list.size() - 1); 134 | } 135 | 136 | // 其他类型通过iterator滚动到最后一个元素. 137 | Iterator iterator = collection.iterator(); 138 | while (true) { 139 | T current = iterator.next(); 140 | if (!iterator.hasNext()) { 141 | return current; 142 | } 143 | } 144 | } 145 | 146 | /** 147 | * 返回a+b的新List. 148 | */ 149 | public static List union(final Collection a, final Collection b) { 150 | List result = new ArrayList(a); 151 | result.addAll(b); 152 | return result; 153 | } 154 | 155 | /** 156 | * 返回a-b的新List. 157 | */ 158 | public static List subtract(final Collection a, final Collection b) { 159 | List list = new ArrayList(a); 160 | for (T element : b) { 161 | list.remove(element); 162 | } 163 | 164 | return list; 165 | } 166 | 167 | /** 168 | * 返回a与b的交集的新List. 169 | */ 170 | public static List intersection(Collection a, Collection b) { 171 | List list = new ArrayList(); 172 | 173 | for (T element : a) { 174 | if (b.contains(element)) { 175 | list.add(element); 176 | } 177 | } 178 | return list; 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/jpa/DynamicSpecifications.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.util.jpa; 2 | import com.google.common.collect.Lists; 3 | import org.apache.commons.lang3.StringUtils; 4 | import org.springframework.data.jpa.domain.Specification; 5 | 6 | import javax.persistence.criteria.*; 7 | import java.util.Collection; 8 | import java.util.List; 9 | 10 | public class DynamicSpecifications 11 | { 12 | public static Specification bySearchFilter(final Collection filters, final Class clazz) 13 | { 14 | return new Specification() 15 | { 16 | 17 | public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder builder) 18 | { 19 | if (Collections3.isNotEmpty(filters)) 20 | { 21 | 22 | List predicates = Lists.newArrayList(); 23 | for (SearchFilter filter : filters) 24 | { 25 | // nested path translate, 如Task的名为"user.name"的filedName, 26 | // 转换为Task.user.name属性 27 | String[] names = StringUtils.split(filter.fieldName, "."); 28 | Path expression = root.get(names[0]); 29 | for (int i = 1; i < names.length; i++) 30 | { 31 | expression = expression.get(names[i]); 32 | } 33 | 34 | // logic operator 35 | switch (filter.operator) 36 | { 37 | case EQ: 38 | predicates.add(builder.equal(expression, filter.value)); 39 | break; 40 | case NE: 41 | predicates.add(builder.notEqual(expression, filter.value)); 42 | break; 43 | case LIKE: 44 | predicates.add(builder.like(expression, "%" + filter.value + "%")); 45 | break; 46 | case NOTLIKE: 47 | predicates.add(builder.notLike(expression, "%" + filter.value + "%")); 48 | break; 49 | case GT: 50 | predicates.add(builder.greaterThan(expression, (Comparable)filter.value)); 51 | break; 52 | case LT: 53 | predicates.add(builder.lessThan(expression, (Comparable)filter.value)); 54 | break; 55 | case GTE: 56 | predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable)filter.value)); 57 | break; 58 | case LTE: 59 | predicates.add(builder.lessThanOrEqualTo(expression, (Comparable)filter.value)); 60 | break; 61 | case IN: 62 | String[] values = filter.value.toString().split(","); 63 | predicates.add(expression.in(values)); 64 | break; 65 | case INLIST: 66 | List ids = (List) filter.value; 67 | predicates.add(expression.in(ids)); 68 | break; 69 | case NOTIN: 70 | predicates.add(builder.not(expression.in(filter.value))); 71 | break; 72 | case ISNULL: 73 | predicates.add(builder.isNull(expression)); 74 | break; 75 | case ISNOTNULL: 76 | predicates.add(builder.isNotNull(expression)); 77 | break; 78 | } 79 | } 80 | 81 | // 将所有条件用 and 联合起来 82 | if (!predicates.isEmpty()) 83 | { 84 | return builder.and(predicates.toArray(new Predicate[predicates.size()])); 85 | } 86 | } 87 | 88 | return builder.conjunction(); 89 | } 90 | }; 91 | } 92 | } -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/jpa/PageUtil.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.util.jpa; 2 | import org.apache.commons.collections.CollectionUtils; 3 | import org.apache.commons.lang3.ArrayUtils; 4 | import org.apache.commons.lang3.StringUtils; 5 | import org.apache.commons.lang3.Validate; 6 | import org.springframework.data.domain.PageRequest; 7 | import org.springframework.data.domain.Sort; 8 | import org.springframework.data.domain.Sort.Direction; 9 | import org.springframework.data.jpa.domain.Specification; 10 | 11 | import java.util.List; 12 | import java.util.Map; 13 | 14 | /** 15 | * 16 | * 分页工具类 17 | * 18 | */ 19 | public class PageUtil 20 | { 21 | 22 | /** 23 | * 创建分页请求. 24 | */ 25 | public static PageRequest buildPageRequest(int pageNumber, int pageSize) 26 | { 27 | Sort sort = null; 28 | return buildPageRequest(pageNumber, pageSize, sort); 29 | } 30 | 31 | /** 32 | * 33 | * <创建分页请求,并按照指定的顺序和属性进行排序 单个字段排序>
34 | * <比如direction为"desc",oderBy为id,会按照id降序> 35 | * 36 | * @param pageNumber 37 | * @param pageSize 38 | * @param direction 39 | * @param orderBy 40 | * @return 41 | * @see [类、类#方法、类#成员] 42 | */ 43 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, Direction direction, String orderBy) 44 | { 45 | Sort sort = null; 46 | if (null != direction && StringUtils.isNotBlank(orderBy)) 47 | { 48 | sort = new Sort(direction, orderBy); 49 | } 50 | return buildPageRequest(pageNumber, pageSize, sort); 51 | } 52 | 53 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, String direction, String... orderBys) 54 | { 55 | Sort sort = null; 56 | if ("desc".equalsIgnoreCase(direction)) 57 | { 58 | sort = new Sort(Direction.DESC, orderBys); 59 | } 60 | else 61 | { 62 | sort = new Sort(Direction.ASC, orderBys); 63 | } 64 | return new PageRequest(pageNumber - 1, pageSize, sort); 65 | } 66 | 67 | /** 68 | * 69 | * <创建分页请求,并按照指定的顺序和属性进行排序 排序方向相同的多个字段排序>
70 | * <比如direction为"desc",oderBy为{"id","age"},会按照id降序,然后age降序> 71 | * 72 | * @param pageNumber 73 | * @param pageSize 74 | * @param direction 75 | * @param orderBys 76 | * @return 77 | * @see [类、类#方法、类#成员] 78 | */ 79 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, Direction direction, String... orderBys) 80 | { 81 | Sort sort = null; 82 | if (null != direction && ArrayUtils.isNotEmpty(orderBys)) 83 | { 84 | sort = new Sort(direction, orderBys); 85 | } 86 | return buildPageRequest(pageNumber, pageSize, sort); 87 | } 88 | 89 | /** 90 | * 91 | * <创建分页请求,并按照指定的顺序和属性进行排序 排序方向相同的多个字段排序>
92 | * <比如direction为"desc",oderBy为{"id","age"},会按照id降序,然后age降序> 93 | * 94 | * @param pageNumber 95 | * @param pageSize 96 | * @param direction 97 | * @param orderBys 98 | * @return 99 | * @see [类、类#方法、类#成员] 100 | */ 101 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, Direction direction, List orderBys) 102 | { 103 | Sort sort = null; 104 | if (null != direction && CollectionUtils.isNotEmpty(orderBys)) 105 | { 106 | sort = new Sort(direction, orderBys); 107 | } 108 | return buildPageRequest(pageNumber, pageSize, sort); 109 | } 110 | 111 | /** 112 | * 113 | * <创建分页请求,并按照指定的顺序和属性进行排序 排序方向不同的多个字段排序>
114 | * <例如directions中包含排序方向{"asc","desc","asc"},orderBys中包含需要排序的字段 115 | * {"id","age","name"},则排序的效果为先按照id升序,再按age降序,再按name升序排列.directions和oderBys list大小要相同> 116 | * 117 | * @param pageNumber 118 | * @param pageSize 119 | * @param directions 120 | * @param orderBys 121 | * @return 122 | * @see [类、类#方法、类#成员] 123 | */ 124 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, List directions, 125 | List orderBys) 126 | { 127 | Sort sort = null; 128 | if (CollectionUtils.isNotEmpty(directions) && CollectionUtils.isNotEmpty(orderBys)) 129 | { 130 | Validate.isTrue((directions.size() == orderBys.size()), "directions of size eq to the size of orderBys."); 131 | for (int i = 0, len = directions.size(); i < len; i++) 132 | { 133 | if (i == 0) 134 | { 135 | sort = new Sort(directions.get(i), orderBys.get(i)); 136 | } 137 | else 138 | { 139 | sort.and(new Sort(directions.get(i), orderBys.get(i))); 140 | } 141 | } 142 | } 143 | return buildPageRequest(pageNumber, pageSize, sort); 144 | } 145 | 146 | public static PageRequest buildPageRequest(int pageNumber, int pageSize, Sort sort) 147 | { 148 | Validate.isTrue((pageNumber >= 1), "pageNumber must be gte one."); 149 | Validate.isTrue((pageSize >= 1), "pageSize must be gte one."); 150 | 151 | return new PageRequest(pageNumber - 1, pageSize, sort); 152 | } 153 | 154 | /** 155 | * 156 | * <创建动态查询条件组合>
157 | * <功能详细描述> 158 | * 159 | * @param searchParams 160 | * @param t 161 | * @return 162 | * @see [类、类#方法、类#成员] 163 | */ 164 | @SuppressWarnings("unchecked") 165 | public static Specification buildSpecification(Map searchParams, Class t) 166 | { 167 | Map filters = SearchFilter.parse(searchParams); 168 | Specification spec = (Specification)DynamicSpecifications.bySearchFilter(filters.values(), t.getClass()); 169 | return spec; 170 | } 171 | 172 | } 173 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/jpa/SearchFilter.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.util.jpa; 2 | import com.google.common.collect.Maps; 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | import java.util.Map; 6 | import java.util.Map.Entry; 7 | 8 | public class SearchFilter 9 | { 10 | public enum Operator 11 | { 12 | EQ, NE, LIKE, GT, LT, GTE, LTE, IN, INLIST, NOTIN, ISNULL, ISNOTNULL, NOTLIKE 13 | } 14 | 15 | // add by chenbo; 16 | public enum ConjunctionType 17 | { 18 | ADD, OR 19 | } 20 | 21 | public String fieldName; 22 | 23 | public Object value; 24 | 25 | public Operator operator; 26 | 27 | public ConjunctionType conjunctionType; 28 | 29 | public SearchFilter(String fieldName, Operator operator, Object value) 30 | { 31 | this.fieldName = fieldName; 32 | this.value = value; 33 | this.operator = operator; 34 | this.conjunctionType = ConjunctionType.ADD; 35 | } 36 | 37 | public SearchFilter(String fieldName, Operator operator, Object value, ConjunctionType conjunctionType) 38 | { 39 | this.fieldName = fieldName; 40 | this.value = value; 41 | this.operator = operator; 42 | this.conjunctionType = conjunctionType; 43 | } 44 | 45 | /** 46 | * searchParams中key的格式为OPERATOR_FIELDNAME 47 | */ 48 | public static Map parse(Map searchParams) 49 | { 50 | Map filters = Maps.newHashMap(); 51 | 52 | for (Entry entry : searchParams.entrySet()) 53 | { 54 | // 过滤掉空值 55 | String key = entry.getKey(); 56 | Object value = entry.getValue(); 57 | if (value == null || "".equals(value)) 58 | { 59 | continue; 60 | } 61 | 62 | // 拆分operator与filedAttribute 63 | String[] names = StringUtils.split(key, "_"); 64 | if (names.length != 2 && names.length != 3) 65 | { 66 | throw new IllegalArgumentException(key + " is not a valid search filter name"); 67 | } 68 | String filedName = names[1]; 69 | Operator operator = Operator.valueOf(names[0]); 70 | SearchFilter filter; 71 | if (names.length == 3) 72 | { 73 | // 创建searchFilter 74 | filter = new SearchFilter(filedName, operator, value, ConjunctionType.valueOf(names[2])); 75 | } 76 | else 77 | { 78 | // 创建searchFilter 79 | filter = new SearchFilter(filedName, operator, value); 80 | } 81 | 82 | filters.put(key, filter); 83 | } 84 | 85 | return filters; 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /live-common/src/main/java/com/easy/live/streaming/common/util/jpa/SerializeUtils.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.common.util.jpa; 2 | import java.io.*; 3 | 4 | public final class SerializeUtils { 5 | 6 | 7 | /** 8 | * deserialize 9 | * @param bytes 10 | * @return 11 | */ 12 | public static Object deserialize(byte[] bytes) { 13 | 14 | Object result = null; 15 | 16 | if (isEmpty(bytes)) { 17 | return null; 18 | } 19 | 20 | try { 21 | ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes); 22 | try { 23 | ObjectInputStream objectInputStream = new ObjectInputStream(byteStream); 24 | try { 25 | result = objectInputStream.readObject(); 26 | } catch (ClassNotFoundException ex) { 27 | throw new Exception("Failed to deserialize object type", ex); 28 | } 29 | } catch (Throwable ex) { 30 | throw new Exception("Failed to deserialize", ex); 31 | } 32 | } catch (Exception e) { 33 | // logger.error("Failed to deserialize", e); 34 | } 35 | return result; 36 | } 37 | 38 | public static boolean isEmpty(byte[] data) { 39 | return (data == null || data.length == 0); 40 | } 41 | 42 | /** 43 | * serialize 44 | * @param object 45 | * @return 46 | */ 47 | public static byte[] serialize(Object object) { 48 | byte[] result = null; 49 | if (object == null) { 50 | return new byte[0]; 51 | } 52 | try { 53 | ByteArrayOutputStream byteStream = new ByteArrayOutputStream(128); 54 | try { 55 | if (!(object instanceof Serializable)) { 56 | throw new IllegalArgumentException( 57 | SerializeUtils.class.getSimpleName() + " requires a Serializable payload " 58 | + "but received an object of type [" + object.getClass().getName() + "]"); 59 | } 60 | ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteStream); 61 | objectOutputStream.writeObject(object); 62 | objectOutputStream.flush(); 63 | result = byteStream.toByteArray(); 64 | } catch (Throwable ex) { 65 | throw new Exception("Failed to serialize", ex); 66 | } 67 | } catch (Exception ex) { 68 | // logger.error("Failed to serialize", ex); 69 | } 70 | return result; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /live-config/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-config 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-eureka 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-tomcat 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-actuator 27 | 28 | 29 | 30 | 31 | org.springframework.cloud 32 | spring-cloud-config-server 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-actuator 37 | 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-actuator 43 | 1.5.10.RELEASE 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | org.springframework.boot 52 | spring-boot-maven-plugin 53 | 1.5.2.RELEASE 54 | 55 | 56 | 57 | repackage 58 | 59 | 60 | 61 | 62 | 63 | 64 | -------------------------------------------------------------------------------- /live-config/src/main/java/com/easy/live/streaming/gateway/ConfigApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 6 | import org.springframework.cloud.config.server.EnableConfigServer; 7 | 8 | /** 9 | * @Description:配置中心服务 10 | * @Author: zhangliangfu 11 | * @Create on: 2019-06-12 19:02 12 | */ 13 | 14 | @SpringBootApplication 15 | @EnableDiscoveryClient 16 | @EnableConfigServer 17 | public class ConfigApplication { 18 | public static void main(String[] args) { 19 | new SpringApplicationBuilder(ConfigApplication.class).web(true).run(args); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /live-config/src/main/java/com/easy/live/streaming/gateway/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [Config] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-config/src/main/java/com/easy/live/streaming/gateway/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-config/src/main/java/com/easy/live/streaming/gateway/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.boot.CommandLineRunner; 6 | 7 | public class StartupRunner implements CommandLineRunner { 8 | 9 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 10 | 11 | @Override 12 | public void run(String... args) throws Exception { 13 | logger.info("配置中心服务启动成功"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-config/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-config/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | easy: 2 | eurekaServer: http://live.registry.com:8001/eureka/ 3 | 4 | 5 | server: 6 | port: 8002 7 | 8 | spring: 9 | application: 10 | name: config-server 11 | profiles: 12 | active: native 13 | cloud: 14 | config: 15 | server: 16 | native: 17 | search-locations: classpath:/config/ 18 | default-label: dev 19 | 20 | management: 21 | security: 22 | enabled: false 23 | 24 | eureka: 25 | instance: 26 | preferIpAddress: true 27 | client: 28 | serviceUrl: 29 | defaultZone: ${easy.eurekaServer} 30 | 31 | -------------------------------------------------------------------------------- /live-config/src/main/resources/config/dev/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | http: 3 | multipart: 4 | max-file-size: 50Mb 5 | max-request-size: 100Mb 6 | enabled: true 7 | cloud: 8 | loadbalancer: 9 | retry: 10 | enabled: true 11 | datasource: 12 | driver-class-name: com.mysql.jdbc.Driver 13 | name: habits 14 | url: jdbc:mysql://x.x.x.x:3306/video_live?useUnicode=true&characterEncoding=UTF-8 15 | username: xxx 16 | password: xxxx 17 | jpa: 18 | show-sql: false 19 | generate-ddl: true 20 | hibernate.ddl-auto: update 21 | database-platform: org.hibernate.dialect.MySQL5Dialect 22 | redis: 23 | host: xxxxxx 24 | password: xxxx 25 | database: 0 26 | timeout: 3000 27 | port: 1218 28 | session: 29 | store-type: redis 30 | -------------------------------------------------------------------------------- /live-config/src/main/resources/config/dev/gateway-server-dev.yml: -------------------------------------------------------------------------------- 1 | 2 | zuul: 3 | #add-host-header: true #网关在进行请求路由转发前为请求设置Host头信息 4 | sensitiveHeaders: Session,Origin,Cookie,Set-Cookie,Authorization 5 | semaphore: 6 | max-semaphores: 5000 7 | routes: 8 | video-route: 9 | path: /video-server/** 10 | serviceId: video-server 11 | user-route: 12 | path: /user-server/** 13 | serviceId: user-server 14 | auth-route: 15 | path: /auth-server/** 16 | serviceId: auth-server 17 | error: 18 | path: /gw/error 19 | 20 | server: 21 | connection-timeout: 3000 -------------------------------------------------------------------------------- /live-config/src/main/resources/config/dev/user-server-dev.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangfuz/live-streaming/0ae1727ad2eda591d9eea4f57e03fff6d2b87375/live-config/src/main/resources/config/dev/user-server-dev.yml -------------------------------------------------------------------------------- /live-config/src/main/resources/config/dev/video-server-dev.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangfuz/live-streaming/0ae1727ad2eda591d9eea4f57e03fff6d2b87375/live-config/src/main/resources/config/dev/video-server-dev.yml -------------------------------------------------------------------------------- /live-config/src/main/resources/config/prod/application-prod.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangfuz/live-streaming/0ae1727ad2eda591d9eea4f57e03fff6d2b87375/live-config/src/main/resources/config/prod/application-prod.yml -------------------------------------------------------------------------------- /live-config/src/main/resources/config/test/application-test.yml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/liangfuz/live-streaming/0ae1727ad2eda591d9eea4f57e03fff6d2b87375/live-config/src/main/resources/config/test/application-test.yml -------------------------------------------------------------------------------- /live-data/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | com.easy 13 | live-data 14 | 15 | 16 | 17 | org.projectlombok 18 | lombok 19 | 20 | 21 | org.springframework.boot 22 | spring-boot-starter-data-jpa 23 | 24 | 25 | com.fasterxml.jackson.core 26 | jackson-annotations 27 | 28 | 29 | org.apache.commons 30 | commons-lang3 31 | 32 | 33 | org.springframework.data 34 | spring-data-redis 35 | 36 | 37 | org.springframework.data 38 | spring-data-jpa 39 | 40 | 41 | commons-collections 42 | commons-collections 43 | 44 | 45 | com.google.guava 46 | guava 47 | 48 | 49 | com.alibaba 50 | fastjson 51 | 52 | 53 | com.easy 54 | live-common 55 | 56 | 57 | io.springfox 58 | springfox-swagger-ui 59 | 60 | 61 | io.springfox 62 | springfox-swagger2 63 | 64 | 65 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/bean/BaseOutput.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.bean; 2 | 3 | import lombok.Data; 4 | 5 | import java.io.Serializable; 6 | 7 | /** 8 | * @Description: base input 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-25 10:59 11 | */ 12 | 13 | @Data 14 | public class BaseOutput implements Serializable { 15 | private Integer code; 16 | private String message; 17 | private T data; 18 | 19 | public BaseOutput(Integer code, String message){ 20 | this.code = code; 21 | this.message = message; 22 | } 23 | 24 | public BaseOutput(Integer code, String message, T data){ 25 | this.code = code; 26 | this.message = message; 27 | this.data = data; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/cache/SimpleCacheService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.cache; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.dao.DataAccessException; 6 | import org.springframework.data.redis.connection.RedisConnection; 7 | import org.springframework.data.redis.core.RedisCallback; 8 | import org.springframework.data.redis.core.StringRedisTemplate; 9 | import org.springframework.stereotype.Component; 10 | import org.springframework.util.StringUtils; 11 | 12 | import java.util.*; 13 | import java.util.concurrent.TimeUnit; 14 | 15 | @Component("simpleCacheService") 16 | public class SimpleCacheService { 17 | 18 | @Autowired 19 | private StringRedisTemplate redisTemplate; 20 | 21 | public Set keys(String pattern) { 22 | return redisTemplate.keys(pattern); 23 | } 24 | 25 | public boolean expire(String key, long timeout) { 26 | return redisTemplate.expire(key, timeout, TimeUnit.SECONDS); 27 | } 28 | 29 | public Boolean exists(String key) { 30 | return redisTemplate.execute(new RedisCallback() { 31 | @Override 32 | public Boolean doInRedis(RedisConnection redisConnection) throws DataAccessException { 33 | return redisConnection.exists(key.getBytes()); 34 | } 35 | }); 36 | } 37 | 38 | public long increment(String key, long delta) { 39 | return redisTemplate.opsForValue().increment(key, delta); 40 | } 41 | 42 | public void set(String key, T val) { 43 | if (!StringUtils.isEmpty(val)) { 44 | redisTemplate.opsForValue().set(key, JSON.toJSONString(val)); 45 | } 46 | } 47 | 48 | public void add(String key, T val, long expire) { 49 | if (!StringUtils.isEmpty(val)) { 50 | redisTemplate.opsForValue().set(key, JSON.toJSONString(val), expire, TimeUnit.SECONDS); 51 | } 52 | } 53 | 54 | public void del(String key) { 55 | redisTemplate.delete(key); 56 | } 57 | 58 | public T get(String key, Class clazz) { 59 | String json = redisTemplate.opsForValue().get(key); 60 | if (!StringUtils.isEmpty(json)) { 61 | return JSON.parseObject(json, clazz); 62 | } 63 | return null; 64 | } 65 | 66 | public void hAdd(String key, String hashKey, T val) { 67 | if (!StringUtils.isEmpty(val)) { 68 | redisTemplate.opsForHash().put(key, hashKey, JSON.toJSONString(val)); 69 | } 70 | } 71 | 72 | public T hGet(String key, String hashKey, Class clazz) { 73 | Object obj = redisTemplate.opsForHash().get(key, hashKey); 74 | if (obj != null) { 75 | return JSON.parseObject(obj.toString(), clazz); 76 | } 77 | return null; 78 | } 79 | 80 | public boolean hasKey(String key, String hashKey){ 81 | return redisTemplate.opsForHash().hasKey(key, hashKey); 82 | } 83 | 84 | public List hGetArray(String key, String hashKey, Class clazz) { 85 | Object obj = redisTemplate.opsForHash().get(key, hashKey); 86 | if (obj != null) { 87 | return JSON.parseArray(obj.toString(), clazz); 88 | } 89 | return null; 90 | } 91 | 92 | public Map hGetAll(String key, Class clazz) { 93 | Map map = redisTemplate.opsForHash().entries(key); 94 | if (map != null) { 95 | Map vals = new HashMap<>(); 96 | for (Object k : map.keySet()) { 97 | String val = (String) map.get(k); 98 | vals.put(k.toString(), JSON.parseObject(val, clazz)); 99 | } 100 | return vals; 101 | } 102 | return null; 103 | } 104 | 105 | public Set hKeys(String key) { 106 | Set keys = redisTemplate.opsForHash().keys(key); 107 | if (keys != null) { 108 | Set vals = new HashSet<>(); 109 | for (Object k : keys) { 110 | vals.add(k.toString()); 111 | } 112 | return vals; 113 | } 114 | return Collections.emptySet(); 115 | } 116 | 117 | public List hVals(String key, Class clazz) { 118 | List objs = redisTemplate.opsForHash().values(key); 119 | if (objs != null) { 120 | List vals = new ArrayList<>(); 121 | for (Object obj : objs) { 122 | vals.add(JSON.parseObject(obj.toString(), clazz)); 123 | } 124 | return vals; 125 | } 126 | return Collections.emptyList(); 127 | } 128 | 129 | public long hDel(String key, Object hashKey) { 130 | return redisTemplate.opsForHash().delete(key, hashKey); 131 | } 132 | 133 | 134 | public void sAdd(String key, T val) { 135 | if (!StringUtils.isEmpty(val)) { 136 | redisTemplate.opsForSet().add(key, JSON.toJSONString(val)); 137 | } 138 | } 139 | 140 | public T sGet(String key, Class clazz) { 141 | String json = redisTemplate.opsForSet().pop(key); 142 | if (!StringUtils.isEmpty(json)) { 143 | return JSON.parseObject(json, clazz); 144 | } 145 | return null; 146 | } 147 | 148 | public long sDel(String key, T val) { 149 | return redisTemplate.opsForSet().remove(key, val); 150 | } 151 | 152 | public long zSize(String key) { 153 | return redisTemplate.opsForZSet().zCard(key); 154 | } 155 | 156 | public Set range(String key, long start, long end) { 157 | return redisTemplate.opsForZSet().range(key, 0, end); 158 | } 159 | 160 | public long leftPush(String queueName, String data) { 161 | return redisTemplate.opsForList().leftPush(queueName, data); 162 | } 163 | 164 | public T rightPop(String queueName,Class clazz) { 165 | String json = redisTemplate.opsForList().rightPop(queueName); 166 | if (!StringUtils.isEmpty(json)) { 167 | return JSON.parseObject(json, clazz); 168 | } 169 | return null; 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/cache/ThreadLocalUserCache.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.cache; 2 | 3 | import org.springframework.core.NamedThreadLocal; 4 | 5 | /** 6 | * Description: 用户本地缓存 7 | * Author: zhangliangfu 8 | * Create on: 2019-08-08 13:48 9 | */ 10 | public class ThreadLocalUserCache { 11 | private static final ThreadLocal localThreadConfig = new NamedThreadLocal<>("UserCache"); 12 | 13 | /** 14 | * 设置 15 | * 16 | * @param userCache 17 | */ 18 | public final static void setUserCache(UserCache userCache) { 19 | localThreadConfig.set(userCache); 20 | } 21 | 22 | /** 23 | * 获取 24 | * @return 25 | */ 26 | public final static UserCache getUserCache(){ 27 | return localThreadConfig.get(); 28 | } 29 | 30 | /** 31 | * 移除 32 | */ 33 | public final static void remove(){ 34 | localThreadConfig.remove(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/cache/UserCache.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.cache; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * Description: 用户缓存 7 | * Author: zhangliangfu 8 | * Create on: 2019-08-08 13:49 9 | */ 10 | 11 | @Data 12 | public class UserCache { 13 | private Integer userId; 14 | private String sessionId; 15 | } 16 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/config/SwaggerConfig.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.config; 2 | import org.springframework.context.annotation.Bean; 3 | import org.springframework.context.annotation.Configuration; 4 | import springfox.documentation.builders.ApiInfoBuilder; 5 | import springfox.documentation.builders.PathSelectors; 6 | import springfox.documentation.builders.RequestHandlerSelectors; 7 | import springfox.documentation.service.ApiInfo; 8 | import springfox.documentation.spi.DocumentationType; 9 | import springfox.documentation.spring.web.plugins.Docket; 10 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 11 | 12 | /** 13 | * @Description:swagger配置 14 | * @Author: zhangliangfu 15 | * @Create on: 2019-08-15 15:39 16 | */ 17 | 18 | @Configuration 19 | @EnableSwagger2 20 | public class SwaggerConfig { 21 | 22 | public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.easy"; 23 | public static final String VERSION = "1.0.0"; 24 | 25 | ApiInfo apiInfo() { 26 | return new ApiInfoBuilder() 27 | .title("SpringCloud搭建直播API") 28 | .description("视频直播对接文档\n系统搭建完毕统一走网关(8004)服务,对应的api需要加服务名前缀,如\"/video-server/、/auth-server/、/user" + 29 | "-server/\"等") 30 | .license("EASY-LIFE") 31 | .licenseUrl("http://live.registry.com:80001") 32 | .termsOfServiceUrl("") 33 | .version(VERSION) 34 | .build(); 35 | } 36 | 37 | @Bean 38 | public Docket customImplementation(){ 39 | return new Docket(DocumentationType.SWAGGER_2) 40 | .apiInfo(apiInfo()) 41 | .select() 42 | .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE)) 43 | .paths(PathSelectors.any()) 44 | .build(); 45 | } 46 | } -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/AbstractEntity.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity; 2 | 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | 5 | import javax.persistence.*; 6 | import java.io.Serializable; 7 | 8 | /** 9 | * Created by ZLF on 2017/6/13. 10 | */ 11 | @MappedSuperclass 12 | public class AbstractEntity implements Entity, Serializable { 13 | 14 | @Id 15 | @Column(name = "id") 16 | @GeneratedValue(strategy = GenerationType.IDENTITY) 17 | private ID id; 18 | 19 | public ID getId() { 20 | return id; 21 | } 22 | 23 | public void setId(ID id) { 24 | this.id = id; 25 | } 26 | @JsonIgnore 27 | public boolean isNew() { 28 | if (this.id == null || "".equals(this.id)) { 29 | this.setId(null); 30 | return true; 31 | } 32 | 33 | return false; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/BaseEntity.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity; 2 | import com.fasterxml.jackson.annotation.JsonFormat; 3 | 4 | import javax.persistence.Column; 5 | import javax.persistence.MappedSuperclass; 6 | import javax.persistence.PrePersist; 7 | import javax.persistence.PreUpdate; 8 | import java.util.Date; 9 | 10 | /** 11 | * Created by ZLF on 2017/6/13. 12 | */ 13 | @MappedSuperclass 14 | public abstract class BaseEntity extends AbstractEntity { 15 | 16 | private Date createdTime; 17 | 18 | private Date modifiedTime; 19 | 20 | @PrePersist 21 | public void beforeAdd(){ 22 | if (createdTime==null){ 23 | createdTime=new Date(); 24 | } 25 | modifiedTime=new Date(); 26 | } 27 | @PreUpdate 28 | public void beforeModified(){ 29 | modifiedTime=new Date(); 30 | } 31 | 32 | @Column(updatable = false, name = "created_time") 33 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 34 | public Date getCreatedTime() { 35 | return createdTime; 36 | } 37 | @Column(updatable = false, name = "modified_time") 38 | @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") 39 | public Date getModifiedTime() { 40 | return modifiedTime; 41 | } 42 | public void setCreatedTime(Date createdTime) { 43 | this.createdTime = createdTime; 44 | } 45 | 46 | public void setModifiedTime(Date modifiedTime) { 47 | this.modifiedTime = modifiedTime; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/Entity.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity; 2 | import java.io.Serializable; 3 | 4 | /** 5 | * Created by ZLF on 2017/6/13. 6 | */ 7 | public interface Entity { 8 | ID getId(); 9 | void setId(final ID id); 10 | boolean isNew(); 11 | } 12 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/user/LiveUser.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.user; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | /** 10 | * @Description:直播用户 11 | * @Author: zhangliangfu 12 | * @Create on: 2019-07-12 14:19 13 | */ 14 | @Data 15 | @Entity 16 | @Table(name = "live_user") 17 | public class LiveUser extends BaseEntity { 18 | private String password; 19 | private String title; 20 | private String name; 21 | private String email; 22 | private String phone; 23 | private String avatar; 24 | private boolean useFlag; 25 | } 26 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/user/SystemDic.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.user; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | import org.apache.commons.lang3.builder.ToStringBuilder; 6 | 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | 10 | /** 11 | * <系统字典表> 12 | * 13 | * @author 14 | * @version [1.0, 2015年7月17日] 15 | */ 16 | @Data 17 | @Entity 18 | @Table(name = "system_dic") 19 | public class SystemDic extends BaseEntity 20 | { 21 | 22 | /** 23 | * 注释内容 24 | */ 25 | 26 | private Integer type; 27 | 28 | private String name; 29 | 30 | private String value; 31 | 32 | private Integer sort; 33 | 34 | 35 | @Override 36 | public String toString() 37 | { 38 | return ToStringBuilder.reflectionToString(this); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/user/SystemMenu.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.user; 2 | import com.easy.live.streaming.data.entity.BaseEntity; 3 | import com.fasterxml.jackson.annotation.JsonProperty; 4 | import lombok.Data; 5 | import org.apache.commons.lang3.builder.ToStringBuilder; 6 | 7 | import javax.persistence.Column; 8 | import javax.persistence.Entity; 9 | import javax.persistence.Table; 10 | import javax.persistence.Transient; 11 | import java.util.List; 12 | 13 | /** 14 | * <系统菜单实体类> 15 | * 16 | */ 17 | @Data 18 | @Entity 19 | @Table(name = "system_menu") 20 | public class SystemMenu extends BaseEntity 21 | { 22 | private static final long serialVersionUID = 12131231231233L; 23 | 24 | private String name; 25 | 26 | private String link; 27 | 28 | private Integer sort; 29 | 30 | @JsonProperty("parentId") 31 | @Column(name = "parent_id") 32 | private Integer parentId; 33 | 34 | @Transient 35 | private List childMenus; 36 | 37 | @Override 38 | public String toString() 39 | { 40 | return ToStringBuilder.reflectionToString(this); 41 | } 42 | 43 | 44 | 45 | } -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/user/SystemRole.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.user; 2 | import com.easy.live.streaming.data.entity.BaseEntity; 3 | import com.fasterxml.jackson.annotation.JsonIgnore; 4 | import lombok.Data; 5 | import org.apache.commons.lang3.builder.ToStringBuilder; 6 | 7 | import javax.persistence.*; 8 | import java.util.HashSet; 9 | import java.util.Set; 10 | 11 | /** 12 | * <角色实体类> 13 | * 14 | */ 15 | @Data 16 | @Entity 17 | @Table(name = "system_role") 18 | public class SystemRole extends BaseEntity 19 | { 20 | /** 21 | * 注释 22 | */ 23 | private static final long serialVersionUID = 3260110878096913809L; 24 | 25 | private String name; 26 | 27 | private String remark; 28 | 29 | @Column(name = "use_flag") 30 | private Boolean useFlag=true; 31 | 32 | @JsonIgnore 33 | @ManyToMany(fetch = FetchType.EAGER) 34 | @JoinTable(name = "system_role_menu_rel", joinColumns = { 35 | @JoinColumn(name = "role_id") }, inverseJoinColumns = { 36 | @JoinColumn(name = "menu_id") }) 37 | //@Cache(region = "all", usage = CacheConcurrencyStrategy.READ_WRITE) 38 | private Set menuSet = new HashSet<>(); 39 | 40 | @Override 41 | public String toString() 42 | { 43 | return ToStringBuilder.reflectionToString(this); 44 | } 45 | } -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/user/SystemUser.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.user; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import com.fasterxml.jackson.annotation.JsonIgnore; 5 | import lombok.Data; 6 | import org.apache.commons.lang3.builder.ToStringBuilder; 7 | 8 | import javax.persistence.*; 9 | import java.util.HashSet; 10 | import java.util.Set; 11 | 12 | /** 13 | * <用户表> 14 | * 15 | */ 16 | @Data 17 | @Entity 18 | @Table(name = "system_user") 19 | public class SystemUser extends BaseEntity 20 | { 21 | /** 22 | * 注释 23 | */ 24 | private static final long serialVersionUID = 1007873213039304224L; 25 | 26 | private String account; 27 | 28 | private String name; 29 | 30 | // 不持久化到数据库,也不显示在Restful接口的属性. 31 | @Transient 32 | @JsonIgnore 33 | private String plainPassword; 34 | @JsonIgnore 35 | private String password; 36 | @JsonIgnore 37 | private String salt; 38 | //类型(超级用户|普通用户) 39 | private Integer type; 40 | 41 | @Column(name = "use_flag") 42 | private Boolean useFlag=true; 43 | 44 | private String phone; 45 | 46 | private String img; 47 | 48 | private String email; 49 | 50 | private String remark; 51 | 52 | private String address; 53 | 54 | private String company; 55 | 56 | @JsonIgnore 57 | @ManyToMany(fetch = FetchType.EAGER) 58 | @JoinTable(name = "system_user_role_rel", joinColumns = { 59 | @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") }) 60 | private Set roleSet = new HashSet<>(); 61 | 62 | 63 | @Override 64 | public String toString() 65 | { 66 | return ToStringBuilder.reflectionToString(this); 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/video/VideoLiveCate.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.video; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | 10 | /** 11 | * @Description:直播分类 12 | * @Author: zhangliangfu 13 | * @Create on: 2019-06-17 16:18 14 | */ 15 | 16 | @Data 17 | @Entity 18 | @Table(name = "video_live_cate") 19 | public class VideoLiveCate extends BaseEntity { 20 | 21 | private String name; 22 | 23 | } 24 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/video/VideoLiveRoom.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.video; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | 6 | import javax.persistence.Column; 7 | import javax.persistence.Entity; 8 | import javax.persistence.Table; 9 | import javax.persistence.Transient; 10 | import java.util.List; 11 | 12 | /** 13 | * @Description:视频直播房间 14 | * @Author: zhangliangfu 15 | * @Create on: 2019-06-17 16:04 16 | */ 17 | 18 | @Data 19 | @Entity 20 | @Table(name = "video_live_room") 21 | public class VideoLiveRoom extends BaseEntity { 22 | 23 | //用户ID 24 | @Column(name = "user_id") 25 | private Integer userId; 26 | 27 | //房间名称 28 | @Column(name = "room_name") 29 | private String roomName; 30 | 31 | //标题 32 | @Column(name = "title") 33 | private String title; 34 | 35 | //观看人数 36 | @Column(name = "viewer_count") 37 | private Integer viewerCount; 38 | 39 | //封面 40 | @Column(name = "cover") 41 | private String cover; 42 | 43 | //类型 44 | @Column(name = "cate_id") 45 | private Integer cateId; 46 | 47 | //标签 48 | @Transient 49 | private List tags; 50 | } 51 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/video/VideoLiveRoomTag.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.video; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | /** 10 | * @Description:房间标签 11 | * @Author: zhangliangfu 12 | * @Create on: 2019-06-17 17:04 13 | */ 14 | 15 | @Data 16 | @Entity 17 | @Table(name = "video_live_room_tag") 18 | public class VideoLiveRoomTag extends BaseEntity { 19 | private Integer roomId; 20 | private Integer tagId; 21 | } 22 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/entity/video/VideoLiveTag.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.entity.video; 2 | 3 | import com.easy.live.streaming.data.entity.BaseEntity; 4 | import lombok.Data; 5 | 6 | import javax.persistence.Entity; 7 | import javax.persistence.Table; 8 | 9 | /** 10 | * @Description:直播标签 11 | * @Author: zhangliangfu 12 | * @Create on: 2019-06-17 16:17 13 | */ 14 | 15 | @Data 16 | @Entity 17 | @Table(name = "video_live_tag") 18 | public class VideoLiveTag extends BaseEntity { 19 | private String name; 20 | } 21 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/filter/AcquireUserInfoInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.filter; 2 | 3 | import com.easy.live.streaming.data.cache.ThreadLocalUserCache; 4 | import com.easy.live.streaming.data.cache.UserCache; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 7 | 8 | import javax.servlet.http.HttpServletRequest; 9 | import javax.servlet.http.HttpServletResponse; 10 | 11 | /** 12 | * Description: 获取用户信息拦截器 13 | * Author: zhangliangfu 14 | * Create on: 2019-08-12 17:56 15 | */ 16 | 17 | @Slf4j 18 | public class AcquireUserInfoInterceptor extends HandlerInterceptorAdapter { 19 | 20 | @Override 21 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ 22 | String userId = request.getHeader("userId"); 23 | UserCache userCache = new UserCache(); 24 | if (userId!=null){ 25 | userCache.setUserId(Integer.parseInt(userId)); 26 | } 27 | ThreadLocalUserCache.setUserCache(userCache); 28 | return true; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/BaseRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository; 2 | import org.springframework.data.jpa.repository.JpaSpecificationExecutor; 3 | import org.springframework.data.repository.NoRepositoryBean; 4 | import org.springframework.data.repository.PagingAndSortingRepository; 5 | 6 | import java.io.Serializable; 7 | 8 | /** 9 | * Created by ZLF on 2017/6/15. 10 | */ 11 | 12 | @NoRepositoryBean 13 | public interface BaseRepository extends JpaSpecificationExecutor,PagingAndSortingRepository { 15 | } 16 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/user/LiveUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.LiveUser; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | 6 | /** 7 | * @Description:直播用户 8 | * @Author: zhangliangfu 9 | * @Create on: 2019-07-12 14:24 10 | */ 11 | public interface LiveUserRepository extends BaseRepository { 12 | } 13 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/user/SystemDicRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.SystemDic; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | import org.springframework.data.jpa.repository.Query; 6 | 7 | import java.util.List; 8 | 9 | 10 | public interface SystemDicRepository extends BaseRepository 11 | { 12 | @Query("select s from SystemDic s where s.type =(?1) order by s.sort") 13 | List findDicByType(Integer type); 14 | } 15 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/user/SystemMenuRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.SystemMenu; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | import org.springframework.data.jpa.repository.Modifying; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import java.util.List; 9 | 10 | public interface SystemMenuRepository extends BaseRepository 11 | { 12 | /** 13 | * 根据父id获取menu 14 | * 15 | * @param parentId 16 | * @return 17 | */ 18 | List findByParentId(Integer parentId); 19 | 20 | /** 21 | * 根据id获取menu 22 | * 23 | * @param id 24 | * @return 25 | */ 26 | List findById(Integer id); 27 | 28 | /** 29 | * <根据父id获取menu> 30 | * 31 | * @param parentId 32 | * @return 33 | */ 34 | List findByParentIdOrderBySortAsc(Integer parentId); 35 | 36 | /** 37 | * <更新菜单排序> 38 | * 39 | * @param parentId 40 | * @param sort 41 | */ 42 | @Modifying 43 | @Query("update SystemMenu set sort=sort-1 where parentId =?1 and sort>?2") 44 | void updateSort(Integer parentId, Integer sort); 45 | 46 | /** 47 | * <根据父菜单 查询 最大排序号> 48 | * 49 | * @param parentId 50 | * @return 51 | */ 52 | @Query("select MAX(sort) from SystemMenu where parentId =?1") 53 | Integer getMaxSortNumByParentId(Integer parentId); 54 | 55 | /** 56 | * <查询菜单> 57 | * 58 | * @param menuIdList 59 | * @return 60 | */ 61 | @Query("select s from SystemMenu s where s.id in (?1)") 62 | List findByMenuIds(List menuIdList); 63 | 64 | @Query("select s from SystemMenu s order by s.sort") 65 | List findSortAll(); 66 | } 67 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/user/SystemRoleRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.SystemMenu; 4 | import com.easy.live.streaming.data.entity.user.SystemRole; 5 | import com.easy.live.streaming.data.repository.BaseRepository; 6 | import org.springframework.data.jpa.repository.Query; 7 | 8 | import java.util.List; 9 | 10 | public interface SystemRoleRepository extends BaseRepository 11 | { 12 | /** 13 | * <查询不在 所选roleid的 其他角色列表> 14 | * 15 | * @param roleId 16 | * @return 17 | */ 18 | List findByIdNotIn(List roleId); 19 | 20 | /** 21 | * <查询所有角色> 22 | * 23 | * @return 24 | */ 25 | List findAll(); 26 | 27 | /** 28 | * <一句话功能简述> 29 | * 30 | * @param roleName 31 | * @return 32 | */ 33 | SystemRole findByName(String roleName); 34 | 35 | /** 36 | * <一句话功能简述> 37 | * 38 | * @param roleId 39 | * @param roleName 40 | * @return 41 | */ 42 | @Query("select p from SystemRole p where name =?2 and id !=?1") 43 | SystemRole findByIdAndName(Integer roleId, String roleName); 44 | 45 | @Query("select distinct m from SystemRole p join p.menuSet m where p.id in (?1)") 46 | List findMenusByRoleIds(List roleIds); 47 | 48 | } 49 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/user/SystemUserRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.user; 2 | 3 | 4 | import com.easy.live.streaming.data.entity.user.SystemUser; 5 | import com.easy.live.streaming.data.repository.BaseRepository; 6 | 7 | public interface SystemUserRepository extends BaseRepository 8 | { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/video/VideoLiveCateRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveCate; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | 6 | /** 7 | * @Description: 8 | * @Author: zhangliangfu 9 | * @Create on: 2019-06-17 17:23 10 | */ 11 | public interface VideoLiveCateRepository extends BaseRepository { 12 | } 13 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/video/VideoLiveRoomRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoom; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | 6 | /** 7 | * @Description:视频直播房间 8 | * @Author: zhangliangfu 9 | * @Create on: 2019-06-17 16:04 10 | */ 11 | public interface VideoLiveRoomRepository extends BaseRepository { 12 | } 13 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/video/VideoLiveRoomTagRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoomTag; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | 6 | /** 7 | * @Description: 8 | * @Author: zhangliangfu 9 | * @Create on: 2019-06-17 17:23 10 | */ 11 | public interface VideoLiveRoomTagRepository extends BaseRepository { 12 | } 13 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/repository/video/VideoLiveTagRepository.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.repository.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveTag; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | 6 | /** 7 | * @Description: 8 | * @Author: zhangliangfu 9 | * @Create on: 2019-06-17 17:23 10 | */ 11 | public interface VideoLiveTagRepository extends BaseRepository { 12 | } 13 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/BaseService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service; 2 | 3 | import com.easy.live.streaming.common.util.jpa.PageUtil; 4 | import com.easy.live.streaming.data.repository.BaseRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.data.domain.Page; 7 | import org.springframework.data.domain.PageRequest; 8 | import org.springframework.data.domain.Sort; 9 | import org.springframework.data.jpa.domain.Specification; 10 | import org.springframework.transaction.annotation.Transactional; 11 | 12 | import javax.persistence.EntityManager; 13 | import javax.persistence.PersistenceContext; 14 | import java.io.Serializable; 15 | import java.lang.reflect.ParameterizedType; 16 | import java.lang.reflect.Type; 17 | import java.util.List; 18 | import java.util.Map; 19 | 20 | /** 21 | * Created by ZLF on 2017/6/15. 22 | */ 23 | 24 | 25 | public abstract class BaseService { 26 | @Autowired 27 | private BaseRepository baseRepository; 28 | private Class entityClass; 29 | 30 | @PersistenceContext 31 | public EntityManager em; 32 | 33 | public BaseService() 34 | { 35 | if (null == entityClass) 36 | { 37 | Type type = getClass().getGenericSuperclass(); 38 | if (!(type instanceof ParameterizedType)) 39 | { 40 | type = getClass().getSuperclass().getGenericSuperclass(); 41 | } 42 | entityClass = (Class)((ParameterizedType)type).getActualTypeArguments()[0]; 43 | } 44 | } 45 | 46 | @Transactional(readOnly = false) 47 | public T save(T entity) 48 | { 49 | return baseRepository.save(entity); 50 | } 51 | 52 | @Transactional(readOnly = false) 53 | public void delete(ID id) 54 | { 55 | baseRepository.delete(id); 56 | } 57 | 58 | @Transactional(readOnly = false) 59 | public void delete(T entity) 60 | { 61 | baseRepository.delete(entity); 62 | } 63 | 64 | @Transactional(readOnly = false) 65 | public void delete(List entities) 66 | { 67 | baseRepository.delete(entities); 68 | } 69 | 70 | @Transactional(readOnly = false) 71 | public void deleteAll() 72 | { 73 | baseRepository.deleteAll(); 74 | } 75 | 76 | @Transactional(readOnly = true) 77 | public T findOne(ID id) 78 | { 79 | return baseRepository.findOne(id); 80 | } 81 | 82 | @Transactional(readOnly = true) 83 | public T findOne(Map searchParams) { 84 | Specification spec = PageUtil.buildSpecification(searchParams, entityClass); 85 | return baseRepository.findOne(spec); 86 | } 87 | 88 | @Transactional(readOnly = true) 89 | public boolean exists(ID id) 90 | { 91 | return baseRepository.exists(id); 92 | } 93 | 94 | @Transactional(readOnly = true) 95 | public Long count(Map searchParams) 96 | { 97 | Specification spec = PageUtil.buildSpecification(searchParams, entityClass); 98 | return baseRepository.count(spec); 99 | } 100 | 101 | @Transactional(readOnly = true) 102 | public List findAll() 103 | { 104 | return (List)baseRepository.findAll(); 105 | } 106 | 107 | @Transactional(readOnly = true) 108 | public List findAll(List ids) 109 | { 110 | return (List)baseRepository.findAll(ids); 111 | } 112 | 113 | @Transactional(readOnly = true) 114 | public Page findAll(Map searchParams, int pageNumber, int pageSize) 115 | { 116 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize); 117 | return findAll(searchParams, pageRequest); 118 | } 119 | 120 | @Transactional(readOnly = true) 121 | public Page findAll(Map searchParams, int pageNumber, int pageSize, Sort.Direction direction, 122 | String orderBy) 123 | { 124 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize, direction, orderBy); 125 | return findAll(searchParams, pageRequest); 126 | } 127 | 128 | @Transactional(readOnly = true) 129 | public Page findAll(Map searchParams, int pageNumber, int pageSize, Sort.Direction direction, 130 | String... orderBys) 131 | { 132 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize, direction, orderBys); 133 | return findAll(searchParams, pageRequest); 134 | } 135 | 136 | @Transactional(readOnly = true) 137 | public Page findAll(Map searchParams, int pageNumber, int pageSize, Sort.Direction direction, 138 | List orderBys) 139 | { 140 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize, direction, orderBys); 141 | return findAll(searchParams, pageRequest); 142 | } 143 | 144 | @Transactional(readOnly = true) 145 | public Page findAll(Map searchParams, int pageNumber, int pageSize, List directions, 146 | List orderBys) 147 | { 148 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize, directions, orderBys); 149 | return findAll(searchParams, pageRequest); 150 | } 151 | 152 | @Transactional(readOnly = true) 153 | public Page findAll(Map searchParams, PageRequest pageRequest) 154 | { 155 | Specification spec = PageUtil.buildSpecification(searchParams, entityClass); 156 | return baseRepository.findAll(spec, pageRequest); 157 | } 158 | 159 | @Transactional(readOnly = true) 160 | public List findAll(Map searchParams) 161 | { 162 | Specification spec = PageUtil.buildSpecification(searchParams, entityClass); 163 | return baseRepository.findAll(spec); 164 | } 165 | @Transactional(readOnly = true) 166 | public List findAll(Map searchParams, Sort sort) 167 | { 168 | Specification spec = PageUtil.buildSpecification(searchParams, entityClass); 169 | return baseRepository.findAll(spec,sort); 170 | } 171 | } 172 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/user/LiveUserService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.LiveUser; 4 | import com.easy.live.streaming.data.service.BaseService; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.springframework.stereotype.Service; 7 | 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | /** 12 | * @Description:直播用户 13 | * @Author: zhangliangfu 14 | * @Create on: 2019-07-12 14:25 15 | */ 16 | 17 | @Slf4j 18 | @Service 19 | public class LiveUserService extends BaseService { 20 | 21 | public LiveUser findLiveUserByName(String name){ 22 | Map searchParams = new HashMap<>(); 23 | searchParams.put("EQ_name", name); 24 | LiveUser one = null; 25 | try { 26 | one = findOne(searchParams); 27 | }catch (Exception e){ 28 | log.error("查询出错:{}", e.getMessage()); 29 | } 30 | return one; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/user/SystemDicService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.user; 2 | 3 | import com.easy.live.streaming.data.entity.user.SystemDic; 4 | import com.easy.live.streaming.data.repository.user.SystemDicRepository; 5 | import com.easy.live.streaming.data.service.BaseService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * 系统字典表. 13 | * 14 | * @author 15 | */ 16 | @Service 17 | public class SystemDicService extends BaseService 18 | { 19 | @Autowired 20 | private SystemDicRepository systemDicRepository; 21 | public List findDicByType(Integer type){ 22 | return systemDicRepository.findDicByType(type); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/user/SystemMenuService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.user; 2 | import com.easy.live.streaming.common.util.jpa.PageUtil; 3 | import com.easy.live.streaming.data.entity.user.SystemMenu; 4 | import com.easy.live.streaming.data.repository.user.SystemMenuRepository; 5 | import com.easy.live.streaming.data.service.BaseService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.data.domain.Page; 8 | import org.springframework.data.domain.PageRequest; 9 | import org.springframework.data.jpa.domain.Specification; 10 | import org.springframework.stereotype.Service; 11 | 12 | import java.util.List; 13 | import java.util.Map; 14 | 15 | /** 16 | * <功能详细描述> 17 | * 18 | */ 19 | @Service 20 | public class SystemMenuService extends BaseService 21 | { 22 | 23 | @Autowired 24 | private SystemMenuRepository systemMenuRepository; 25 | 26 | /** 27 | * <根据 parentId获得menu列表> 28 | * 29 | * @param parentId 30 | * @return 31 | */ 32 | public List getMenuByParentId(Integer parentId) 33 | { 34 | List list = systemMenuRepository.findByParentIdOrderBySortAsc(parentId); 35 | for (SystemMenu menu : list) 36 | { 37 | if (menu != null) 38 | { 39 | List childMenus = getMenuByParentId(menu.getId()); 40 | menu.setChildMenus(childMenus); 41 | } 42 | } 43 | return list; 44 | } 45 | 46 | /** 47 | * <保存menu> 48 | * 49 | * @param menu 50 | */ 51 | public void saveMenu(SystemMenu menu) 52 | { 53 | systemMenuRepository.save(menu); 54 | } 55 | 56 | /** 57 | * <更新菜单信息> 58 | * 59 | * @param 60 | */ 61 | public void updateMenu(Integer parentId, Integer sort) 62 | { 63 | systemMenuRepository.updateSort(parentId, sort); 64 | } 65 | 66 | /** 67 | * <根据parentId获得最大的sortnum> 68 | * 69 | * @param parentId 70 | * @return 71 | */ 72 | public Integer getMaxSortNumByParentId(Integer parentId) 73 | { 74 | return systemMenuRepository.getMaxSortNumByParentId(parentId); 75 | } 76 | 77 | /** 78 | * <获得菜单管理信息> 79 | * 80 | * @param searchParams 81 | * @param pageNumber 82 | * @param pageSize 83 | * @return 84 | */ 85 | public Page getMenuList(Map searchParams, int pageNumber, int pageSize) 86 | { 87 | String parentId = "parentId"; 88 | String sortnum = "sort"; 89 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize, parentId, sortnum); 90 | Specification spec = PageUtil.buildSpecification(searchParams, SystemMenu.class); 91 | 92 | return systemMenuRepository.findAll(spec, pageRequest); 93 | } 94 | 95 | /** 96 | * <获取菜单列表> 97 | * 98 | * @return 99 | */ 100 | public List getAllMenuList() 101 | { 102 | return (List)systemMenuRepository.findAll(); 103 | } 104 | 105 | /** 106 | * <根据menuId查询> 107 | * 108 | * @param id 109 | * @return 110 | */ 111 | public SystemMenu getMenuById(Integer id) 112 | { 113 | return systemMenuRepository.findOne(id); 114 | } 115 | 116 | } 117 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/user/SystemRoleService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.user; 2 | 3 | import com.easy.live.streaming.common.util.jpa.PageUtil; 4 | import com.easy.live.streaming.data.entity.user.SystemRole; 5 | import com.easy.live.streaming.data.repository.user.SystemRoleRepository; 6 | import com.easy.live.streaming.data.service.BaseService; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.data.domain.Page; 9 | import org.springframework.data.domain.PageRequest; 10 | import org.springframework.data.jpa.domain.Specification; 11 | import org.springframework.stereotype.Service; 12 | 13 | import java.util.List; 14 | import java.util.Map; 15 | 16 | 17 | /** 18 | * <角色管理接口> <处理角色信息接口> 19 | * 20 | */ 21 | @Service 22 | public class SystemRoleService extends BaseService 23 | { 24 | 25 | @Autowired 26 | private SystemRoleRepository systemRoleRepository; 27 | 28 | 29 | /** 30 | * <保存角色信息> 31 | * 32 | * @param entity 33 | */ 34 | public void saveRole(SystemRole entity) 35 | { 36 | systemRoleRepository.save(entity); 37 | } 38 | 39 | 40 | /** 41 | * <根据id获得角色信息> 42 | * 43 | * @param id 44 | * @return 45 | * @see [类、类#方法、类#成员] 46 | */ 47 | public SystemRole getRole(Integer id) 48 | { 49 | return systemRoleRepository.findOne(id); 50 | } 51 | 52 | /** 53 | * <获得所有角色信息> 54 | * 55 | * @return 56 | */ 57 | public List getAllRole() 58 | { 59 | return systemRoleRepository.findAll(); 60 | } 61 | 62 | /** 63 | * <删除角色信息> 64 | * 65 | * @param id 66 | */ 67 | public void deleteRole(Integer id) 68 | { 69 | systemRoleRepository.delete(id); 70 | } 71 | 72 | /** 73 | * <获得角色信息列表> 74 | * 75 | * @param searchParams 76 | * @param pageNumber 77 | * @param pageSize 78 | * @return 79 | */ 80 | public Page getRoleList(Map searchParams, int pageNumber, int pageSize) 81 | { 82 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize); 83 | Specification spec = PageUtil.buildSpecification(searchParams, SystemRole.class); 84 | return systemRoleRepository.findAll(spec, pageRequest); 85 | } 86 | 87 | /** 88 | * <获得用户没有绑定的角色信息列表> 89 | * 90 | * @param roleId 91 | * @return 92 | */ 93 | public List getRoleLists(List roleId) 94 | { 95 | return systemRoleRepository.findByIdNotIn(roleId); 96 | 97 | } 98 | 99 | /** 100 | * <根据角色名查询> 101 | * 102 | * @param roleName 103 | * @return 104 | */ 105 | public SystemRole findRoleByName(String roleName) 106 | { 107 | return systemRoleRepository.findByName(roleName); 108 | } 109 | 110 | /** 111 | * <一句话功能简述> 112 | * 113 | * @param roleName 114 | * @param roleId 115 | * @return 116 | */ 117 | public SystemRole findRoleByNameAndId(String roleName, Integer roleId) 118 | { 119 | return systemRoleRepository.findByIdAndName(roleId, roleName); 120 | } 121 | 122 | /** 123 | * <一句话功能简述> 124 | * 125 | * @param roleId 126 | * @return 127 | */ 128 | public SystemRole findOne(Integer roleId) 129 | { 130 | return systemRoleRepository.findOne(roleId); 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/user/SystemUserService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.user; 2 | import com.easy.live.streaming.common.util.jpa.PageUtil; 3 | import com.easy.live.streaming.data.entity.user.SystemMenu; 4 | import com.easy.live.streaming.data.entity.user.SystemRole; 5 | import com.easy.live.streaming.data.entity.user.SystemUser; 6 | import com.easy.live.streaming.data.repository.user.SystemMenuRepository; 7 | import com.easy.live.streaming.data.repository.user.SystemUserRepository; 8 | import com.easy.live.streaming.data.service.BaseService; 9 | import com.google.common.collect.Lists; 10 | import org.apache.commons.collections.map.HashedMap; 11 | import org.springframework.beans.factory.annotation.Autowired; 12 | import org.springframework.data.domain.Page; 13 | import org.springframework.data.domain.PageRequest; 14 | import org.springframework.data.domain.Sort; 15 | import org.springframework.data.jpa.domain.Specification; 16 | import org.springframework.stereotype.Service; 17 | import org.springframework.util.CollectionUtils; 18 | 19 | import java.util.List; 20 | import java.util.Map; 21 | import java.util.Set; 22 | 23 | /** 24 | * <用户信息接口> 25 | * 26 | * 27 | */ 28 | @Service 29 | public class SystemUserService extends BaseService 30 | { 31 | @Autowired 32 | private SystemUserRepository systemUserRepository; 33 | 34 | @Autowired 35 | private SystemMenuRepository systemMenuRepository; 36 | 37 | /** 38 | * <查询分页返回的用户列表信息> 39 | * 40 | * @param searchParams 查询参数 41 | * @param pageNumber 查询起始页 42 | * @param pageSize 查询页大小 43 | * @return 44 | * 45 | */ 46 | public Page getUsers(Map searchParams, int pageNumber, int pageSize) 47 | { 48 | PageRequest pageRequest = PageUtil.buildPageRequest(pageNumber, pageSize); 49 | Specification spec = PageUtil.buildSpecification(searchParams, SystemUser.class); 50 | return systemUserRepository.findAll(spec, pageRequest); 51 | } 52 | 53 | public List findUser(Map searchParams, Sort sort) 54 | { 55 | Specification spec = PageUtil.buildSpecification(searchParams, SystemUser.class); 56 | return systemUserRepository.findAll(spec, sort); 57 | } 58 | 59 | public List getMenuByUserId(Integer userId) { 60 | SystemUser user = systemUserRepository.findOne(userId); 61 | System.out.println(userId+"*****************"); 62 | List menus = Lists.newArrayList(); 63 | if (user.getType()==0) { 64 | menus = (List) systemMenuRepository.findAll(); 65 | } else { 66 | Set roles = user.getRoleSet(); 67 | if (roles.isEmpty()) 68 | return Lists.newArrayList(); 69 | for (SystemRole role : roles) { 70 | for (SystemMenu menu : role.getMenuSet()) { 71 | if (!menus.contains(menu)) { 72 | menus.add(menu); 73 | } 74 | } 75 | } 76 | } 77 | return menus; 78 | } 79 | 80 | /** 81 | * 根据userId获取菜单列表 当前菜单较少,采取一次查询后封装 后续菜单增加后,可多次查询DB再封装 82 | * 83 | * @param userId 84 | * @return 85 | * @see [类、类#方法、类#成员] 86 | */ 87 | public List getMenuTreeByUserId(Integer userId) 88 | { 89 | List retMenus = Lists.newArrayList(); 90 | List menus= getMenuByUserId(userId); 91 | ListallMenus=(List) systemMenuRepository.findSortAll(); 92 | if (!CollectionUtils.isEmpty(menus)){ 93 | for(SystemMenu menu:allMenus){ 94 | for (SystemMenu menut : menus){ 95 | if(menut.getParentId()!=null&&menut.getParentId().equals(menu.getId())){ 96 | if(!retMenus.contains(menu)) 97 | retMenus.add(menu); 98 | if(menu.getChildMenus()==null){ 99 | menu.setChildMenus(Lists.newArrayList()); 100 | } 101 | menu.getChildMenus().add(menut); 102 | } 103 | } 104 | } 105 | } 106 | return retMenus; 107 | } 108 | public List getMenuTree() 109 | { 110 | List retMenus = Lists.newArrayList(); 111 | List menus=(List) systemMenuRepository.findAll(); 112 | ListallMenus=(List) systemMenuRepository.findSortAll(); 113 | if (!CollectionUtils.isEmpty(menus)){ 114 | for(SystemMenu menu:allMenus){ 115 | for (SystemMenu menut : menus){ 116 | if(menut.getParentId()!=null&&menut.getParentId().equals(menu.getId())){ 117 | if(!retMenus.contains(menu)) 118 | retMenus.add(menu); 119 | if(menu.getChildMenus()==null){ 120 | menu.setChildMenus(Lists.newArrayList()); 121 | } 122 | menu.getChildMenus().add(menut); 123 | } 124 | } 125 | } 126 | } 127 | return retMenus; 128 | } 129 | 130 | public SystemUser findUserByAccount(String account){ 131 | Map searchParams = new HashedMap(); 132 | searchParams.put("EQ_account",account); 133 | List systemUsers = findAll(searchParams); 134 | if (CollectionUtils.isEmpty(systemUsers)){ 135 | return null; 136 | } 137 | return systemUsers.get(0); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/video/VideoLiveCateService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveCate; 4 | import com.easy.live.streaming.data.service.BaseService; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @Description: 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-17 17:25 11 | */ 12 | @Service 13 | public class VideoLiveCateService extends BaseService { 14 | } 15 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/video/VideoLiveRoomService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoom; 4 | import com.easy.live.streaming.data.service.BaseService; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @Description:视频直播房间 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-17 16:03 11 | */ 12 | 13 | @Service 14 | public class VideoLiveRoomService extends BaseService { 15 | } 16 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/video/VideoLiveRoomTagService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoomTag; 4 | import com.easy.live.streaming.data.service.BaseService; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @Description: 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-17 17:25 11 | */ 12 | 13 | @Service 14 | public class VideoLiveRoomTagService extends BaseService { 15 | } 16 | -------------------------------------------------------------------------------- /live-data/src/main/java/com/easy/live/streaming/data/service/video/VideoLiveTagService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.data.service.video; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveTag; 4 | import com.easy.live.streaming.data.service.BaseService; 5 | import org.springframework.stereotype.Service; 6 | 7 | /** 8 | * @Description: 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-17 17:25 11 | */ 12 | 13 | @Service 14 | public class VideoLiveTagService extends BaseService { 15 | } 16 | -------------------------------------------------------------------------------- /live-gateway/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-gateway 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-eureka 18 | 19 | 20 | org.springframework.boot 21 | spring-boot-actuator 22 | 1.5.16.RELEASE 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-zuul 27 | 28 | 29 | org.springframework.boot 30 | spring-boot-actuator 31 | 32 | 33 | 34 | 35 | org.springframework.cloud 36 | spring-cloud-starter-hystrix 37 | 38 | 39 | org.springframework.cloud 40 | spring-cloud-starter-feign 41 | 42 | 43 | org.springframework.cloud 44 | spring-cloud-starter-config 45 | 46 | 47 | mysql 48 | mysql-connector-java 49 | 50 | 51 | com.easy 52 | live-data 53 | 54 | 55 | com.easy 56 | live-servants 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | org.springframework.boot 65 | spring-boot-maven-plugin 66 | 1.5.2.RELEASE 67 | 68 | 69 | 70 | repackage 71 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/GatewayApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway; 2 | 3 | import org.springframework.boot.autoconfigure.domain.EntityScan; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.cloud.client.SpringCloudApplication; 6 | import org.springframework.cloud.netflix.feign.EnableFeignClients; 7 | import org.springframework.cloud.netflix.hystrix.EnableHystrix; 8 | import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 9 | import org.springframework.context.annotation.ComponentScan; 10 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 11 | 12 | /** 13 | * @Description:网关服务 14 | * @Author: zhangliangfu 15 | * @Create on: 2019-06-12 19:02 16 | */ 17 | 18 | @SpringCloudApplication 19 | @EnableZuulProxy 20 | @EnableHystrix 21 | @EnableFeignClients(basePackages={"com.easy"}) 22 | @ComponentScan(basePackages={"com.easy"}) 23 | @EnableJpaRepositories(basePackages = "com.easy") 24 | @EntityScan(basePackages = "com.easy") 25 | public class GatewayApplication { 26 | public static void main(String[] args) { 27 | new SpringApplicationBuilder(GatewayApplication.class).web(true).run(args); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/controller/HandleErrorController.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.controller; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.autoconfigure.web.ErrorController; 7 | import org.springframework.http.HttpStatus; 8 | import org.springframework.http.ResponseEntity; 9 | import org.springframework.stereotype.Controller; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | import org.springframework.web.bind.annotation.ResponseBody; 12 | 13 | import javax.servlet.http.HttpServletRequest; 14 | 15 | /** 16 | * Description: 错误处理controller 17 | * Author: zhangliangfu 18 | * Create on: 2019-08-07 16:03 19 | */ 20 | @Controller 21 | public class HandleErrorController implements ErrorController { 22 | 23 | @Value("${error.path:/error}") 24 | private String errorPath; 25 | 26 | @Override 27 | public String getErrorPath() { 28 | return errorPath; 29 | } 30 | 31 | @RequestMapping(value = "${error.path:/error}") 32 | public @ResponseBody 33 | ResponseEntity error(HttpServletRequest request) { 34 | 35 | int status = getErrorStatus(request); 36 | String errorMessage = getErrorMessage(request); 37 | BaseOutput baseOutput = new BaseOutput(status, errorMessage); 38 | return ResponseEntity.status(status).body(JSON.toJSONString(baseOutput)); 39 | } 40 | 41 | private int getErrorStatus(HttpServletRequest request) { 42 | Integer statusCode = (Integer)request.getAttribute("javax.servlet.error.status_code"); 43 | return statusCode != null ? statusCode : HttpStatus.INTERNAL_SERVER_ERROR.value(); 44 | } 45 | 46 | private String getErrorMessage(HttpServletRequest request) { 47 | final Throwable exc = (Throwable) request.getAttribute("javax.servlet.error.exception"); 48 | return exc != null ? exc.getMessage() : "Unexpected error occurred"; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/filter/AccessLogFilter.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.filter; 2 | 3 | import com.easy.live.streaming.data.cache.SimpleCacheService; 4 | import com.netflix.zuul.ZuulFilter; 5 | import com.netflix.zuul.context.RequestContext; 6 | import com.netflix.zuul.exception.ZuulException; 7 | import lombok.extern.slf4j.Slf4j; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.stereotype.Component; 10 | 11 | import javax.servlet.http.HttpServletRequest; 12 | 13 | /** 14 | * Description: 访问日志 15 | * Author: zhangliangfu 16 | * Create on: 2019-07-25 10:21 17 | */ 18 | @Slf4j 19 | @Component 20 | public class AccessLogFilter extends ZuulFilter { 21 | 22 | @Autowired 23 | private SimpleCacheService cacheService; 24 | 25 | @Override 26 | public String filterType() { 27 | return "pre"; 28 | } 29 | 30 | @Override 31 | public int filterOrder() { 32 | return 0; 33 | } 34 | 35 | @Override 36 | public boolean shouldFilter() { 37 | return true; 38 | } 39 | 40 | @Override 41 | public Object run() throws ZuulException { 42 | RequestContext requestContext = RequestContext.getCurrentContext(); 43 | HttpServletRequest request = requestContext.getRequest(); 44 | requestContext.addZuulRequestHeader("sessionId", request.getSession().getId()); 45 | String requestURI = request.getRequestURI(); 46 | log.debug("通过网关请求接口:{}", requestURI); 47 | return null; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/filter/HandleZuulExceptionFilter.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.filter; 2 | 3 | import com.alibaba.fastjson.JSON; 4 | import com.easy.live.streaming.common.config.Constants; 5 | import com.easy.live.streaming.data.bean.BaseOutput; 6 | import com.netflix.zuul.ZuulFilter; 7 | import com.netflix.zuul.context.RequestContext; 8 | import com.netflix.zuul.exception.ZuulException; 9 | import lombok.extern.slf4j.Slf4j; 10 | import org.springframework.stereotype.Component; 11 | import org.springframework.util.ReflectionUtils; 12 | 13 | /** 14 | * Description: 处理zuul异常过滤器 15 | * Author: zhangliangfu 16 | * Create on: 2019-08-05 17:15 17 | */ 18 | 19 | @Slf4j 20 | @Component 21 | public class HandleZuulExceptionFilter extends ZuulFilter { 22 | @Override 23 | public String filterType() { 24 | return "error"; 25 | } 26 | 27 | @Override 28 | public int filterOrder() { 29 | return Integer.MAX_VALUE; 30 | } 31 | 32 | @Override 33 | public boolean shouldFilter() { 34 | return true; 35 | } 36 | 37 | @Override 38 | public Object run(){ 39 | try { 40 | RequestContext ctx = RequestContext.getCurrentContext(); 41 | Object e = ctx.get("throwable"); 42 | 43 | if (e != null && e instanceof ZuulException) { 44 | log.error("检测到ZuulException, message:{}", ((ZuulException) e).getMessage()); 45 | BaseOutput baseOutput = new BaseOutput(Constants.RetMsg.EXCEPTION_LOGIN.code, 46 | Constants.RetMsg.EXCEPTION_LOGIN.msg); 47 | ctx.setResponseBody(JSON.toJSONString(baseOutput)); 48 | ctx.getResponse().setContentType("application/json"); 49 | } 50 | } 51 | catch (Exception ex) { 52 | log.error("Exception filtering in custom error filter", ex); 53 | ReflectionUtils.rethrowRuntimeException(ex); 54 | } 55 | return null; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/filter/LoginFilter.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.filter; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.data.cache.SimpleCacheService; 6 | import com.easy.live.streaming.data.cache.UserCache; 7 | import com.easy.live.streaming.servants.api.auth.servant.AuthServant; 8 | import com.netflix.zuul.ZuulFilter; 9 | import com.netflix.zuul.context.RequestContext; 10 | import com.netflix.zuul.exception.ZuulException; 11 | import lombok.extern.slf4j.Slf4j; 12 | import org.apache.shiro.SecurityUtils; 13 | import org.apache.shiro.subject.Subject; 14 | import org.springframework.beans.factory.annotation.Autowired; 15 | import org.springframework.stereotype.Component; 16 | 17 | /** 18 | * Description: 登录session过滤 19 | * Author: zhangliangfu 20 | * Create on: 2019-07-20 17:05 21 | */ 22 | 23 | @Slf4j 24 | @Component 25 | public class LoginFilter extends ZuulFilter { 26 | 27 | @Autowired 28 | private SimpleCacheService simpleCacheService; 29 | 30 | @Override 31 | public String filterType() { 32 | return "pre"; 33 | } 34 | 35 | @Override 36 | public int filterOrder() { 37 | return 1; 38 | } 39 | 40 | @Override 41 | public boolean shouldFilter() { 42 | RequestContext currentContext = RequestContext.getCurrentContext(); 43 | return needLogin(currentContext.getRequest().getRequestURI()); 44 | } 45 | 46 | /** 47 | * 带open的为开放接口,其余的都需要验证登录 48 | * @param requestUri 49 | * @return 50 | */ 51 | private boolean needLogin(String requestUri){ 52 | if (requestUri.contains("/open/")){ 53 | return false; 54 | }else { 55 | return true; 56 | } 57 | } 58 | 59 | @Override 60 | public Object run() throws ZuulException{ 61 | log.debug("检查用户是否登录"); 62 | 63 | //确定是否已经鉴定过身份 64 | RequestContext ctx = RequestContext.getCurrentContext(); 65 | String sessionId = ctx.getRequest().getSession().getId(); 66 | String key = Constants.LOGIN_CACHE + sessionId; 67 | UserCache userCache = simpleCacheService.get(key, UserCache.class); 68 | if (userCache==null) { 69 | ctx.remove("error.status_code"); 70 | throw new ZuulException(Constants.RetMsg.EXCEPTION_LOGIN.msg, Constants.RetMsg.EXCEPTION_LOGIN.code, 71 | Constants.RetMsg.EXCEPTION_LOGIN.msg); 72 | }else { 73 | ctx.addZuulRequestHeader("userId", userCache.getUserId()+""); 74 | simpleCacheService.add(key, userCache, Constants.LOGIN_CACHE_TIME); 75 | } 76 | return null; 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/handler/GlobalExceptionHandler.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.handler; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.common.exception.GlobalException; 5 | import lombok.extern.slf4j.Slf4j; 6 | import org.apache.shiro.authz.AuthorizationException; 7 | import org.apache.shiro.authz.UnauthorizedException; 8 | import org.springframework.web.bind.annotation.ControllerAdvice; 9 | import org.springframework.web.bind.annotation.ExceptionHandler; 10 | import org.springframework.web.bind.annotation.ResponseBody; 11 | 12 | /** 13 | * Description: 全局异常捕捉 14 | * Author: zhangliangfu 15 | * Create on: 2019-08-01 15:21 16 | */ 17 | 18 | @Slf4j 19 | @ControllerAdvice(basePackages = "com.easy") 20 | public class GlobalExceptionHandler { 21 | 22 | @ResponseBody 23 | @ExceptionHandler 24 | public Exception handleGlobalException(Exception e){ 25 | log.error(e.getMessage()); 26 | return new GlobalException(Constants.RetMsg.FAIL.code, e.getMessage()); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [Gateway] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-gateway/src/main/java/com/easy/live/streaming/gateway/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.gateway.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.CommandLineRunner; 7 | 8 | public class StartupRunner implements CommandLineRunner { 9 | 10 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 11 | 12 | @Value("${spring.profiles.active:default}") 13 | private String env; 14 | 15 | @Value("${spring.cloud.config.profile:default}") 16 | private String configEnv; 17 | 18 | @Override 19 | public void run(String... args) throws Exception { 20 | logger.info("spring.profiles.active:{}",env); 21 | logger.info("spring.cloud.config.profile:{}",configEnv); 22 | logger.info("网关服务启动成功"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-gateway/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: gateway-server 4 | profiles: 5 | active: dev 6 | server: 7 | port: 8003 8 | 9 | management: 10 | security: 11 | enabled: false -------------------------------------------------------------------------------- /live-gateway/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-gateway/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | easy: 2 | eurekaServer: http://live.registry.com:8001/eureka/ 3 | 4 | eureka: 5 | instance: 6 | lease-expiration-duration-in-seconds: 15 7 | lease-renewal-interval-in-seconds: 5 8 | preferIpAddress: true 9 | hostname: live.registry.com 10 | metadataMap: 11 | cluster: MAIN 12 | client: 13 | registryFetchIntervalSeconds: 5 14 | serviceUrl: 15 | defaultZone: ${easy.eurekaServer} 16 | 17 | spring: 18 | cloud: 19 | config: 20 | profile: dev 21 | discovery: 22 | enabled: true 23 | serviceId: config-server 24 | 25 | 26 | -------------------------------------------------------------------------------- /live-gateway/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ${FILE_LOG_PATTERN} 13 | UTF-8 14 | 15 | 16 | 17 | ${LOG_HOME}/gateway-server-%d{yyyy-MM-dd}.%i.log 18 | 19 | 20MB 20 | 60 21 | 20GB 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /live-registry/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-registry 13 | 1.0-SNAPSHOT 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-eureka-server 18 | 19 | 20 | 21 | 22 | 23 | 24 | org.springframework.boot 25 | spring-boot-maven-plugin 26 | 1.5.2.RELEASE 27 | 28 | 29 | 30 | repackage 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /live-registry/src/main/java/com/easy/live/streaming/registry/RegistryApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.registry; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.builder.SpringApplicationBuilder; 5 | import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; 6 | 7 | /** 8 | * @Description:注册中心 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-10 17:23 11 | */ 12 | 13 | @SpringBootApplication 14 | @EnableEurekaServer 15 | public class RegistryApplication { 16 | public static void main(String[] args) { 17 | new SpringApplicationBuilder(RegistryApplication.class).web(true).run(args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /live-registry/src/main/java/com/easy/live/streaming/registry/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.registry.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [Registry] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-registry/src/main/java/com/easy/live/streaming/registry/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.registry.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-registry/src/main/java/com/easy/live/streaming/registry/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.registry.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.CommandLineRunner; 7 | 8 | public class StartupRunner implements CommandLineRunner { 9 | 10 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 11 | 12 | @Override 13 | public void run(String... args) throws Exception { 14 | logger.info("注册中心服务启动成功"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /live-registry/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: registry-server 4 | 5 | server: 6 | port: 8001 7 | 8 | eureka: 9 | instance: 10 | hostname: live.registry.com 11 | server: 12 | responseCacheUpdateInvervalMs: 3000 13 | responseCacheAutoExpirationInSeconds: 180 14 | evictionIntervalTimerInMs: 3000 15 | enableSelfPreservation: false 16 | client: 17 | registerWithEureka: false 18 | fetchRegistry: false 19 | serviceUrl: 20 | defaultZone: http://live.registry.com:8001/eureka/ -------------------------------------------------------------------------------- /live-registry/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-registry/src/main/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | ${FILE_LOG_PATTERN} 12 | 13 | 14 | 15 | ${LOG_HOME}/live-registry-%d{yyyy-MM-dd}.%i.log 16 | 17 | 20MB 18 | 60 19 | 20GB 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /live-servants/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-servants 13 | 14 | 15 | 16 | org.springframework 17 | spring-webmvc 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-netflix-core 22 | 23 | 24 | com.easy 25 | live-common 26 | 27 | 28 | com.easy 29 | live-data 30 | 31 | 32 | io.github.openfeign 33 | feign-hystrix 34 | 35 | 36 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/auth/fallback/AuthServantFallback.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.auth.fallback; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.servants.api.auth.servant.AuthServant; 6 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 7 | import feign.hystrix.FallbackFactory; 8 | import lombok.extern.slf4j.Slf4j; 9 | 10 | /** 11 | * Description: 鉴权服务fallback 12 | * Author: zhangliangfu 13 | * Create on: 2019-08-14 14:16 14 | */ 15 | 16 | @Slf4j 17 | public class AuthServantFallback implements FallbackFactory { 18 | @Override 19 | public AuthServant create(Throwable throwable) { 20 | return new AuthServant() { 21 | @Override 22 | public BaseOutput login(UserInput input) { 23 | log.error("用户登录失败, input:{}", input); 24 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "用户登录失败"); 25 | } 26 | 27 | @Override 28 | public BaseOutput logout() { 29 | log.error("用户登出失败"); 30 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "用户登出失败"); 31 | } 32 | 33 | @Override 34 | public BaseOutput isLogin() { 35 | log.error("获取用户是否登陆失败"); 36 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "获取用户是否登陆失败"); 37 | } 38 | }; 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/auth/servant/AuthServant.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.auth.servant; 2 | 3 | 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.data.cache.UserCache; 6 | import com.easy.live.streaming.servants.api.auth.fallback.AuthServantFallback; 7 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 8 | import io.swagger.annotations.Api; 9 | import io.swagger.annotations.ApiOperation; 10 | import org.springframework.cloud.netflix.feign.FeignClient; 11 | import org.springframework.web.bind.annotation.RequestMapping; 12 | 13 | /** 14 | * Description: 鉴权服务 15 | * Author: zhangliangfu 16 | * Create on: 2019-08-14 14:13 17 | */ 18 | @FeignClient(name = "live-auth-servant", fallbackFactory = AuthServantFallback.class) 19 | @Api(value = "AuthServant", description = "鉴权服务接口") 20 | public interface AuthServant { 21 | 22 | @ApiOperation(value="用户登录接口", notes="用户登录接口") 23 | @RequestMapping("/open/live/auth/login") 24 | BaseOutput login(UserInput input); 25 | 26 | @ApiOperation(value="用户登出接口", notes="用户登出接口") 27 | @RequestMapping("/open/live/auth/logout") 28 | BaseOutput logout(); 29 | 30 | @RequestMapping("/open/live/auth/isLogin") 31 | BaseOutput isLogin(); 32 | } 33 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/user/fallback/UserServantFallback.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.user.fallback; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.servants.api.user.servant.UserServant; 6 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 7 | import com.easy.live.streaming.servants.protocol.output.user.UserOutput; 8 | import feign.hystrix.FallbackFactory; 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | /** 12 | * @Description:用户servant回滚 13 | * @Author: zhangliangfu 14 | * @Create on: 2019-07-10 19:03 15 | */ 16 | 17 | @Slf4j 18 | public class UserServantFallback implements FallbackFactory { 19 | @Override 20 | public UserServant create(Throwable throwable) { 21 | return new UserServant() { 22 | @Override 23 | public BaseOutput createUser(UserInput input) { 24 | log.error("创建用户失败, input:{}", input); 25 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "创建用户失败"); 26 | } 27 | }; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/user/servant/UserServant.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.user.servant; 2 | 3 | import com.easy.live.streaming.data.bean.BaseOutput; 4 | import com.easy.live.streaming.servants.api.user.fallback.UserServantFallback; 5 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 6 | import com.easy.live.streaming.servants.protocol.output.user.UserOutput; 7 | import io.swagger.annotations.Api; 8 | import io.swagger.annotations.ApiOperation; 9 | import org.springframework.cloud.netflix.feign.FeignClient; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | 12 | /** 13 | * @Description:用户Api 14 | * @Author: zhangliangfu 15 | * @Create on: 2019-07-10 19:02 16 | */ 17 | 18 | @FeignClient(name = "live-user-servant", fallbackFactory = UserServantFallback.class) 19 | @Api(value = "UserServant", description = "用户服务接口") 20 | public interface UserServant { 21 | 22 | /** 23 | * 创建用户 24 | * @param input 25 | * @return 26 | */ 27 | @ApiOperation(value="创建用户接口", notes="创建用户接口") 28 | @RequestMapping("/open/live/user/createUser") 29 | BaseOutput createUser(UserInput input); 30 | } 31 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/video/fallback/VideoRoomServantFallback.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.video.fallback; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.servants.api.video.servant.VideoRoomServant; 6 | import com.easy.live.streaming.servants.protocol.input.video.VideoRoomInput; 7 | import com.easy.live.streaming.servants.protocol.output.video.VideoRoomOutput; 8 | import feign.hystrix.FallbackFactory; 9 | import lombok.extern.slf4j.Slf4j; 10 | 11 | import java.util.List; 12 | 13 | /** 14 | * @Description:直播房间fallback 15 | * @Author: zhangliangfu 16 | * @Create on: 2019-06-24 19:40 17 | */ 18 | 19 | @Slf4j 20 | public class VideoRoomServantFallback implements FallbackFactory { 21 | @Override 22 | public VideoRoomServant create(Throwable throwable) { 23 | return new VideoRoomServant() { 24 | /** 25 | * 新建直播房间 26 | * 27 | * @param input 28 | * @return 29 | */ 30 | @Override 31 | public BaseOutput addVideoRoom(VideoRoomInput input) { 32 | log.error("新建直播房间失败, input:{}", input); 33 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "新建直播房间失败"); 34 | } 35 | 36 | /** 37 | * 获取直播房间列表 38 | * 39 | * @param input 40 | * @return 41 | */ 42 | @Override 43 | public BaseOutput> videoRoomList(VideoRoomInput input) { 44 | log.error("获取直播房间列表失败,input:{}",input); 45 | return new BaseOutput<>(Constants.RetMsg.FAIL.getCode(), "获取直播房间列表失败"); 46 | } 47 | }; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/api/video/servant/VideoRoomServant.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.api.video.servant; 2 | 3 | import com.easy.live.streaming.data.bean.BaseOutput; 4 | import com.easy.live.streaming.servants.api.video.fallback.VideoRoomServantFallback; 5 | import com.easy.live.streaming.servants.protocol.output.video.VideoRoomOutput; 6 | import com.easy.live.streaming.servants.protocol.input.video.VideoRoomInput; 7 | import io.swagger.annotations.Api; 8 | import io.swagger.annotations.ApiOperation; 9 | import org.springframework.cloud.netflix.feign.FeignClient; 10 | import org.springframework.web.bind.annotation.RequestMapping; 11 | 12 | import java.util.List; 13 | 14 | /** 15 | * @Description:直播房间Servant 16 | * @Author: zhangliangfu 17 | * @Create on: 2019-06-20 17:21 18 | */ 19 | 20 | @FeignClient(name = "live-video-servant", fallbackFactory = VideoRoomServantFallback.class) 21 | @Api(value = "VideoRoomServant", description = "直播间接口") 22 | public interface VideoRoomServant { 23 | /** 24 | * 新建直播房间 25 | * @param input 26 | * @return 27 | */ 28 | @ApiOperation(value="创建房间接口", notes="创建房间接口") 29 | @RequestMapping("/live/video/room/addVideoRoom") 30 | BaseOutput addVideoRoom(VideoRoomInput input); 31 | /** 32 | * 获取直播房间列表 33 | * @param input 34 | * @return 35 | */ 36 | @ApiOperation(value="房间列表接口", notes="房间列表接口") 37 | @RequestMapping("/open/live/video/room/videoRoomList") 38 | BaseOutput> videoRoomList(VideoRoomInput input); 39 | } 40 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/protocol/input/user/UserInput.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.protocol.input.user; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | 7 | /** 8 | * @Description:用户入参 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-07-10 19:07 11 | */ 12 | 13 | @Data 14 | @ApiModel(description = "用户参数") 15 | public class UserInput { 16 | @ApiModelProperty(name = "密码", dataType = "string") 17 | private String password; 18 | @ApiModelProperty(name = "用户头衔", dataType = "string", example = "直播区第一coder") 19 | private String title; 20 | @ApiModelProperty(name = "暂未启用") 21 | private String avatar; 22 | @ApiModelProperty(name = "用户ID", dataType = "int") 23 | private Integer id; 24 | @ApiModelProperty(name = "用户名称", dataType = "string", example = "沉默的coder") 25 | private String name; 26 | @ApiModelProperty(name = "暂未启用", dataType = "string") 27 | private String email; 28 | @ApiModelProperty(name = "暂未启用", dataType = "string") 29 | private String phone; 30 | @ApiModelProperty(name = "暂未启用", dataType = "string") 31 | private boolean useFlag; 32 | @ApiModelProperty(name = "前端无用", dataType = "string") 33 | private String sessionId; 34 | } 35 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/protocol/input/video/VideoRoomInput.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.protocol.input.video; 2 | 3 | import io.swagger.annotations.ApiModel; 4 | import io.swagger.annotations.ApiModelProperty; 5 | import lombok.Data; 6 | 7 | /** 8 | * @Description:直播房间入参 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-24 19:45 11 | */ 12 | 13 | @Data 14 | @ApiModel 15 | public class VideoRoomInput { 16 | 17 | //房间名称 18 | @ApiModelProperty(name = "房间名称", value = "Coder的直播间", dataType = "string", example = "Coder的直播间") 19 | private String roomName; 20 | 21 | //标题 22 | @ApiModelProperty(name = "标题", value = "今天直播撸代码1000行", dataType = "string", example = "今天直播撸代码1000行") 23 | private String title; 24 | 25 | //封面 26 | @ApiModelProperty(name = "直播间封面", value = "http://xxxx", dataType = "string", example = "http://xxxx") 27 | private String cover; 28 | 29 | //类型 30 | @ApiModelProperty(name = "直播间类型", value = "1", dataType = "int", example = "1") 31 | private Integer cateId; 32 | } 33 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/protocol/output/user/UserOutput.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.protocol.output.user; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @Description:用户出参 7 | * @Author: zhangliangfu 8 | * @Create on: 2019-07-10 19:05 9 | */ 10 | 11 | @Data 12 | public class UserOutput { 13 | private String password; 14 | private String title; 15 | private String avatar; 16 | private Integer id; 17 | private String name; 18 | private String email; 19 | private String phone; 20 | private boolean useFlag; 21 | } 22 | -------------------------------------------------------------------------------- /live-servants/src/main/java/com/easy/live/streaming/servants/protocol/output/video/VideoRoomOutput.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.servants.protocol.output.video; 2 | 3 | import lombok.Data; 4 | 5 | /** 6 | * @Description:直播房间出参 7 | * @Author: zhangliangfu 8 | * @Create on: 2019-06-24 19:45 9 | */ 10 | 11 | @Data 12 | public class VideoRoomOutput { 13 | 14 | //房间名称 15 | private String roomName; 16 | 17 | //标题 18 | private String title; 19 | 20 | //封面 21 | private String cover; 22 | 23 | //类型 24 | private Integer cateId; 25 | 26 | //直播URL 27 | private String liveUrl; 28 | } 29 | -------------------------------------------------------------------------------- /live-user/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-user 13 | 14 | 15 | 16 | org.springframework.cloud 17 | spring-cloud-starter-eureka 18 | 19 | 20 | org.springframework.cloud 21 | spring-cloud-starter-feign 22 | 23 | 24 | org.springframework.cloud 25 | spring-cloud-starter-config 26 | 27 | 28 | mysql 29 | mysql-connector-java 30 | 31 | 32 | com.easy 33 | live-servants 34 | 35 | 36 | com.easy 37 | live-data 38 | 39 | 40 | 41 | 42 | 43 | 44 | org.springframework.boot 45 | spring-boot-maven-plugin 46 | 1.5.2.RELEASE 47 | 48 | 49 | 50 | repackage 51 | 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/UserApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.autoconfigure.domain.EntityScan; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 9 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 10 | 11 | /** 12 | * @Description:用户服务 13 | * @Author: zhangliangfu 14 | * @Create on: 2019-06-13 17:56 15 | */ 16 | 17 | @ComponentScan(basePackages={"com.easy"}) 18 | @EnableJpaRepositories(basePackages = "com.easy") 19 | @EntityScan(basePackages = "com.easy") 20 | @SpringBootApplication 21 | @EnableDiscoveryClient 22 | @EnableSwagger2 23 | public class UserApplication { 24 | public static void main(String[] args) { 25 | new SpringApplicationBuilder(UserApplication.class).web(true).run(args); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/controller/UserController.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.controller; 2 | 3 | import com.easy.live.streaming.data.bean.BaseOutput; 4 | import com.easy.live.streaming.servants.api.user.servant.UserServant; 5 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 6 | import com.easy.live.streaming.servants.protocol.output.user.UserOutput; 7 | import com.easy.live.streaming.user.service.UserService; 8 | import lombok.extern.slf4j.Slf4j; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.web.bind.annotation.RestController; 11 | 12 | /** 13 | * @Description: 用户controller 14 | * @Author: zhangliangfu 15 | * @Create on: 2019-07-10 19:17 16 | */ 17 | 18 | @Slf4j 19 | @RestController 20 | public class UserController implements UserServant { 21 | @Autowired 22 | private UserService userService; 23 | 24 | /** 25 | * 创建用户 26 | * 27 | * @param input 28 | * @return 29 | */ 30 | @Override 31 | public BaseOutput createUser(UserInput input) { 32 | log.debug("创建用户,input:{}", input); 33 | return userService.createUser(input); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/interceptor/AccessLogInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.interceptor; 2 | 3 | import lombok.extern.slf4j.Slf4j; 4 | import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 5 | 6 | import javax.servlet.http.HttpServletRequest; 7 | import javax.servlet.http.HttpServletResponse; 8 | 9 | /** 10 | * Description: 访问记录拦截器 11 | * Author: zhangliangfu 12 | * Create on: 2019-07-31 13:49 13 | */ 14 | @Slf4j 15 | public class AccessLogInterceptor extends HandlerInterceptorAdapter { 16 | 17 | private ThreadLocal invokeTime = new ThreadLocal<>(); 18 | 19 | @Override 20 | public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler){ 21 | String requestURI = request.getRequestURI(); 22 | String method = request.getMethod(); 23 | invokeTime.set(System.currentTimeMillis()); 24 | log.info("=========> 接口访问开始, URI:{},Method:{}", requestURI, method); 25 | return true; 26 | } 27 | 28 | @Override 29 | public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { 30 | Long startTime = invokeTime.get(); 31 | long endTime = System.currentTimeMillis(); 32 | long miliSec = endTime - startTime; 33 | String requestURI = request.getRequestURI(); 34 | String method = request.getMethod(); 35 | log.info("=========> 接口访问结束, URI:{},Method:{},耗时:{}毫秒", requestURI, method,miliSec); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/interceptor/WebInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.interceptor; 2 | 3 | import com.easy.live.streaming.data.filter.AcquireUserInfoInterceptor; 4 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 5 | import org.springframework.context.annotation.Bean; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 9 | 10 | /** 11 | * Description: WEB拦截器 12 | * Author: zhangliangfu 13 | * Create on: 2019-07-26 14:57 14 | */ 15 | 16 | @Configuration 17 | @AutoConfigureAfter(WebMvcConfigurerAdapter.class) 18 | public class WebInterceptor extends WebMvcConfigurerAdapter { 19 | 20 | @Bean 21 | public AccessLogInterceptor accessLogInterceptor(){ 22 | return new AccessLogInterceptor(); 23 | } 24 | 25 | @Override 26 | public void addInterceptors(InterceptorRegistry registry) { 27 | registry.addInterceptor(new AcquireUserInfoInterceptor()); 28 | registry.addInterceptor(accessLogInterceptor()).addPathPatterns("/**") 29 | .excludePathPatterns("/swagger-resources/**"); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [User] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.CommandLineRunner; 7 | 8 | public class StartupRunner implements CommandLineRunner { 9 | 10 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 11 | 12 | @Value("${spring.profiles.active:dev}") 13 | private String env; 14 | 15 | @Value("${spring.cloud.config.profile:dev}") 16 | private String configEnv; 17 | 18 | @Override 19 | public void run(String... args) { 20 | logger.info("spring.profiles.active:{}",env); 21 | logger.info("spring.cloud.config.profile:{}",configEnv); 22 | logger.info("用户服务启动成功"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.service; 2 | 3 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.servants.protocol.output.user.UserOutput; 6 | 7 | /** 8 | * @Description:用户Service 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-13 17:53 11 | */ 12 | public interface UserService { 13 | /** 14 | * 创建用户 15 | * 16 | * @param input 17 | * @return 18 | */ 19 | BaseOutput createUser(UserInput input); 20 | } 21 | -------------------------------------------------------------------------------- /live-user/src/main/java/com/easy/live/streaming/user/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.user.service.impl; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.bean.BaseOutput; 5 | import com.easy.live.streaming.data.cache.SimpleCacheService; 6 | import com.easy.live.streaming.data.entity.user.LiveUser; 7 | import com.easy.live.streaming.data.service.user.LiveUserService; 8 | import com.easy.live.streaming.servants.protocol.input.user.UserInput; 9 | import com.easy.live.streaming.servants.protocol.output.user.UserOutput; 10 | import com.easy.live.streaming.user.service.UserService; 11 | import org.apache.shiro.SecurityUtils; 12 | import org.apache.shiro.authc.AuthenticationException; 13 | import org.apache.shiro.authc.UsernamePasswordToken; 14 | import org.apache.shiro.subject.Subject; 15 | import org.springframework.beans.BeanUtils; 16 | import org.springframework.beans.factory.annotation.Autowired; 17 | import org.springframework.stereotype.Service; 18 | 19 | /** 20 | * Description: 用户Service实现 21 | * Author: zhangliangfu 22 | * Create on: 2019-06-13 17:54 23 | */ 24 | 25 | @Service 26 | public class UserServiceImpl implements UserService { 27 | 28 | @Autowired 29 | private LiveUserService liveUserService; 30 | 31 | /** 32 | * 创建用户 33 | * 34 | * @param input 35 | * @return 36 | */ 37 | @Override 38 | public BaseOutput createUser(UserInput input) { 39 | LiveUser user = new LiveUser(); 40 | BeanUtils.copyProperties(input, user); 41 | LiveUser save = liveUserService.save(user); 42 | UserOutput userOutput = new UserOutput(); 43 | BeanUtils.copyProperties(save, userOutput); 44 | return new BaseOutput<>(Constants.RetMsg.SUCCESS.code, "注册用户成功", userOutput); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /live-user/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: user-server 4 | 5 | server: 6 | port: 8012 -------------------------------------------------------------------------------- /live-user/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-user/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | easy: 2 | eurekaServer: http://live.registry.com:8001/eureka/ 3 | 4 | eureka: 5 | instance: 6 | lease-expiration-duration-in-seconds: 15 7 | lease-renewal-interval-in-seconds: 5 8 | preferIpAddress: true 9 | hostname: live.registry.com 10 | metadataMap: 11 | cluster: MAIN 12 | client: 13 | registryFetchIntervalSeconds: 5 14 | serviceUrl: 15 | defaultZone: ${easy.eurekaServer} 16 | 17 | spring: 18 | cloud: 19 | config: 20 | profile: dev 21 | discovery: 22 | enabled: true 23 | serviceId: config-server 24 | 25 | 26 | -------------------------------------------------------------------------------- /live-user/src/main/resources/logback-spring.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | ${FILE_LOG_PATTERN} 13 | UTF-8 14 | 15 | 16 | 17 | ${LOG_HOME}/gateway-server-%d{yyyy-MM-dd}.%i.log 18 | 19 | 20MB 20 | 60 21 | 20GB 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /live-video/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | live-parent 7 | com.easy 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | live-video 13 | 14 | 15 | 16 | 17 | org.springframework.cloud 18 | spring-cloud-starter-eureka 19 | 20 | 21 | org.springframework.cloud 22 | spring-cloud-starter-feign 23 | 24 | 25 | org.springframework.cloud 26 | spring-cloud-starter-config 27 | 28 | 29 | mysql 30 | mysql-connector-java 31 | 32 | 33 | com.easy 34 | live-data 35 | 36 | 37 | com.easy 38 | live-servants 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | org.springframework.boot 47 | spring-boot-maven-plugin 48 | 1.5.2.RELEASE 49 | 50 | 51 | 52 | repackage 53 | 54 | 55 | 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/VideoApplication.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video; 2 | 3 | import org.springframework.boot.autoconfigure.SpringBootApplication; 4 | import org.springframework.boot.autoconfigure.domain.EntityScan; 5 | import org.springframework.boot.builder.SpringApplicationBuilder; 6 | import org.springframework.cloud.client.discovery.EnableDiscoveryClient; 7 | import org.springframework.context.annotation.ComponentScan; 8 | import org.springframework.data.jpa.repository.config.EnableJpaRepositories; 9 | import springfox.documentation.swagger2.annotations.EnableSwagger2; 10 | 11 | /** 12 | * @Description:视频直播 13 | * @Author: zhangliangfu 14 | * @Create on: 2019-06-13 17:56 15 | */ 16 | 17 | @ComponentScan(basePackages={"com.easy"}) 18 | @EnableJpaRepositories(basePackages = "com.easy") 19 | @EntityScan(basePackages = "com.easy") 20 | @SpringBootApplication 21 | @EnableDiscoveryClient 22 | @EnableSwagger2 23 | public class VideoApplication { 24 | public static void main(String[] args) { 25 | new SpringApplicationBuilder(VideoApplication.class).web(true).run(args); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/controller/VideoController.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.controller; 2 | 3 | import com.easy.live.streaming.common.config.Constants; 4 | import com.easy.live.streaming.data.cache.ThreadLocalUserCache; 5 | import com.easy.live.streaming.data.cache.UserCache; 6 | import com.easy.live.streaming.data.entity.video.VideoLiveRoom; 7 | import com.easy.live.streaming.data.service.video.VideoLiveRoomService; 8 | import com.easy.live.streaming.servants.api.video.servant.VideoRoomServant; 9 | import com.easy.live.streaming.servants.protocol.input.video.VideoRoomInput; 10 | import com.easy.live.streaming.data.bean.BaseOutput; 11 | import com.easy.live.streaming.servants.protocol.output.video.VideoRoomOutput; 12 | import org.springframework.beans.factory.annotation.Autowired; 13 | import org.springframework.web.bind.annotation.RestController; 14 | 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | /** 19 | * @Description:视频直播Controller 20 | * @Author: zhangliangfu 21 | * @Create on: 2019-06-13 18:01 22 | */ 23 | 24 | @RestController 25 | public class VideoController implements VideoRoomServant { 26 | 27 | @Autowired 28 | private VideoLiveRoomService videoLiveRoomService; 29 | /** 30 | * 新建直播房间 31 | * 32 | * @param input 33 | * @return 34 | */ 35 | @Override 36 | public BaseOutput addVideoRoom(VideoRoomInput input) { 37 | UserCache userCache = ThreadLocalUserCache.getUserCache(); 38 | VideoLiveRoom videoLiveRoom = new VideoLiveRoom(); 39 | videoLiveRoom.setTitle(input.getTitle()); 40 | videoLiveRoom.setCover(input.getCover()); 41 | videoLiveRoom.setRoomName(input.getRoomName()); 42 | videoLiveRoom.setCateId(input.getCateId()); 43 | videoLiveRoom.setUserId(userCache.getUserId()); 44 | videoLiveRoomService.save(videoLiveRoom); 45 | return new BaseOutput<>(Constants.RetMsg.SUCCESS.code, Constants.RetMsg.SUCCESS.msg); 46 | } 47 | 48 | /** 49 | * 获取直播房间列表 50 | * 51 | * @param input 52 | * @return 53 | */ 54 | @Override 55 | public BaseOutput> videoRoomList(VideoRoomInput input) { 56 | List list = new ArrayList<>(); 57 | List all = videoLiveRoomService.findAll(); 58 | all.forEach(videoLiveRoom -> { 59 | VideoRoomOutput videoRoomOutput = new VideoRoomOutput(); 60 | videoRoomOutput.setTitle(videoLiveRoom.getTitle()); 61 | videoRoomOutput.setCateId(videoLiveRoom.getCateId()); 62 | videoRoomOutput.setCover(videoLiveRoom.getCover()); 63 | videoRoomOutput.setRoomName(videoLiveRoom.getRoomName()); 64 | videoRoomOutput.setLiveUrl(Constants.LIVE_URL_PREFIX+videoLiveRoom.getId()); 65 | list.add(videoRoomOutput); 66 | }); 67 | return new BaseOutput<>(Constants.RetMsg.SUCCESS.code, Constants.RetMsg.SUCCESS.msg, list); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/inteceptor/WebInterceptor.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.inteceptor; 2 | 3 | import com.easy.live.streaming.data.filter.AcquireUserInfoInterceptor; 4 | import org.springframework.boot.autoconfigure.AutoConfigureAfter; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.InterceptorRegistry; 7 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 8 | 9 | /** 10 | * Description: WEB拦截器 11 | * Author: zhangliangfu 12 | * Create on: 2019-07-26 14:57 13 | */ 14 | 15 | @Configuration 16 | @AutoConfigureAfter(WebMvcConfigurerAdapter.class) 17 | public class WebInterceptor extends WebMvcConfigurerAdapter { 18 | 19 | @Override 20 | public void addInterceptors(InterceptorRegistry registry) { 21 | registry.addInterceptor(new AcquireUserInfoInterceptor()).addPathPatterns("/**") 22 | .excludePathPatterns("/swagger-resources/**"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/intergration/ApplicationDestroy.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.DisposableBean; 6 | 7 | public class ApplicationDestroy implements DisposableBean { 8 | 9 | Logger logger = LoggerFactory.getLogger(ApplicationDestroy.class); 10 | 11 | @Override 12 | public void destroy() throws Exception { 13 | logger.info("System [Video] destroy"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/intergration/AutoConfigure.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.intergration; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 6 | 7 | @Configuration 8 | public class AutoConfigure extends WebMvcConfigurerAdapter { 9 | 10 | @Bean 11 | StartupRunner startupRunner() { 12 | return new StartupRunner(); 13 | } 14 | 15 | @Bean 16 | ApplicationDestroy applicationDestroy() { 17 | return new ApplicationDestroy(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/intergration/StartupRunner.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.intergration; 2 | 3 | import org.slf4j.Logger; 4 | import org.slf4j.LoggerFactory; 5 | import org.springframework.beans.factory.annotation.Value; 6 | import org.springframework.boot.CommandLineRunner; 7 | 8 | public class StartupRunner implements CommandLineRunner { 9 | 10 | Logger logger = LoggerFactory.getLogger(StartupRunner.class); 11 | 12 | @Value("${spring.profiles.active:dev}") 13 | private String env; 14 | 15 | @Value("${spring.cloud.config.profile:dev}") 16 | private String configEnv; 17 | 18 | @Override 19 | public void run(String... args) { 20 | logger.info("spring.profiles.active:{}",env); 21 | logger.info("spring.cloud.config.profile:{}",configEnv); 22 | logger.info("视频直播服务启动成功"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/service/VideoService.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.service; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoom; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @Description:直播Service 9 | * @Author: zhangliangfu 10 | * @Create on: 2019-06-13 17:53 11 | */ 12 | public interface VideoService { 13 | VideoLiveRoom saveVideoRoom(VideoLiveRoom videoLiveRoom); 14 | VideoLiveRoom delVideoRoom(Integer roomId); 15 | List listVideoRooms(); 16 | } 17 | -------------------------------------------------------------------------------- /live-video/src/main/java/com/easy/live/streaming/video/service/impl/VideoServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.easy.live.streaming.video.service.impl; 2 | 3 | import com.easy.live.streaming.data.entity.video.VideoLiveRoom; 4 | import com.easy.live.streaming.data.service.video.VideoLiveRoomService; 5 | import com.easy.live.streaming.video.service.VideoService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * @Description:直播Service实现 13 | * @Author: zhangliangfu 14 | * @Create on: 2019-06-13 17:54 15 | */ 16 | 17 | @Service 18 | public class VideoServiceImpl implements VideoService { 19 | @Autowired 20 | private VideoLiveRoomService videoLiveRoomService; 21 | 22 | @Override 23 | public VideoLiveRoom saveVideoRoom(VideoLiveRoom videoLiveRoom) { 24 | VideoLiveRoom result; 25 | if (videoLiveRoom.isNew()){ 26 | result = videoLiveRoomService.save(videoLiveRoom); 27 | }else { 28 | VideoLiveRoom one = videoLiveRoomService.findOne(videoLiveRoom.getId()); 29 | if (one!=null){ 30 | one.setCateId(videoLiveRoom.getCateId()); 31 | one.setRoomName(videoLiveRoom.getRoomName()); 32 | one.setCover(videoLiveRoom.getCover()); 33 | one.setTitle(videoLiveRoom.getTitle()); 34 | videoLiveRoomService.save(one); 35 | } 36 | result = one; 37 | } 38 | return result; 39 | } 40 | 41 | @Override 42 | public VideoLiveRoom delVideoRoom(Integer roomId) { 43 | return null; 44 | } 45 | 46 | @Override 47 | public List listVideoRooms() { 48 | return null; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /live-video/src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | application: 3 | name: video-server 4 | 5 | server: 6 | port: 8013 7 | 8 | -------------------------------------------------------------------------------- /live-video/src/main/resources/banner.txt: -------------------------------------------------------------------------------- 1 | ____ ___ ___ 2 | /\ _`\ /\_ \ __ /'___\ 3 | \ \ \L\_\ __ ____ __ __ \//\ \ /\_\/\ \__/ __ 4 | \ \ _\L /'__`\ /',__\/\ \/\ \ \ \ \ \/\ \ \ ,__\/'__`\ 5 | \ \ \L\ \/\ \L\.\_/\__, `\ \ \_\ \ \_\ \_\ \ \ \ \_/\ __/ 6 | \ \____/\ \__/.\_\/\____/\/`____ \ /\____\\ \_\ \_\\ \____\ 7 | \/___/ \/__/\/_/\/___/ `/___/> \ \/____/ \/_/\/_/ \/____/ 8 | /\___/ 9 | \/__/ -------------------------------------------------------------------------------- /live-video/src/main/resources/bootstrap.yml: -------------------------------------------------------------------------------- 1 | easy: 2 | eurekaServer: http://live.registry.com:8001/eureka/ 3 | 4 | eureka: 5 | instance: 6 | lease-expiration-duration-in-seconds: 15 7 | lease-renewal-interval-in-seconds: 5 8 | preferIpAddress: true 9 | hostname: live.registry.com 10 | metadataMap: 11 | cluster: MAIN 12 | client: 13 | registryFetchIntervalSeconds: 5 14 | serviceUrl: 15 | defaultZone: ${easy.eurekaServer} 16 | 17 | spring: 18 | cloud: 19 | config: 20 | profile: dev 21 | discovery: 22 | enabled: true 23 | serviceId: config-server 24 | 25 | 26 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | 8 | UTF-8 9 | 10 | 11 | com.easy 12 | live-parent 13 | 1.0-SNAPSHOT 14 | 15 | live-registry 16 | live-config 17 | live-gateway 18 | live-servants 19 | live-user 20 | live-video 21 | live-data 22 | live-common 23 | live-auth 24 | 25 | pom 26 | 27 | 28 | 29 | 30 | com.easy 31 | live-dependencies 32 | 1.0-SNAPSHOT 33 | pom 34 | import 35 | 36 | 37 | 38 | com.easy 39 | live-common 40 | 1.0-SNAPSHOT 41 | 42 | 43 | com.easy 44 | live-data 45 | 1.0-SNAPSHOT 46 | 47 | 48 | com.easy 49 | live-servants 50 | 1.0-SNAPSHOT 51 | 52 | 53 | 54 | 55 | 56 | 57 | org.apache.maven.plugins 58 | maven-compiler-plugin 59 | 3.6.1 60 | 61 | 1.8 62 | 1.8 63 | 64 | 65 | 66 | 67 | --------------------------------------------------------------------------------