Hexo Blog

fsh的博客


  • 首页

  • 归档

IntelliJ IDEA 配置插件

发表于 2019-09-18 更新于 2019-09-19

插件

插件名称 作用 快捷键
GenerateAllSetter 生成带默认值的所有set方法的对象 alt+enter
p3c 代码风格检查插件 -
Lombok 生成get和set方法以及构造参数 @Data

常用快捷键

去掉多余引用 Ctrl + Alt + O
尝试修复问题 Alt+Enter

配置指导

配置指导

用Nacos作为Dubbo的注册中心

发表于 2019-09-05

什么是 Nacos

Nacos 是一个微服务管理平台,支持几乎所有主流类型的“服务”的发现、配置和管理。

什么是Dubbo

Dubbo是一款高性能、轻量级的开源Java RPC框架,它提供了三大核心能力:面向接口的远程方法调用,智能容错和负载均衡,以及服务自动注册和发现。

Maven依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
<!-- Apache Dubbo Begin -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-client</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.spring</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-common</artifactId>
</exclusion>
</exclusions>
</dependency>

配置注册中心

1
2
3
4
5
6
7
8
9
dubbo:
registry:
address: nacos://127.0.0.1:8848
scan:
base-packages: com.example.admin.service
protocol:
name: dubbo
port: -1
serialization: kryo

示例接口与实现

1
2
3
4
5
public interface DemoService {

String sayName(String name);

}

实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Service(version = "${demo.service.version}")
public class DefaultService implements DemoService {

@Value("${demo.service.name}")
private String serviceName;

public String sayName(String name) {
RpcContext rpcContext = RpcContext.getContext();
return String.format("Service [name :%s , port : %d] %s(\"%s\") : Hello,%s",
serviceName,
rpcContext.getLocalPort(),
rpcContext.getMethodName(),
name,
name);
}
}

消费

1
2
3
4
5
6
7
8
@Reference(version = "1.0.0")
private DemoService demoService;

@GetMapping("/test")
private ResponseResult<UserDTO> validateReg() {
UserDO user = userService.sayName("admin");
return new ResponseResult(200, "成功", user);
}

接口和实现可以拆分成不同的模块,实现解耦

Spring Security oAuth2

发表于 2019-08-15 更新于 2019-09-18

Spring Security

Spring Security 是一个spring的安全框架

oAuth2

是一种授权协议,他的主要作用是为了提供认证和授权的标准

Access Token

客户端访问资源服务器的令牌,代表用户的授权

Refresh Token

刷新令牌,是用来实现安全令牌的时效机制,使通过安全令牌访问的资源更加安全

配置认证服务器

在云服务中一般有认证服务器和资源服务器,通常认证服务器用来获取用户的登录和授权信息,而资源服务器则用来校验具体的权限信息

认证服务器注解

@Configuration
@EnableAuthorizationServer

认证服务器继承类

认证服务器继承AuthorizationServerConfigurerAdapter
认证服务器通过客户端的id,密钥,客户端授权类型取得客户端授权范围

资源服务代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

/**
* @author admin
* @version v1.0.0
**/
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {

@Autowired
private BCryptPasswordEncoder passwordEncoder;

@Autowired
private AuthenticationManager authenticationManager;
// 增加 TokenStore 配置
@Bean
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
.authenticationManager(authenticationManager)
// 增加 TokenStore 配置
.tokenStore(tokenStore());
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.checkTokenAccess("isAuthenticated()")
.allowFormAuthenticationForClients();
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("client")
.secret(passwordEncoder.encode("secret"))
.authorizedGrantTypes("password", "refresh_token")
.scopes("backend")
.resourceIds("backend-resources")
.accessTokenValiditySeconds(60 * 60 * 24)
.refreshTokenValiditySeconds(60 * 60 * 24 * 30);
}
}

服务器安全配置

创建一个类继承 WebSecurityConfigurerAdapter 并添加相关注解(@Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
)

服务器安全配置代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @author admin
* @version v1.0.0
**/
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

@Bean
public BCryptPasswordEncoder passwordEncoder() {
// 设置默认的加密方式
return new BCryptPasswordEncoder();
}

@Bean
@Override
protected UserDetailsService userDetailsService() {
return new UserDetailsServiceImpl();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 使用自定义认证与授权
auth.userDetailsService(userDetailsService());
}

@Override
public void configure(WebSecurity web){
web.ignoring().antMatchers("/user/login").antMatchers("/user/logout");
/* .antMatchers("/oauth/authorize")
.antMatchers("/oauth/check_token");*/

}

/**
* 用于支持 password 模式
*
* @return
* @throws Exception
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.exceptionHandling()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
// 增加了授权访问配置,正常用户都可以注销
.antMatchers("/user/logout").hasAuthority("USER");
}
}

配置资源服务器

资源服务器则用来校验具体的权限信息

资源服务器注解

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)

资源服务器继承类

资源服务器继承ResourceServerConfigurerAdapter
资源服务器通声明自身资源ID和需要的角色权限

资源服务器代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true)
public class ResourceServerConfiguration extends ResourceServerConfigurerAdapter {
@Override
public void configure(HttpSecurity http) throws Exception {
http
.exceptionHandling()
.and()
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers("/**").hasAuthority("USER");
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
// 配置资源 ID
resources.resourceId("backend-resources");
}
}

yml文件配置

1
2
3
4
5
6
7
8
9
10
security:
oauth2:
client:
client-id: client
client-secret: secret
access-token-uri: http://127.0.0.1:9001/oauth/token
user-authorization-uri: http://127.0.0.1:9001/oauth/authorize
id: client
resource:
token-info-uri: http://127.0.0.1:9001/oauth/check_token

Druid

发表于 2019-08-08

Druid

Druid是一个JDBC组件库,包括数据库连接池、SQL Parser等组件。DruidDataSource是最好的数据库连接池。

Maven

1
2
3
4
5
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>

数据库连接池
进行数据库操作时数据库连接会不断的创建和销毁,会消耗大量资源还有销毁失败导致内存泄漏的风险,而数据库链接池则是能管理这些链接,需要时可以调用,不用时可以放回去从而避免了一次建立数据库连接和断开的操作时间消耗。

参考配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server:
port: 8101

spring:
application:
name: provider-demo

cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848

datasource:
url: jdbc:mysql://127.0.0.1:3309/data_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: admin@2018
driver-class-name: com.mysql.cj.jdbc.Driver

Maven依赖管理

发表于 2019-08-07

声明

1
2
3
4
5
6
7
8
<!--项目的全球唯一标识符,通常使用全限定的包名区分该项目和其他项目。并且构建时生成的路径也是由此生成, 如com.mycompany.app生成的相对路径为:/com/mycompany/app-->
<groupId>mycompany.demo</groupId>
<!-- 构件的标识符,它和group ID一起唯一标识一个构件。换句话说,你不能有两个不同的项目拥有同样的artifact ID和groupID;在某个 特定的group ID下,artifact ID也必须是唯一的。构件是项目产生的或使用的一个东西,Maven为项目产生的构件包括:JARs,源 码,二进制发布和WARs等。-->
<artifactId>demo-data</artifactId>
<!--项目产生的构件类型,例如jar、war、ear、pom。插件可以创建他们自己的构件类型,所以前面列的不是全部构件类型-->
<packaging>jar</packaging>
<!--项目当前版本,格式为:主版本.次版本.增量版本-限定版本号-->
<version>1.0-SNAPSHOT</version>

模块

1
2
3
4
5
6
<!--模块(有时称作子项目) 被构建成项目的一部分。列出的每个模块元素是指向该模块的目录的相对路径-->
<modules>
<module>cloud-provider</module>
<module>cloud-provider-api</module>
<module>cloud-gateway</module>
</modules>

父项目坐标”< parent>”

1
2
3
4
5
6
7
8
9
10
<parent>
<!--被继承的父项目的构件标识符-->
<artifactId/>
<!--被继承的父项目的全球唯一标识符-->
<groupId/>
<!--被继承的父项目的版本-->
<version/>
<!-- 父项目的pom.xml文件的相对路径。相对路径允许你选择一个不同的路径。默认值是../pom.xml。Maven首先在构建当前项目的地方寻找父项 目的pom,其次在文件系统的这个位置(relativePath位置),然后在本地仓库,最后在远程仓库寻找父项目的pom。-->
<relativePath/>
</parent>

依赖版本管理

1
2
3
4
5
6
7
<!-- 管理子项目的版本号除非子项目版本有特别声明-->
<dependencyManagement>
<dependencies>
<!--参见dependencies/dependency元素-->
<dependency>......</dependency>
</dependencies>
</dependencyManagement>

依赖管理

1
2
3
4
<!--该元素描述了项目相关的所有依赖,若在父项目中则会直接被子项目继承-->
<dependencies>
<dependency>......</dependency>
</dependencies>

Xshell反向隧道进行内网穿透

发表于 2019-08-07 更新于 2019-08-08

场景

当前有两台服务器a,b,其中a有公网ip,而b只有内网,现在需要在本机上用Xhell链接b进行管理。

搭建隧道

用Xhell链接至a服务器,在链接属性中开启隧道,选择添加创建类型为Dynamic(SOCKS4/5)监听端口1080或其他的隧道,并保持链接作为代理服务器。

开启代理

在用Xhell链接至b服务器时额外开启代理,编辑代理服务器,编辑代理属性类型为SOCKS5主机为本地,端口为1080或其他。此时在链接至b就会发现b已经可以链接了

名词解释表

发表于 2019-08-02

REST

REST即表述性状态传递(英文:Representational State Transfer,简称REST)一种软件架构风格。
表述性状态转移是一组架构约束条件和原则。满足这些约束条件和原则的应用程序或设计就是RESTful。需要注意的是,REST是设计风格而不是标准。REST通常基于使用HTTP,URI,和XML(标准通用标记语言下的一个子集)以及HTML(标准通用标记语言下的一个应用)这些现有的广泛流行的协议和标准。

QPS

QPS,Query Per Second,也就就是每秒有多少请求

Single Sign On,单点登录

Single Sign On,单点登录

阿里云

ECS

ECS 云服务器(Elastic Compute Service, ECS)
处理能力可弹性伸缩的计算服务器

RDS

云数据库(Relational Database Service,简称RDS)
稳定可靠、可弹性伸缩的在线数据库服务

通讯协议

HTTP

HTTP是一个简单的请求-响应协议,它通常运行在TCP之上。
请求报文格格式:请求行 - 通用信息头 - 请求头 - 实体头 - 报文主体
应答报文格式如下:状态行 - 通用信息头 - 响应头 - 实体头 - 报文主体

TCP

传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议

RPC

RPC(Remote Procedure Call)—远程过程调用,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。

Actor模型

为了利用多核心的计算机,我们需要并发执行,但是多线程的方式会引入很多问题和增加调试难度。

actor按次序处理消息,比如你发送三个消息给一个actor,它们不会被并发处理。如果你想让这三个消息得到并发处理,你需要创建3个actor,然后分别发送给它们。

接受到的异步消息存在于actor内部的一个队列中,我们可以把它形象化的叫做邮箱(mailbox)。

索引

发表于 2019-08-01
  • 网络
    • 网络协议
    • 魔法上网
  • Java
    • Lambda
    • Docker
  • Spring-Boot
  • Spring-cloud
  • Spring-cloud-alibaba

Redis

发表于 2019-05-24 更新于 2019-07-31

Redis

redis 是一个持久化的键值对存储器,其中主键是用来表示数据块所在位置,而值在java则是通过序列化存储一切对象。redis和数据库类似可通过select命令切换数据库,并且通过快照来持久数据库。通常作用在数据缓存,消息队列,分布式集群Session分离处理等。

Redis数据结构

redis有5种数据类型结构分别是字符型,散列型,列表,集合,有序集合。

字符型

通常意义上的键值对,也是redis主要缓存数据的格式,可以通过命令对值进行长度获取,截断等操作

散列

比字符串结构多了个中间层:字段,字段类比hash的Key可以有多个字段

列表

可以存储数组形式的值

集合

用于存储唯一值,可以用作交集运算

有序集合

和集合一样,但是多了权重

Redis支持

管道

redis是支持管道的,所以你可以发送一堆请求却不用等待它们的响应

过期

Redis 允许你指定 key 的存活时间,你可以使用expore key seconds 设置过时时间(秒),也可以expireat key timestamp 设置具体那一刻过时(从1970年01月01日开始的秒数)

消息中间间

redis通过发布订阅(Publication and Subscriptions)实现消息直接的传递

Docker-Compose

发表于 2019-04-30 更新于 2019-08-01

Compose 简介

Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排。
它允许用户通过一个单独的 docker-compose.yml 模板文件(YAML 格式)来定义一组相关联的应用容器为一个项目(project)。

Compose 中有两个重要的概念:

  1. 服务 (service):一个应用的容器,实际上可以包括若干运行相同镜像的容器实例。

  2. 项目 (project):由一组关联的应用容器组成的一个完整业务单元,在 docker-compose.yml 文件中定义。

docker-compose安装

下载

1
2
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)"
-o /usr/local/bin/docker-compose

授权

1
sudo chmod +x /usr/local/bin/docker-compose

检查

1
docker-compose --version

该docker-compose up命令聚合每个容器的输出(基本上正在运行docker-compose logs -f)。

docker-compose -f cmac-server.yml up

容器配置修改

先从容器中拷贝一份到指定目录,修改指定目录里面的配置,下次启动时将源目录挂载至此

1
docker cp -r c2dfee831758:/etc/mysql /cmac/mysql/conf
12…6

fsh

54 日志
4 分类
20 标签
© 2019 fsh
由 Hexo 强力驱动 v3.9.0
|
主题 – NexT.Pisces v7.3.0