SPRING BOOT+ELASTICSEARCH+RABBITMQ
阿新 • • 發佈:2019-02-02
- 直接進入正題 構思的流程圖:
. 安裝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!