Elasticsearch5.6 安装

1 . 安装jdk 1.8

下载地址 : https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

tar -zxvf jdk-8u191-linux-x64.tar.gz

设置环境变量  /usr/java/jdk1.8.0_151 目录为刚解压的路径

vim /etc/profile

JAVA_HOME=/usr/java/jdk1.8.0_151
JRE_HOME=/usr/java/jdk1.8.0_151/jre
CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

执行命令 source /etc/profile 使用环境变量生效

测试
java -version

[root@MiWiFi-R3-srv ~]# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)
[root@MiWiFi-R3-srv ~]# 

2 . 安装 elasticsearch 5.6

创建es用户

groupadd elasticsearch

-g 加入分组

useradd -g elasticsearch elasticsearch

es目录权限分配给 elasticsearch 用户和组

chown -R elasticsearch:elasticsearch es目录

切换到 elasticsearch 用户登录

su elasticsearch

切换到es 目录

cd es目录

启动es 

./bin/elasticsearch

3 . 错误解决

  • max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

    elasticsearch启动时遇到的错误 问题翻译过来就是:elasticsearch用户拥有的内存权限太小,至少需要262144;

    解决:
    
    切换到root用户
    
    执行命令:
    
    sysctl -w vm.max_map_count=262144
    
    查看结果:
    
    sysctl -a|grep vm.max_map_count
    
    显示:
    
    vm.max_map_count = 262144
    
    上述方法修改之后,如果重启虚拟机将失效,所以:
    
    解决办法:
    
       /etc/sysctl.conf文件最后添加一行
    
    vm.max_map_count=262144

spring boot websocket

websocket server

  • 导入 websocket 依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
  • 配置 bean
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

@Configuration
public class WebsocketConf {

    @Bean
    public ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }

}
  • 编写服务器端代码

import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 服务地址:ws://127.0.0.1:8080/ws
 */

@ServerEndpoint("/ws")
@Component
public class WebSocketServer {
    /**
     * 客户端会话
     */
    private Session session;

    private CopyOnWriteArrayList<WebSocketServer> webSocketServers = new CopyOnWriteArrayList<>();

    /**
     * 客户端连接事件
     * @param session
     */
    @OnOpen
    public void onOpen(Session session){
        this.session = session;
        webSocketServers.add(this);
        System.out.println("[ 新客户端连接上了] 当前在线客户端数:" + webSocketServers.size());
    }

    /**
     * 断开链接事件
     */
    @OnClose
    public void onClose(){
        webSocketServers.remove(this);
        System.out.println("[ 客户端断开连接 ] 当前在线客户端数:" + webSocketServers.size());
    }

    /**
     * 接收消息事件
     * @param message 接收到的消息
     */
    @OnMessage
    public void onMessage(String message){
        System.out.println("收到消息[" + toString() + "] :" + message );
    }

    /**
     * 广播消息
     * @param message 广播数据
     */
    public void sendMessage(String message){
        if ( webSocketServers != null && webSocketServers.size() > 0 ){
            for (WebSocketServer websocketServer : webSocketServers){
                try {
                    websocketServer.session.getBasicRemote().sendText(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}

websocket 测试客户端 : http://www.blue-zero.com/WebSocket/

spring boot aop

AOP 使用

使用步骤

  • 在pom.xml 文件增加 spring-boot-starter-aop 依赖,默认已经开启
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
  • 编写Aspect 封装横切关注点(日志,监控等)
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LogAspect {

    @Before("execution(* com.pcg.wx1.dao..*.*(..))")
    public void log(){
        System.out.println("切面打印日志中...");
    }
}
  • Aspect 需要纳入到 spring 容器管理 (加入 @Aspect 和 @Component 注解)