前言
Feign基于Netflix Feign实现,整合了Ribbon和Hystrix,Feign的功能包含了其他两者的功能。
此serverClientA工程是在Spring Boot+Spring Cloud微服务案例演练(1):服务注册与发现步骤3中的基础上完成的。
一、搭建serverClientA服务工程
1.修改pom.xml配置文件,添加相关依赖
<!--服务调用方 添加依赖Feign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>1.4.5.RELEASE</version>
</dependency>
<!-- 添加thymeleaf依赖-->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.修改application.properties配置文件
#启用Feign
feign.hystrix.enabled=true
#Thymeleaf配置
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.cache=false
在新版本中Feign对Hystrix的支持默认是关闭的,所以要通过配置手动打开feign.hystrix.enabled=true,这样服务降级等功能才有效果。
3.启动类中添加注解@EnableFeignClients
通过@EnableFeignClients来开启Spring Cloud的Feign支持功能。
package com.lpc.serverClientA;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ServerClientAApplication {
public static void main(String[] args) {
SpringApplication.run(ServerClientAApplication.class, args);
}
}
4.新建MessageGenerationController类
该类负责将数据以xml报文的形式存储,并提供返回xml报文的方法供serverclientB调用。
package com.lpc.serverClientA.controller;
import com.lpc.serverClientA.entity.InputData;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.text.SimpleDateFormat;
import java.util.Date;
@Controller
public class MessageGenerationController {
private String result, time;
private Logger logger = LoggerFactory.getLogger(getClass());
@RequestMapping(value = "/index", produces = "text/plain;charset=UTF-8")
public String index(ModelMap map) {
InputData inputData = new InputData();
time = getDate();
inputData.setDate(time);
map.put("fpInfo", inputData);
return "index";
}
@ResponseBody
@RequestMapping(value = "/generateXML", method = RequestMethod.POST, produces = "text/plain;charset=UTF-8")
private String generate(@ModelAttribute InputData inputData) {
logger.info("inputData = " + inputData.toString());
result = rtnXML(inputData);
return "result = " + result;
}
//生成xml报文
private String rtnXML(InputData inputData) {
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("utf-8");
Element root = document.addElement("FPXT");//添加根节点
Element output = root.addElement("OUTPUT");
Element fpzl = output.addElement("FPZL");
fpzl.addText(inputData.getFpzl());
Element fpzt = output.addElement("FPZT");
fpzt.addText(inputData.getFpzt());
Element nsrsbh = output.addElement("NSRSBH");
nsrsbh.addText(inputData.getNsrsbh());
Element date = output.addElement("DATE");
date.addText(time);
Element fpdm = output.addElement("FPDM");
fpdm.addText(inputData.getFpdm());
String resultXML = document.asXML();
logger.debug("resultXML = " + resultXML);
return resultXML;
}
//获取当前时间
private String getDate() {
Date date = new Date();
SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
return simpleDate.format(date);
}
//返回xml报文,供serverClientB服务调用
@RequestMapping(value = "/generate", method = RequestMethod.GET)
public ResponseEntity<Object> generate() {
logger.info("resultXML = " + result);
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
}
5.新建InputData实体类
package com.lpc.serverClientA.entity;
import java.io.Serializable;
public class InputData implements Serializable {
String nsrsbh;//纳税人识别号
String fpdm;//发票代码
String fpzl;//发票种类
String fpzt;//发票状态
String date;//日期
public String getNsrsbh() {
return nsrsbh;
}
public void setNsrsbh(String nsrsbh) {
this.nsrsbh = nsrsbh;
}
public String getFpdm() {
return fpdm;
}
public void setFpdm(String fpdm) {
this.fpdm = fpdm;
}
public String getFpzl() {
return fpzl;
}
public void setFpzl(String fpzl) {
this.fpzl = fpzl;
}
public String getFpzt() {
return fpzt;
}
public void setFpzt(String fpzt) {
this.fpzt = fpzt;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
@Override
public String toString() {
return "InputData{" +
"nsrsbh='" + nsrsbh + '\'' +
", fpdm='" + fpdm + '\'' +
", fpzl='" + fpzl + '\'' +
", fpzt='" + fpzt + '\'' +
", date='" + date + '\'' +
'}';
}
}
6.新建页面index.html
这里用到了Thymeleaf模板引擎,下面是一些简单介绍。
Thymeleaf是一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似JSP,Velocity,FreeMaker等,它也可以轻易地与Spring MVC等Web框架进行集成作为Web应用的模板引擎。与其它模板引擎相比,Thymeleaf最大的特点是能够直接在浏览器中打开并正确显示模板页面,而不需要启动整个Web应用。
它的功能特性如下:
Spring MVC中@Controller中的方法可以直接返回模板名称,接下来Thymeleaf模板引擎会自动进行渲染。
模板中的表达式支持Spring表达式语言(Spring EL)。
表单支持,并兼容Spring MVC的数据绑定与验证机制。
国际化支持。
注:
- 通过post方式提交表单的时候,需要有一个实体类,去接收表单传递的数据内容。
- 在对象的属性读取中,Thymeleaf 提供了两种方式:
(1)直接通过${userInfo.username} ,这种实体bean + 属性的方式。
(2)通过选择表达式*{username}的这种方式。 - 在表单提交的表单中,表单对象需要在界面跳转进来的时候,传递一个表单对象过来。
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>生成发送报文</title>
</head>
<body>
<form action="#" name="generation" th:action="@{/generateXML}" th:object="${fpInfo}" method="post">
<table>
<tr>
<td>纳税人识别号:</td>
<td>
<label>
<input type="text" th:field="*{nsrsbh}">
</label>
</td>
</tr>
<tr>
<td>发票代码:</td>
<td>
<label>
<input type="text" th:field="*{fpdm}">
</label>
</td>
</tr>
<tr>
<td>发票种类:</td>
<td>
<label>
<input type="text" th:field="*{fpzl}">
</label>
</td>
</tr>
<tr>
<td>发票状态:</td>
<td>
<label>
<input type="text" th:field="*{fpzt}">
</label>
</td>
</tr>
<tr>
<td>时间:</td>
<td>
<label>
<p th:text="*{date}"></p>
</label>
</td>
</tr>
</table>
<input type="submit" value="生成报文"/>
</form>
</body>
</html>
运行项目后,访问此地址:http://localhost:2223/index,显示如下页面:
在输入框中输入数据后,点击生成报文按钮,跳转到generateXML并返回xml报文字符串:
参考链接:
https://blog.csdn.net/yelllowcong/article/details/79830468