关闭

SPRING BOOT+ELASTICSEARCH+RABBITMQ

98人阅读 评论(0) 收藏 举报
分类:
  • 直接进入正题 构思的流程图:
Created with Raphaël 2.1.0请求用户身份验证数据库操作AOPRABBITMQ搜索项目操作返回ACK返回结果集

. 安装RABBITMQ

echo 'deb http://www.rabbitmq.com/debian/ testing main' | sudo tee /etc/apt/sources.list.d/rabbitmq.list

wget -O- https://www.rabbitmq.com/rabbitmq-release-signing-key.asc | sudo apt-key add -

sudo apt-get update

sudo apt-get install rabbitmq-server

sudo rabbitmqctl add_user admin admin 

sudo rabbitmqctl set_user_tags admin administrator

sudo rabbitmq-plugins enable rabbitmq_management

http://192.168.10.69:15672

. 安装elasticSearch

注:spring-boot-starter-data-elasticsearch不支持高版本es
采用2.4.4版本~ 另外es自身不带可视化页面,需要安装head~
https://www.elastic.co/products/elasticsearch download deb

dpkg -i elasticsearch-2.4.4.deb

/etc/init.d/elasticsearch start 

head:
cd elasticsearch-2.4.4/bin/plugin

/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head

http://192.168.10.69:9200/_plugin/head/

. 最繁琐的部署环节已经完成 踩了无数个坑 心累~
接下来首先在主项目配置rabbit provider

amqp-client-3.5.4.jar
spring-amqp-1.5.6.RELEASE.jar
<!-- mq start -->
<!-- connection -->
<rabbit:connection-factory id="connectionFactory"
        host="${mq.host}" username="${mq.username}" password="${mq.password}"
        port="${mq.port}" virtual-host="${mq.vhost}" />

<rabbit:admin connection-factory="connectionFactory" id="amqpAdmin" />

<!-- json convert -->
<bean id="jsonMessageConverter"
        class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />

<!-- es queue -->
<rabbit:queue id="es_queue" name="es_queue" durable="true"
        auto-delete="false" exclusive="false" />

<!-- exchange -->
<rabbit:direct-exchange name="es_exchange" id="es_exchange"
        durable="true" auto-delete="false">
        <rabbit:bindings>
            <rabbit:binding queue="es_queue" key="es_queue" />
        </rabbit:bindings>
</rabbit:direct-exchange>

<!-- template -->
<rabbit:template id="amqpTemplate" exchange="es_exchange"
        connection-factory="connectionFactory" message-converter="jsonMessageConverter"/>
<!-- mq end -->
@Service
public class EsMqServiceImpl implements EsMqService {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public void pushToMessageQueue(String exchange, String route, String message) {
        amqpTemplate.convertAndSend(exchange, route, message);
    }
}
rabbitmq还是值得一学的,稍微看了下《RabbitMQ实战 高效部署分布式消息队列》,看到第四章实在看不下去了,有点涩,又缺乏实战的机会。但感觉基本概念理解了,平时操作也够用了。

. 搜索项目搭建

这里使用SPRING-BOOT代替SPRING 2个原因:
1.SPRING-BOOT实在是方便
2.是因为自己想实战下

新建方法: 
1.STS插件
2.http://projects.spring.io/spring-boot/ 

.pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 热部署 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency> 

        <!-- ES -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>

        <!-- RabbitMQ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

.application.properties

spring.application.name=elasticsearch

#mq
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=panchen
spring.rabbitmq.username=panchen
spring.rabbitmq.password=panchen

#es
spring.data.elasticsearch.cluster-nodes=localhost:9300
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.repositories.enabled=true  

.entity

/**
 * @Field :FieldType type() default FieldType.Auto;#自动检测属性的类型 FieldIndex index()
 *        default FieldIndex.analyzed;#默认情况下分词 DateFormat format() default
 *        DateFormat.none; String pattern() default ""; boolean store() default
 *        false;#默认情况下不存储原文 String searchAnalyzer() default "";#指定字段搜索时使用的分词器
 *        String indexAnalyzer() default "";#指定字段建立索引时指定的分词器 String[]
 *        ignoreFields() default {};#如果某个字段需要被忽略 boolean includeInParent()
 *        default false; 不加则默认值
 * @Document: 索引库命名 / 类型 / 分区数 / 每个分区的备份数 / 刷新间隔 / 文件存储类型
 */

@Document(indexName = "userinfo_index", type = "userInfo", shards = 5, replicas = 1, refreshInterval = "-1", indexStoreType = "fs")
public class UserInfo implements Serializable {

    private static final long serialVersionUID = 551589397625941750L;

    @Id
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }

    @Override
    public String toString() {
        return "UserInfo [id=" + id + ", name=" + name + "]";
    }

}

.repository ~~ dao

/**
 * 接口中自定义方法,在方法名中包含T中字段名 查询关键字包括find…By, read…By, query…By, count…By, and
 * get…By,And and Or连接 泛型的参数分别是实体类型和主键类型
 * 
 * @author pc
 */

public interface UserInfoRepository extends ElasticsearchRepository<UserInfo, Long> {
}

. test

@SpringBootApplication
//这个注解是个大坑!!!
@EnableElasticsearchRepositories
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CompanyElasticsearchApplication.class)
public class SearchRepositoryTest {

    @Autowired
    // spring Boot默认会扫描启动类同包以及子包下的注解
    private UserInfoRepository userInfoRepository;

    @Test
    public void test() {
        System.out.println("start");
        UserInfo userInfo=new UserInfo();
        userInfo.setId(1l);
        userInfo.setName("潘晨");
        System.out.println(userInfoRepository.save(userInfo));
        System.out.println("新增成功");
        System.out.println(userInfoRepository.count());
        System.out.println("查询成功");
        System.out.println("end");
    }
}

. OK!

1
0

猜你在找
【直播】机器学习&深度学习系统实战(唐宇迪)
【直播】Kaggle 神器:XGBoost 从基础到实战(冒教授)
【直播回放】深度学习基础与TensorFlow实践(王琛)
【直播】计算机视觉原理及实战(屈教授)
【直播】机器学习之凸优化(马博士)
【直播】机器学习之矩阵(黄博士)
【直播】机器学习之概率与统计推断(冒教授)
【直播】机器学习之数学基础
【直播】TensorFlow实战进阶(智亮)
【直播】深度学习30天系统实训(唐宇迪)
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:544次
    • 积分:52
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:2篇
    • 译文:0篇
    • 评论:1条
    文章分类