从零搭建Web所需服务(五)& 从零搭建微服务SpringCloud(番外)微服务集成ES分词服务

本文涉及的产品
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
注册配置 MSE Nacos/ZooKeeper,182元/月
云原生网关 MSE Higress,422元/月
简介: 本文作为从零搭建Web所需服务系列的第五篇及从零搭建微服务SpringCloud系列的番外篇,捞干介绍如何通过微服务集成ES分词服务。

1、启动ES

image.png

参考文献: 从零搭建Web所需服务(一)Windows下Elasticsearch环境搭建和介绍

2、导入Elasticsearch相关依赖

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>transport</artifactId>
    <version>6.2.2</version>
</dependency>
<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>elasticsearch-rest-client</artifactId>
    <version>7.6.2</version>
</dependency>

3、配置application.yml

server:
  port: 1070  #暴露的端口
spring:
  application:
    name: ES
es-config:  #自定义配置Es参数,可以自定义 es-config 这个名称,例如下面
  esname: my-application
  esip: 127.0.0.1
  esnodes: 9300
#es-config-two:
#  esname: my-application
#  esip: 127.0.0.1
#  esnodes: 9300

4、配置ConfigurationFileModel

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.net.InetAddress;

@Component
//获取application.yml文件中配置的叫 “es-config” 的参数值,并注入到类中
@ConfigurationProperties(prefix = "es-config")
public class ConfigurationFileModel {

    //无参构造
    public ConfigurationFileModel() {
        super();
    }
    //Get-Set方法进行传值
    public String getEsName() {
        return esName;
    }
    public void setEsName(String esName) {
        this.esName = esName;
    }
    public String getEsIp() {
        return esIp;
    }
    public void setEsIp(String esIp) {
        this.esIp = esIp;
    }
    public int getEsNodes() {
        return esNodes;
    }
    public void setEsNodes(int esNodes) {
        this.esNodes = esNodes;
    }
    //有参构造
    public ConfigurationFileModel(String esName, String esIp, int esNodes) {
        this.esName = esName;
        this.esIp = esIp;
        this.esNodes = esNodes;
    }
    //成员变量
    private String esName;
    private String esIp;
    private int esNodes;

    @Bean
    //作者此方案为非标准配置方式,注意业务开发时将@Bean书写到总配置类中
    public TransportClient client(){
        TransportClient client=null;
        try {
            Settings settings=Settings.builder().put("cluster.name",esName).build();
            client = new PreBuiltTransportClient(settings).addTransportAddresses(new TransportAddress(InetAddress.getByName(esIp), esNodes));
            return client;
        }catch (Exception e){
            return null;
        }
    }

}

5、配置ESController

import org.elasticsearch.action.admin.indices.analyze.AnalyzeRequest;
import org.elasticsearch.action.admin.indices.analyze.AnalyzeResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@Configuration
@RestController
@CrossOrigin
@RequestMapping("/es")
public class ESController {
    @Autowired
    ConfigurationFileModel configurationFileModel;
    @GetMapping("/esRequest")
    public void vivi(String one){
        AnalyzeRequest request = (new AnalyzeRequest(null)).analyzer("ik_max_word").text(one);
        List<AnalyzeResponse.AnalyzeToken> tokens = configurationFileModel.client().admin().indices().analyze(request).actionGet().getTokens();
        for (int i=0;i<tokens.size();i++){
            System.out.print(tokens.get(i).getTerm()+"-");
        }
    }
}

6、配置ESServerStart

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
//@EnableConfigurationProperties(ConfigurationFileModel.class) //具体的作用就不介绍了,可以看下这篇文章
/**
 * 关于@EnableConfigurationProperties注解的使用
 * https://blog.csdn.net/wanghuiwei888/article/details/121054828
 * */
public class ESServerStart {
    public static void main(String[] args) {
        SpringApplication.run(ESServerStart.class);
    }
}

7、启动测试

image.png

http://localhost:1070/es/esRequest?str=%E4%B8%AD%E5%8D%8E%E4%BA%BA%E6%B0%91%E5%85%B1%E5%92%8C%E5%9B%BD

image.png

成功


相关文档

从零搭建Web所需服务(一)Windows下Elasticsearch环境搭建和介绍
从零搭建微服务SpringCloud(四)设计SpringCloud服务提供者

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。 &nbsp;
目录
相关文章
|
3月前
|
开发框架 JSON 中间件
Go语言Web开发框架实践:使用 Gin 快速构建 Web 服务
Gin 是一个高效、轻量级的 Go 语言 Web 框架,支持中间件机制,非常适合开发 RESTful API。本文从安装到进阶技巧全面解析 Gin 的使用:快速入门示例(Hello Gin)、定义 RESTful 用户服务(增删改查接口实现),以及推荐实践如参数校验、中间件和路由分组等。通过对比标准库 `net/http`,Gin 提供更简洁灵活的开发体验。此外,还推荐了 GORM、Viper、Zap 等配合使用的工具库,助力高效开发。
|
5月前
|
中间件 Go
Golang | Gin:net/http与Gin启动web服务的简单比较
总的来说,`net/http`和 `Gin`都是优秀的库,它们各有优缺点。你应该根据你的需求和经验来选择最适合你的工具。希望这个比较可以帮助你做出决策。
216 35
|
9月前
|
弹性计算 API 持续交付
后端服务架构的微服务化转型
本文旨在探讨后端服务从单体架构向微服务架构转型的过程,分析微服务架构的优势和面临的挑战。文章首先介绍单体架构的局限性,然后详细阐述微服务架构的核心概念及其在现代软件开发中的应用。通过对比两种架构,指出微服务化转型的必要性和实施策略。最后,讨论了微服务架构实施过程中可能遇到的问题及解决方案。
|
7月前
|
数据采集 Web App开发 API
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
本文介绍了如何使用FastAPI和Selenium搭建RESTful接口,访问免版权图片网站Pixabay并采集图片及其描述信息。通过配置代理IP、User-Agent和Cookie,提高爬虫的稳定性和防封禁能力。环境依赖包括FastAPI、Uvicorn和Selenium等库。代码示例展示了完整的实现过程,涵盖代理设置、浏览器模拟及数据提取,并提供了详细的中文注释。适用于需要高效、稳定的Web数据抓取服务的开发者。
357 15
FastAPI与Selenium:打造高效的Web数据抓取服务 —— 采集Pixabay中的图片及相关信息
|
7月前
|
Cloud Native Java Nacos
springcloud/springboot集成NACOS 做注册和配置中心以及nacos源码分析
通过本文,我们详细介绍了如何在 Spring Cloud 和 Spring Boot 中集成 Nacos 进行服务注册和配置管理,并对 Nacos 的源码进行了初步分析。Nacos 作为一个强大的服务注册和配置管理平台,为微服务架构提供
2990 14
|
7月前
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
395 7
|
9月前
|
存储 JavaScript 开发工具
基于HarmonyOS 5.0(NEXT)与SpringCloud架构的跨平台应用开发与服务集成研究【实战】
本次的.HarmonyOS Next ,ArkTS语言,HarmonyOS的元服务和DevEco Studio 开发工具,为开发者提供了构建现代化、轻量化、高性能应用的便捷方式。这些技术和工具将帮助开发者更好地适应未来的智能设备和服务提供方式。
基于HarmonyOS 5.0(NEXT)与SpringCloud架构的跨平台应用开发与服务集成研究【实战】
|
9月前
|
NoSQL 前端开发 测试技术
👀探秘微服务:从零开启网关 SSO 服务搭建之旅
单点登录(Single Sign-On,简称SSO)是一种认证机制,它允许用户只需一次登录就可以访问多个应用程序或系统。本文结合网关和SaToken快速搭建可用的Session管理服务。
515 8
|
10月前
|
弹性计算 持续交付 API
构建高效后端服务:微服务架构的深度解析与实践
在当今快速发展的软件行业中,构建高效、可扩展且易于维护的后端服务是每个技术团队的追求。本文将深入探讨微服务架构的核心概念、设计原则及其在实际项目中的应用,通过具体案例分析,展示如何利用微服务架构解决传统单体应用面临的挑战,提升系统的灵活性和响应速度。我们将从微服务的拆分策略、通信机制、服务发现、配置管理、以及持续集成/持续部署(CI/CD)等方面进行全面剖析,旨在为读者提供一套实用的微服务实施指南。
|
9月前
|
弹性计算 Kubernetes API
构建高效后端服务:微服务架构的深度剖析与实践####
本文深入探讨了微服务架构的核心理念、设计原则及实现策略,旨在为开发者提供一套系统化的方法论,助力其构建灵活、可扩展且易于维护的后端服务体系。通过案例分析与实战经验分享,揭示了微服务在提升开发效率、优化资源利用及增强系统稳定性方面的关键作用。文章首先概述了微服务架构的基本概念,随后详细阐述了其在后端开发中的应用优势与面临的挑战,最后结合具体实例,展示了如何从零开始规划并实施一个基于微服务的后端项目。 ####