ElasticSearch环境搭建 IDEA基础案例,以及linux上的基础搭建。

首先,你得下载这个文件,解压打开之后,我们看见

打开bin目录。 

 

 

 

 

我们在双击 elasticsearch.bat文件,我们默认用管理员打开。

出现这个打开基本就成功了。 

以上为在windows中操作。

下面介绍我们在linux中操作。以及案例运行。

为了方便,我们用虚拟机来测试。

主机配置。。。。 

我使用 Xshell连接。 

在linux主机中 ,我们使用的是tar.gz,我们先使用Xftp把文件从本地拿到虚拟机中,现在链接上虚拟机,

 

现在进行操作,搭建服务只能在普通用户下,不能在root用户下。

我们之后在改错的时候,用户无权限之类的,要不就在root用户下,给普通用户授权,要不就  su root 到 root用户编辑文件。 

我在根目录下创建了一个ES的文件夹    mkdir ES   

我从主机上拿取的文件  都放在下面:

 启动,只能在普通用户下启动。

  解压后进入解压文件,

 

 

 我们在进入bin

 

 

 我们在 执行   ./elasticsearch  命令 。下面是问题解决方案 。

 

 权限不够自己就想想办法。www.百度.com 

                       ERROR: bootstrap checks failed

    1. max file descriptors [4096] for elasticsearch process likely too low, increase to at least [65536]
    2.  max number of threads [1024] for user [lishang] likely too low, increase to at least [2048]

 

                      解决:切换到root用户,编辑limits.conf 添加类似如下内容

 

    1. vi /etc/security/limits.conf 
    2. 添加如下内容:
    3. * soft nofile 65536
    4. * hard nofile 131072
    5. * soft nproc 2048
    6. * hard nproc 4096

 max virtual memory areas vm.max_count [65530] likely too low,increase to at least [26244] 

 

 基本也就这些错误了。

 

 现在我们开始基础案例的演示,我们首先的配置两个文件。 

 在application.properties文件中加入两行:

 

spring.data.elasticsearch.cluster-nodes=192.168.33.4:9300
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=1200s

ip是启动es的主机ip,那个9300是服务端,如果测试是否开启,在浏览器中输出ip:9200就行。

现在书写案例:
import org.springframework.data.elasticsearch.annotations.Document;

import javax.persistence.Id;
import java.io.Serializable;


//文档
@Document(indexName = "blog",type = "blog")
public class EsBlog implements Serializable {
    @Id //主键
    private String id;
    private String title;
    private String summary;
    private String content;
    protected EsBlog(){

    }

    public EsBlog(String title, String summary, String content) {
        this.title = title;
        this.summary = summary;
        this.content = content;
    }

    @Override
    public String toString() {
        return "EsBlog{" +
                "id='" + id + '\'' +
                ", title='" + title + '\'' +
                ", summary='" + summary + '\'' +
                ", content='" + content + '\'' +
                '}';
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }
}

 

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;




public interface EsBlogRepository extends ElasticsearchRepository<EsBlog,String> {
    //分页查询博客
    Page<EsBlog> findByTitleOrSummaryOrContent(String title, String summary, String content, Pageable pageable);
}

 

测试

import cn.studet.entity.EsBlog;
import cn.studet.entity.EsBlogRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot03ApplicationTests {
    @Autowired
    private EsBlogRepository esBlogRepository;
    @Before
    public void initData() {
        esBlogRepository.deleteAll();
        //测试数据
        esBlogRepository.save(new EsBlog("劝学诗"," 颜真卿的劝学诗","黑发不知勤学早,白首方悔读书迟。"));
        esBlogRepository.save(new EsBlog("冬夜读书示子聿","  陆游的冬夜读书示子聿","纸上得来终觉浅,绝知此事要躬行。"));
        esBlogRepository.save(new EsBlog("泊秦淮","  杜牧的泊秦淮","商女不知亡国恨,隔江犹唱后庭花。"));
    }

    @Test
    public void testFind() {
        Pageable pageable=new PageRequest(0,2);
        String title="诗";
        String summary="劝学诗";
        String content="勤学";
        Page<EsBlog> page = esBlogRepository.findByTitleOrSummaryOrContent(title, summary, content, pageable);
        assertThat(page.getTotalElements()).isEqualTo(1);
        for (EsBlog item:page.getContent()){
            System.out.println(item.getTitle());
        }
    }

}

 

下面我们来说, 

SpringSecurity 的小案例

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
         xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"
>
    <meta charset="UTF-8">
    <title>登录</title>
    <script type="text/javascript" th:src="@{/js/jquery-1.8.3.min.js}"></script>
    <script type="text/javascript">

    </script>
</head>
<body>
   <div>
       <form th:action="@{/login}" method="post">
           <h2>请登录</h2>
           用户名:<input name="username" type="text"/><br/>
           密码:<input name="password" type="password"/><br/>
           <input type="submit" value="登录"/><br/>
           <div th:if="${loginError}"></div>
           <div th:text="${errorMsg}"></div>
       </form>
   </div>
</body>
</html>

  

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
>
<head>
    <meta charset="UTF-8">
    <title>博客系统</title>
    <script type="text/javascript" th:src="@{/js/jquery-1.8.3.min.js}"></script>
    <script type="text/javascript">

    </script>
</head>
<body>
   <div>
      <div sec:authorize="isAuthenticated()">
          <p>登录的用户名为:<span sec:authentication="name"></span></p>
          <p>登录的角色为:<span sec:authentication="principal.authorities"></span></p>
      </div>
       <div sec:authorize="isAnonymous()">
            <p>未登录</p>
       </div>
   </div>
</body>
</html>

  

 

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class loginController {
    @GetMapping("/index")
    public String index(){
        return "index";
    }


    @GetMapping("/login")
    public String login(){
        return "login";
    }

    @GetMapping("/login-error")
    public String loginError(Model model){
        model.addAttribute("loginError",true);
        model.addAttribute("errorMsg","登录失败,用户名或密码错误");
        return "login";
    }
}

 

import org.springframework.security.crypto.password.PasswordEncoder;


public class MyPasswordEncoder implements PasswordEncoder {
    @Override
    public String encode(CharSequence charSequence) {
        return charSequence.toString();
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return s.equals(charSequence);
    }
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;


@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .authorizeRequests()
                .antMatchers("/css/**","/js/**","/fonts/**", "/index").permitAll()
                .antMatchers("/users/**").hasRole("users")
                .and()
                .formLogin()
                .loginPage("/login")
                .failureUrl("/login-error");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
                .inMemoryAuthentication()
                .passwordEncoder(new MyPasswordEncoder())
                .withUser("小明").password("ztm1021810064").roles("users");
    }

}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringbootesApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootesApplication.class, args);
    }
}

自己拿去玩一玩

 

posted on 2018-07-07 09:23  LWJDD  阅读(173)  评论(0编辑  收藏  举报

导航