1. 程式人生 > >基於idea的springcloud的helloworld項目搭建過程整理

基於idea的springcloud的helloworld項目搭建過程整理

成功 無法 添加 超時時間 final ava lte ehcache stand

Springcloud的搭建主要包括三個部分:服務註冊中心、服務提供者、服務消費者。每一個部分都是一個springboot項目,它們通過配置文件(application.properties或application.yml)關聯在一起。

一、創建服務註冊中心

1、 按照如下過程依次操作

2、 在application.properties中填寫如下內容,聲明本服務是一個註冊中心

server.port:7770
eureka.instance.hostname:localhost
#一下兩行表明本服務是一個註冊中心,而非服務提供或者消費方
eureka.client.registerWithEureka

: false
eureka.client.fetchRegistry
: false
#服務註冊到哪裏
eureka.client.serviceUrl.defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
#本服務的名稱
spring.application.name: eurka-server

3、 在啟動類上面添加@EnableEurekaServer註解即可

4、 啟動服務即可,註意服務端口是application.properties中配置的server.port

二、創建服務提供者

1、 類似註冊中心創建springboot,只是以下這個地方有些不同。

2、 在application.properties中進行註冊信息的配置

#本服務端口號
server.port: 7771
#本服務名
spring.application.name: hello-springcloud-service
#註冊中心的地址
eureka.client.serviceUrl.defaultZone: http://localhost:7770/eureka/

註:服務名可以重復,會認為是同一個服務,負載均衡使用

3、 在啟動類上面添加@EnableEurekaClient註解

4、 創建VO,用於接收參數

1) VO類的屬性名要與參數名完全一致

2) 提供setter/getter方法

3) Get和Post接收的vo不要共用一個,最好分著設計

5、 創建Controller

1) 本質上就是ssh中創建controller一樣,註解使用@RestController

2) 寫出這個請求是get還是post請求

3) 返回值類型可以是字符串或者map、List等類型都是可以的

4) 參數註意事項

A) 如果是get請求,可以使用VO這個pojo類接收,也可以通過“類型 參數名”這種方式接收(普通類型的參數接收而已)。

最正規的方式是:@RequestParam(“參數名”) 類型 參數名這種方式。

B) 如果是post請求,必須使用VO這個pojo類的方式接收,由於傳遞的是json字符串,所以必須使用@RequestBody修飾。而且,只能有一個@RequestBody

例如:@RequestBody PoJo類 pojo類的對象

此外,如果請求中除了一個json字符串以外,還有其他參數,那麽json字符串用@RequestBody的方式接收,而其余參數參考get的方式接收即可。

5) 該Controller不僅可以作為其他服務的提供者,也可以直接通過前端請求。只是註意post請求的特殊處理。

package com.lennar.serviceprovider.controller;

import com.lennar.serviceprovider.vo.GetVO;
import com.lennar.serviceprovider.vo.HelloWorldTestVO;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MyController {

@Value("${server.port}")
String port;//負載均衡的時候便於打印識別是哪個服務(端口不同,服務不同)

//1.get請求
//1.1.返回字符串
@RequestMapping(value = "/test/getMethod1", method = RequestMethod.GET)
public String getMethod1(GetVO getVO) {
return "get-字符串(RequestMapping):hello " + getVO.getStudentName() + " ,I am from port:" + port;
}

// public String getMethod1(@RequestParam(value = "studentName") String name) {
// return "get-字符串(RequestMapping):hello " + name + " ,I am from port:" + port;
// }

@GetMapping(value = "/test/getMethod2")
public String getMethod2(@RequestParam(value = "studentName") String name) {
return "get-字符串(GetMapping):hello " + name + " ,I am from port:" + port;
}

//1.2.返回Map
@RequestMapping(value = "/test/getMethod3", method = RequestMethod.GET)
public Map<String, Object> getMethod3(@RequestParam(value = "studentName") String name) {
Map<String, Object> map = new HashMap<>();
map.put("resultMessage", "get-Map(RequestMapping):hello " + name + " ,I am from port:" + port);
return map;
}

@GetMapping(value = "/test/getMethod4")
public Map<String, Object> getMethod4(@RequestParam(value = "studentName") String name) {
Map<String, Object> map = new HashMap<>();
map.put("resultMessage", "get-Map(GetMapping):hello " + name + " ,I am from port:" + port);
return map;
}

//2.post請求
//2.1.返回字符串
@RequestMapping(value = "/test/postMethod1", method = RequestMethod.POST)
public String postMethod1(GetVO getVO, @RequestBody HelloWorldTestVO helloWorldTestVO) {
return "post-字符串(RequestMapping):hello " + helloWorldTestVO.getName() + "(" + helloWorldTestVO.getAge() + ")" + "your score is " + getVO.getStudentScore() + " ,I‘m from port:" + port;
}
// @RequestMapping(value = "/test/postMethod1", method = RequestMethod.POST)
// public String postMethod1(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO) {
// return "post-字符串(RequestMapping):hello " + helloWorldTestVO.getName() + "(" + helloWorldTestVO.getAge() + ")" + "your score is " + score + " ,I‘m from port:" + port;
// }

@PostMapping(value = "/test/postMethod2")
public List<String> postMethod2(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO) {
List<String> list = new ArrayList<>();
list.add("post-字符串(PostMapping):hello " + helloWorldTestVO.getName() + "(" + helloWorldTestVO.getAge() + ")" + "your score is " + score + " ,I‘m from port:" + port);
return list;

}

//1.2.返回Map
@RequestMapping(value = "/test/postMethod3", method = RequestMethod.POST)
public Map<String, Object> postMethod3(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO) {
Map<String, Object> map = new HashMap<>();
map.put("resultMessage", "post-map(RequestMapping):hello " + helloWorldTestVO.getName() + "(" + helloWorldTestVO.getAge() + ")" + "your score is " + score + " ,I‘m from port:" + port);
return map;
}

@PostMapping(value = "/test/postMethod4")
public Map<String, Object> postMethod4(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO) {
Map<String, Object> map = new HashMap<>();
map.put("resultMessage", "post-map(PostMapping):hello " + helloWorldTestVO.getName() + "(" + helloWorldTestVO.getAge() + ")" + "your score is " + score + " ,I‘m from port:" + port);
return map;
}
}

6、 啟動即可(前提:註冊中心處於開啟狀態)

註:為了實現負載均衡的集群效果,可以修改端口,多啟動幾個項目。

可以參考:https://blog.csdn.net/forezp/article/details/76408139

三、創建服務消費者(使用@Feign版本)

1、 創建項目與二中一樣

2、 在application.properties中添加如下內容,以便消費服務

#註冊中心的地址
eureka.client.serviceUrl.defaultZone: http://localhost:7770/eureka/
#本服務的端口
server.port: 7772
#服務名
spring.application.name: service-consumer-feign


#調用集群的時候需要設置超時時間,否則可能無法連接成功或者無法正常返回數據
#程序會報nested exception is feign.RetryableException: connect timed out executing POST類似的超時錯誤
#請求連接的超時時間(單位:毫秒)
ribbon.ConnectTimeout: 30000
#請求處理的超時時間(單位:毫秒)
ribbon.ReadTimeout: 120000

3、 在啟動類上添加註解@EnableEurekaClient、@EnableDiscoveryClient、@EnableFeignClients

4、 創建VO,路徑不需要和服務提供者完全一樣,只需要保證參數名一致即可。主要用於調用服務時傳遞參數(尤其是post請求)

5、 創建service層接口即可,主要用於調用服務提供者。服務提供者接口處如何編寫調用規則(mapping類型、請求方式、參數格式以及返回類型等),這裏就如何調用和編寫接口。

package com.lennar.service;

import com.lennar.serviceconsumer.HelloWorldTestVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//value中為服務名
//FeignClient內部封裝了ribbon,可以負載均衡
@FeignClient(value = "hello-springcloud-service")//value就是服務提供者的服務名稱,在服務提供者的application.properties中設置
public interface FeignService {

//1.get請求
//1.1.返回字符串
@RequestMapping(value = "/test/getMethod1", method = RequestMethod.GET)
String myGetQuery1(@RequestParam(value = "studentName") String myName);

@GetMapping(value = "/test/getMethod1")
String myGetQuery2(@RequestParam(value = "studentName") String myName);

@RequestMapping(value = "/test/getMethod2", method = RequestMethod.GET)
String myGetQuery3(@RequestParam(value = "studentName") String myName);

@GetMapping(value = "/test/getMethod2")
String myGetQuery4(@RequestParam(value = "studentName") String myName);

//1.2.返回map
@RequestMapping(value = "/test/getMethod3", method = RequestMethod.GET)
Map<String, Object> myGetQuery5(@RequestParam(value = "studentName") String myName);

@GetMapping(value = "/test/getMethod3")
Map<String, Object> myGetQuery6(@RequestParam(value = "studentName") String myName);

@RequestMapping(value = "/test/getMethod4", method = RequestMethod.GET)
Map<String, Object> myGetQuery7(@RequestParam(value = "studentName") String myName);

@GetMapping(value = "/test/getMethod4")
Map<String, Object> myGetQuery8(@RequestParam(value = "studentName") String myName);

//2.post請求
//2.1.返回字符串
@RequestMapping(value = "/test/postMethod1", method = RequestMethod.POST)
String myPostQuery1(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@PostMapping(value = "/test/postMethod1")
String myPostQuery2(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@RequestMapping(value = "/test/postMethod2", method = RequestMethod.POST)
List<String> myPostQuery3(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@PostMapping(value = "/test/postMethod2")
List<String> myPostQuery4(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

//2.2.返回map
@RequestMapping(value = "/test/postMethod3", method = RequestMethod.POST)
Map<String, Object> myPostQuery5(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@PostMapping(value = "/test/postMethod3")
Map<String, Object> myPostQuery6(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@RequestMapping(value = "/test/postMethod4", method = RequestMethod.POST)
Map<String, Object> myPostQuery7(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);

@PostMapping(value = "/test/postMethod4")
Map<String, Object> myPostQuery8(@RequestParam(value = "studentScore") String score, @RequestBody HelloWorldTestVO helloWorldTestVO);
}

6、 創建Controller,就是前端調用的Controller,Controller中的方法調用5中的Service接口的方法,進而調用服務提供者的方法。

package com.lennar.controller;

import com.lennar.service.FeignService;
import com.lennar.serviceconsumer.HelloWorldTestVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class MyController {

@Autowired
FeignService feignService;

//1.get請求
@GetMapping("/hello1")
public String sayGetHello1(@RequestParam("name") String name) {
return feignService.myGetQuery1(name);
}

@GetMapping("/hello2")
public String sayGetHello2(@RequestParam("name") String name) {
return feignService.myGetQuery2(name);
}

@GetMapping("/hello3")
public String sayGetHello3(@RequestParam("name") String name) {
return feignService.myGetQuery3(name);
}

@GetMapping("/hello4")
public String sayGetHello4(@RequestParam("name") String name) {
return feignService.myGetQuery4(name);
}

@GetMapping("/hello5")
public Map<String, Object> sayGetHello5(@RequestParam("name") String name) {
return feignService.myGetQuery5(name);
}

@GetMapping("/hello6")
public Map<String, Object> sayGetHello6(@RequestParam("name") String name) {
return feignService.myGetQuery6(name);
}

@GetMapping("/hello7")
public Map<String, Object> sayGetHello7(@RequestParam("name") String name) {
return feignService.myGetQuery7(name);
}

@GetMapping("/hello8")
public Map<String, Object> sayGetHello8(@RequestParam("name") String name) {
return feignService.myGetQuery8(name);
}

//2.post請求
@PostMapping(value = "/hello1")
public String sayPostHello1(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery1(score, helloWorldTestVO);
}

@PostMapping(value = "/hello2")
public String sayPostHello2(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery2(score, helloWorldTestVO);
}

@PostMapping(value = "/hello3")
public List<String> sayPostHello3(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery3(score, helloWorldTestVO);
}

@PostMapping(value = "/hello4")
public List<String> sayPostHello4(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery4(score, helloWorldTestVO);
}

@PostMapping(value = "/hello5")
public Map<String, Object> sayPostHello5(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery5(score, helloWorldTestVO);
}

@PostMapping(value = "/hello6")
public Map<String, Object> sayPostHello6(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery6(score, helloWorldTestVO);
}

@PostMapping(value = "/hello7")
public Map<String, Object> sayPostHello7(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery7(score, helloWorldTestVO);
}

@PostMapping(value = "/hello8")
public Map<String, Object> sayPostHello8(String name, String age, String score) {
HelloWorldTestVO helloWorldTestVO = new HelloWorldTestVO();
helloWorldTestVO.setName(name);
helloWorldTestVO.setAge(age);
return feignService.myPostQuery8(score, helloWorldTestVO);
}

}

7、 啟動即可

註:消費者只會調用服務提供者已經註冊(先於消費者打開服務前已經在註冊中心註冊過的)且處於打開狀態的服務。

四、創建前端調用項目(以AJAX為例)

1、 保證谷歌瀏覽器跨域,並且使用該瀏覽器打開前端服務。

https://jingyan.baidu.com/article/148a1921c9dbf24d71c3b11f.html

2、 編寫前端代碼即可

修改pom文件中的properties和dependencies

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.4.RELEASE</spring.version>
<hibernate.version>4.3.8.Final</hibernate.version>
<jackson.version>2.5.0</jackson.version>
</properties>

<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- spring -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>

<!-- 使用SpringMVC需配置 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- 關系型數據庫整合時需配置 如hibernate jpa等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>

<!-- hibernate -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
</dependency>

<!-- 二級緩存ehcache -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.9.0</version>
</dependency>

<!-- log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.14</version>
</dependency>

<!--postgresql-->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>

<!-- mysql連接 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

<!--oracle-->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3</version>
</dependency>

<!-- c3p0數據源 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5-pre10</version>
</dependency>

<!-- json -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.3</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>

<!-- aop -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--用於保證jsp中基本api使用,比如request的方法等-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>

</dependencies>

修改web.xml(將原來的web.xml覆蓋即可)

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<!--<context-param>-->
<!--<param-name>contextConfigLocation</param-name>-->
<!--<param-value>classpath:spring.xml</param-value>-->
<!--</context-param>-->

<!--<listener>-->
<!--<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>-->
<!--</listener>-->

<!--<servlet>-->
<!--<servlet-name>springDispatcherServlet</servlet-name>-->
<!--<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>-->
<!--<init-param>-->
<!--<param-name>contextConfigLocation</param-name>-->
<!--<param-value>classpath:springmvc.xml</param-value>-->
<!--</init-param>-->
<!--<load-on-startup>1</load-on-startup>-->
<!--</servlet>-->

<!--<servlet-mapping>-->
<!--<servlet-name>springDispatcherServlet</servlet-name>-->
<!--<url-pattern>*.action</url-pattern>-->
<!--</servlet-mapping>-->

<!--springmvc中文亂碼-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param> <!--字符的編碼格式 -->
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param> <!--強制使用Encoding設置的編碼格式-->
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

</web-app>

添加jquery.js並在index.jsp中導入

(index.jsp)

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<
head>
<
title>首界面</title>
<
style>
</
style>
</
head>
<
body>
<
hr>
<
hr>
<
div id="testDivId"></div>
<
hr>
<
hr>
<
div><font color="red">向消費者發出請求</font></div>
<
button class="testGet" id="testGetBtnId1">testGet1</button>
<
button class="testGet" id="testGetBtnId2">testGet2</button>
<
button class="testGet" id="testGetBtnId3">testGet3</button>
<
button class="testGet" id="testGetBtnId4">testGet4</button>
<
button class="testGet" id="testGetBtnId5">testGet5</button>
<
button class="testGet" id="testGetBtnId6">testGet6</button>
<
button class="testGet" id="testGetBtnId7">testGet7</button>
<
button class="testGet" id="testGetBtnId8">testGet8</button>
<
br>
<
br>
<
br>
<
button class="testPost" id="testPostBtnId1">testPost1</button>
<
button class="testPost" id="testPostBtnId2">testPost2</button>
<
button class="testPost" id="testPostBtnId3">testPost3</button>
<
button class="testPost" id="testPostBtnId4">testPost4</button>
<
button class="testPost" id="testPostBtnId5">testPost5</button>
<
button class="testPost" id="testPostBtnId6">testPost6</button>
<
button class="testPost" id="testPostBtnId7">testPost7</button>
<
button class="testPost" id="testPostBtnId8">testPost8</button>
<
br>
<
hr>
<
hr>
<
div><font color="red">向提供者發出請求</font></div>
<
button class="testProviderGet" id="testProviderGet1">testProviderGet1</button>
<
button class="testProviderGet" id="testProviderGet2">testProviderGet2</button>
<
button class="testProviderGet" id="testProviderGet3">testProviderGet3</button>
<
button class="testProviderGet" id="testProviderGet4">testProviderGet4</button>
<
br>
<
br>
<
br>
<
button class="testProviderPost" id="testProviderPost1">testProviderPost1</button>
<
button class="testProviderPost" id="testProviderPost2">testProviderPost2</button>
<
button class="testProviderPost" id="testProviderPost3">testProviderPost3</button>
<
button class="testProviderPost" id="testProviderPost4">testProviderPost4</button>
<
script>
var contextPath = "${pageContext.request.contextPath}";
</
script>
<
script src="${pageContext.request.contextPath}/js/jquery.js"></script>
<
script src="${pageContext.request.contextPath}/js/myTestJS.js"></script>
</
body>
</
html>

(myTestJS.js文件)

$(function () {
//向消費者發送請求,進而消費者調用服務者的服務,提供數據
$(".testGet").on("click", function () {
var flag = $(this).text().split("testGet")[1];
$.ajax({
url: "http://10.17.66.19:7772/hello" + flag,
data: {name: "張三"},
type: "GET",
success: function (res) {
if (Number(flag) > 4) {
$("#testDivId").html(res.resultMessage);
} else {
$("#testDivId").html(res);
}
}
});
});

$(".testPost").on("click", function () {
var flag = $(this).text().split("testPost")[1];
$.ajax({
url: "http://10.17.66.19:7772/hello" + flag,
data: {name: "王五", age: "24", score: "110"},
type: "POST",
success: function (res) {
if (Number(flag) > 4) {
$("#testDivId").html(res.resultMessage);
} else {
$("#testDivId").html(res);
}
}
});
});

//直接向服務提供者發送請求
//只是post請求中,如果有@RequestParam,則需要將參數放在請求頭部,而@RequestBody修飾的內容放在data的json字符串中,且設置contentType: "application/json"
$(".testProviderGet").on("click", function () {
var flag = $(this).text().split("testProviderGet")[1];
$.ajax({
url: "http://10.17.66.19:7771/test/getMethod" + flag,
data: {studentName: "李四"},
type: "GET",
success: function (res) {
if (Number(flag) > 2) {
$("#testDivId").html(res.resultMessage);
} else {
$("#testDivId").html(res);
}
}
});
});

$(".testProviderPost").on("click", function () {
var flag = $(this).text().split("testProviderPost")[1];
$.ajax({
url: "http://10.17.66.19:7771/test/postMethod" + flag + "?studentScore=120",
data: JSON.stringify({name: "王五", age: "24"}),
type: "POST",
contentType: "application/json",
success: function (res) {
if (Number(flag) > 2) {
$("#testDivId").html(res.resultMessage);
} else {
$("#testDivId").html(res);
}
}
});
});
});

3、 配置一個Tomcat,啟動項目,使用跨域的谷歌瀏覽器測試即可

五、註意事項

1、 通過idea創建項目的時候,https://start.spring.io很有可能不能使用,這個時候,通過點擊一下,在瀏覽器中打開一次,然後回到idea,再試一試。

2、 可以直接到https://start.spring.io中去創建項目。對於註冊中心只需要Eureka Server;對於服務提供者需要web和Eureka Discovery;對於服務消費者需要web、Eureka Discovery和Feign。

基於idea的springcloud的helloworld項目搭建過程整理