- 新增news-search项目新闻前台项目
- 通过feign调用news-service项目中暴露接口完成新闻检索
1 新增news-search导入项目依赖,由于需要控制器和视图解析,后面可能会用到redis做缓存导入如下依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gc.springboot</groupId>
<artifactId>news-search</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR2</spring-cloud.version>
</properties>
<!-- springboot 父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<!-- web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<!-- log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- maven插件 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2 配置application.properties文件 配置注册中心的地址和服务名称端口号
server.port=8092
spring.application.name=news-search
eureka.client.serviceUrl.defaultZone=http://localhost:8091/eureka/
3 启动了添加服务发现注解@EnableEurekaClient
/**
* 新闻前台项目
* @author jiji
*
*/
@SpringBootApplication
@EnableEurekaClient
public class APP {
public static void main(String[] args) {
SpringApplication.run(APP.class, args);
}
}
4 启动项目(在启动项目之前需先启动注册中心)
在图中可以看到news-search项目个news-service项目已成功注册到注册中心。
4 继续在news-search项目中添加feign的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
5 新建newsSearchServicer接口(feign使用的是接口的方式)
package com.gc.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
/**
*
* @author gc
* 新闻检索service
* NEWS-SERVICE: 注册到注册中心的项目名称
* 注意:在方法中传递多个参数的时候 使用@RequestParam注解 窦泽会报 Method has too many Body parameters: public abstract 这个错误
*
* listNewData 提供接口 请求参数和新闻后台项目调用新闻列表的入参保持一致
*
* @RequestMapping 注解中method 请求方式 value 新闻后台项目控制器 调用新闻后台项目的请求接口
*
*/
@FeignClient(value="NEWS-SERVICE")
public interface NewsSearchService {
@RequestMapping(method=RequestMethod.GET,value="/news/list")
public Object listNewData(@RequestParam("pagesize") String pagesize,@RequestParam("currentPage") String currentPage, @RequestParam("key") String key);
}
6 新建NewsSearchController的新闻检索控制器类,注入 newsSearchServicer提供简单的调用方法
package com.gc.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.service.NewsSearchService;
/**
* 新闻项目检索控制器
* @author gc
*
*/
@RequestMapping("newsSearch")
@Controller
public class NewsSearchController {
@Autowired
private NewsSearchService newService;
@RequestMapping("/list")
@ResponseBody
public Object getList(){
return newService.listNewData("30", "1", "");
}
}
7 启动新闻检索项目,并访问http://localhost:8092/newsSearch/list项目地址。出现请求超时的异常
java.net.SocketTimeoutException: Read timed out 请求超时
原因我们未在配置文件中配置feign的请求超时时间,修改application.properties文件,配置feign的超时时间和日志级别
eureka.client.serviceUrl.defaultZone=http://localhost:8091/eureka/
feign.client.config.default.connectTimeout=2000
feign.client.config.default.readTimeout=2000
feign.client.config.default.loggerLevel=basic
8 再次启动项目,访问接口成功的得到响应的结果数据。
9 配置springmvc的视图解析器,使用html
package com.gc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class MVCConfig implements WebMvcConfigurer {
@Bean
public InternalResourceViewResolver viewResolver(){
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/html/");
viewResolver.setSuffix(".html");
viewResolver.setContentType("text/html;charset=UTF-8");
viewResolver.setExposeContextBeansAsAttributes(true);;
viewResolver.setOrder(1);
viewResolver.setCache(false);
return viewResolver;
}
}
10 修改application.properties文件
spring.resources.static-locations =classpath:/js/,classpath:/css/
11 编写视图控制器
package com.gc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/page/{name}")
public String toPage(@PathVariable("name") String name){
return name;
}
}
12 访问http://localhost:8092/page/index 首页 可看到
视图解析器和新闻前台项目就搭建完成了,下面使用jq解析返回的json数据,在页面上完善检索功能。