一、创建SpringBoot项目
删除SpringBoot的src文件夹
二、commons 通用项目
1.新建 maven 项目(commons)
项目结构
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sp01-commons</artifactId>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-parameter-names</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jdk8</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-guava</artifactId>
<version>2.9.8</version>
</dependency>
<!--由于有springboot,所以不用添加lombok版本(由springboot自动管理)-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
</project>
1.添加实体类 pojo
Item
package cn.tedu.sp01.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Item {
private Integer id;
private String name;
private Integer number;
}
User
package cn.tedu.sp01.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
Order
package cn.tedu.sp01.pojo;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
private String id; //20210812USE745678YUGHJ9485Y
private User user; //用户
private List<Item> items; //订单的商品列表
}
2.业务接口 service
ItemService
package cn.tedu.sp01.server;
import cn.tedu.sp01.pojo.Item;
import java.util.List;
public interface ItemService {
//获取订单中的商品列表
List<Item> getItems(String orderId);
//减少商品库存
void decreaseNumbers(List<Item> list);
}
UserService
package cn.tedu.sp01.server;
import cn.tedu.sp01.pojo.User;
public interface UserService {
//获取用户
User getUser(Integer id);
//增加积分
void addScore(Integer id, Integer score);
}
OrderService
package cn.tedu.sp01.server;
import cn.tedu.sp01.pojo.Order;
public interface OrderService {
//获取订单
Order getOrder(String orderId);
//添加订单
void addOrder(Order order);
}
3.工具类 util
CookieUtil
package cn.tedu.web.util;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieUtil {
/**
* @param response
* @param name
* @param value
* @param maxAge
*/
public static void setCookie(HttpServletResponse response,
String name, String value, String domain, String path, int maxAge) {
Cookie cookie = new Cookie(name, value);
if(domain != null) {
cookie.setDomain(domain);
}
cookie.setPath(path);
cookie.setMaxAge(maxAge);
response.addCookie(cookie);
}
public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) {
setCookie(response, name, value, null, "/", maxAge);
}
public static void setCookie(HttpServletResponse response, String name, String value) {
setCookie(response, name, value, null, "/", 3600);
}
public static void setCookie(HttpServletResponse response, String name) {
setCookie(response, name, "", null, "/", 3600);
}
/**
* @param request
* @param name
* @return
*/
public static String getCookie(HttpServletRequest request, String name) {
String value = null;
Cookie[] cookies = request.getCookies();
if (null != cookies) {
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name)) {
value = cookie.getValue();
}
}
}
return value;
}
/**
* @param response
* @param name
* @return
*/
public static void removeCookie(HttpServletResponse response, String name, String domain, String path) {
setCookie(response, name, "", domain, path, 0);
}
}
JsonUtil
package cn.tedu.web.util;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class JsonUtil {
private static ObjectMapper mapper;
private static JsonInclude.Include DEFAULT_PROPERTY_INCLUSION = JsonInclude.Include.NON_DEFAULT;
private static boolean IS_ENABLE_INDENT_OUTPUT = false;
private static String CSV_DEFAULT_COLUMN_SEPARATOR = ",";
static {
try {
initMapper();
configPropertyInclusion();
configIndentOutput();
configCommon();
} catch (Exception e) {
log.error("jackson config error", e);
}
}
private static void initMapper() {
mapper = new ObjectMapper();
}
private static void configCommon() {
config(mapper);
}
private static void configPropertyInclusion() {
mapper.setSerializationInclusion(DEFAULT_PROPERTY_INCLUSION);
}
private static void configIndentOutput() {
mapper.configure(SerializationFeature.INDENT_OUTPUT, IS_ENABLE_INDENT_OUTPUT);
}
private static void config(ObjectMapper objectMapper) {
objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
objectMapper.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
objectMapper.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
objectMapper.registerModule(new ParameterNamesModule());
objectMapper.registerModule(new Jdk8Module());
objectMapper.registerModule(new JavaTimeModule());
objectMapper.registerModule(new GuavaModule());
}
public static void setSerializationInclusion(JsonInclude.Include inclusion) {
DEFAULT_PROPERTY_INCLUSION = inclusion;
configPropertyInclusion();
}
public static void setIndentOutput(boolean isEnable) {
IS_ENABLE_INDENT_OUTPUT = isEnable;
configIndentOutput();
}
public static <V> V from(URL url, Class<V> c) {
try {
return mapper.readValue(url, c);
} catch (IOException e) {
log.error("jackson from error, url: {}, type: {}", url.getPath(), c, e);
return null;
}
}
public static <V> V from(InputStream inputStream, Class<V> c) {
try {
return mapper.readValue(inputStream, c);
} catch (IOException e) {
log.error("jackson from error, type: {}", c, e);
return null;
}
}
public static <V> V from(File file, Class<V> c) {
try {
return mapper.readValue(file, c);
} catch (IOException e) {
log.error("jackson from error, file path: {}, type: {}", file.getPath(), c, e);
return null;
}
}
public static <V> V from(Object jsonObj, Class<V> c) {
try {
return mapper.readValue(jsonObj.toString(), c);
} catch (IOException e) {
log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), c, e);
return null;
}
}
public static <V> V from(String json, Class<V> c) {
try {
return mapper.readValue(json, c);
} catch (IOException e) {
log.error("jackson from error, json: {}, type: {}", json, c, e);
return null;
}
}
public static <V> V from(URL url, TypeReference<V> type) {
try {
return mapper.readValue(url, type);
} catch (IOException e) {
log.error("jackson from error, url: {}, type: {}", url.getPath(), type, e);
return null;
}
}
public static <V> V from(InputStream inputStream, TypeReference<V> type) {
try {
return mapper.readValue(inputStream, type);
} catch (IOException e) {
log.error("jackson from error, type: {}", type, e);
return null;
}
}
public static <V> V from(File file, TypeReference<V> type) {
try {
return mapper.readValue(file, type);
} catch (IOException e) {
log.error("jackson from error, file path: {}, type: {}", file.getPath(), type, e);
return null;
}
}
public static <V> V from(Object jsonObj, TypeReference<V> type) {
try {
return mapper.readValue(jsonObj.toString(), type);
} catch (IOException e) {
log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), type, e);
return null;
}
}
public static <V> V from(String json, TypeReference<V> type) {
try {
return mapper.readValue(json, type);
} catch (IOException e) {
log.error("jackson from error, json: {}, type: {}", json, type, e);
return null;
}
}
public static <V> String to(List<V> list) {
try {
return mapper.writeValueAsString(list);
} catch (JsonProcessingException e) {
log.error("jackson to error, obj: {}", list, e);
return null;
}
}
public static <V> String to(V v) {
try {
return mapper.writeValueAsString(v);
} catch (JsonProcessingException e) {
log.error("jackson to error, obj: {}", v, e);
return null;
}
}
public static <V> void toFile(String path, List<V> list) {
try (Writer writer = new FileWriter(new File(path), true)) {
mapper.writer().writeValues(writer).writeAll(list);
writer.flush();
} catch (Exception e) {
log.error("jackson to file error, path: {}, list: {}", path, list, e);
}
}
public static <V> void toFile(String path, V v) {
try (Writer writer = new FileWriter(new File(path), true)) {
mapper.writer().writeValues(writer).write(v);
writer.flush();
} catch (Exception e) {
log.error("jackson to file error, path: {}, obj: {}", path, v, e);
}
}
public static String getString(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).toString();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get string error, json: {}, key: {}", json, key, e);
return null;
}
}
public static Integer getInt(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).intValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get int error, json: {}, key: {}", json, key, e);
return null;
}
}
public static Long getLong(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).longValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get long error, json: {}, key: {}", json, key, e);
return null;
}
}
public static Double getDouble(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).doubleValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get double error, json: {}, key: {}", json, key, e);
return null;
}
}
public static BigInteger getBigInteger(String json, String key) {
if (StringUtils.isEmpty(json)) {
return new BigInteger(String.valueOf(0.00));
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).bigIntegerValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get biginteger error, json: {}, key: {}", json, key, e);
return null;
}
}
public static BigDecimal getBigDecimal(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).decimalValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get bigdecimal error, json: {}, key: {}", json, key, e);
return null;
}
}
public static boolean getBoolean(String json, String key) {
if (StringUtils.isEmpty(json)) {
return false;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).booleanValue();
} else {
return false;
}
} catch (IOException e) {
log.error("jackson get boolean error, json: {}, key: {}", json, key, e);
return false;
}
}
public static byte[] getByte(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
try {
JsonNode node = mapper.readTree(json);
if (null != node) {
return node.get(key).binaryValue();
} else {
return null;
}
} catch (IOException e) {
log.error("jackson get byte error, json: {}, key: {}", json, key, e);
return null;
}
}
public static <T> ArrayList<T> getList(String json, String key) {
if (StringUtils.isEmpty(json)) {
return null;
}
String string = getString(json, key);
return from(string, new TypeReference<ArrayList<T>>() {});
}
public static <T> String add(String json, String key, T value) {
try {
JsonNode node = mapper.readTree(json);
add(node, key, value);
return node.toString();
} catch (IOException e) {
log.error("jackson add error, json: {}, key: {}, value: {}", json, key, value, e);
return json;
}
}
private static <T> void add(JsonNode jsonNode, String key, T value) {
if (value instanceof String) {
((ObjectNode) jsonNode).put(key, (String) value);
} else if (value instanceof Short) {
((ObjectNode) jsonNode).put(key, (Short) value);
} else if (value instanceof Integer) {
((ObjectNode) jsonNode).put(key, (Integer) value);
} else if (value instanceof Long) {
((ObjectNode) jsonNode).put(key, (Long) value);
} else if (value instanceof Float) {
((ObjectNode) jsonNode).put(key, (Float) value);
} else if (value instanceof Double) {
((ObjectNode) jsonNode).put(key, (Double) value);
} else if (value instanceof BigDecimal) {
((ObjectNode) jsonNode).put(key, (BigDecimal) value);
} else if (value instanceof BigInteger) {
((ObjectNode) jsonNode).put(key, (BigInteger) value);
} else if (value instanceof Boolean) {
((ObjectNode) jsonNode).put(key, (Boolean) value);
} else if (value instanceof byte[]) {
((ObjectNode) jsonNode).put(key, (byte[]) value);
} else {
((ObjectNode) jsonNode).put(key, to(value));
}
}
public static String remove(String json, String key) {
try {
JsonNode node = mapper.readTree(json);
((ObjectNode) node).remove(key);
return node.toString();
} catch (IOException e) {
log.error("jackson remove error, json: {}, key: {}", json, key, e);
return json;
}
}
public static <T> String update(String json, String key, T value) {
try {
JsonNode node = mapper.readTree(json);
((ObjectNode) node).remove(key);
add(node, key, value);
return node.toString();
} catch (IOException e) {
log.error("jackson update error, json: {}, key: {}, value: {}", json, key, value, e);
return json;
}
}
public static String format(String json) {
try {
JsonNode node = mapper.readTree(json);
return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);
} catch (IOException e) {
log.error("jackson format json error, json: {}", json, e);
return json;
}
}
public static boolean isJson(String json) {
try {
mapper.readTree(json);
return true;
} catch (Exception e) {
log.error("jackson check json error, json: {}", json, e);
return false;
}
}
private static InputStream getResourceStream(String name) {
return JsonUtil.class.getClassLoader().getResourceAsStream(name);
}
private static InputStreamReader getResourceReader(InputStream inputStream) {
if (null == inputStream) {
return null;
}
return new InputStreamReader(inputStream, StandardCharsets.UTF_8);
}
}
JsonResult
package cn.tedu.web.util;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class JsonResult<T> {
/** 成功 */
public static final int SUCCESS = 200;
/** 没有登录 */
public static final int NOT_LOGIN = 400;
/** 发生异常 */
public static final int EXCEPTION = 401;
/** 系统错误 */
public static final int SYS_ERROR = 402;
/** 参数错误 */
public static final int PARAMS_ERROR = 403;
/** 不支持或已经废弃 */
public static final int NOT_SUPPORTED = 410;
/** AuthCode错误 */
public static final int INVALID_AUTHCODE = 444;
/** 太频繁的调用 */
public static final int TOO_FREQUENT = 445;
/** 未知的错误 */
public static final int UNKNOWN_ERROR = 499;
private int code;
private String msg;
private T data;
public static JsonResult build() {
return new JsonResult();
}
public static JsonResult build(int code) {
return new JsonResult().code(code);
}
public static JsonResult build(int code, String msg) {
return new JsonResult<String>().code(code).msg(msg);
}
public static <T> JsonResult<T> build(int code, T data) {
return new JsonResult<T>().code(code).data(data);
}
public static <T> JsonResult<T> build(int code, String msg, T data) {
return new JsonResult<T>().code(code).msg(msg).data(data);
}
public JsonResult<T> code(int code) {
this.code = code;
return this;
}
public JsonResult<T> msg(String msg) {
this.msg = msg;
return this;
}
public JsonResult<T> data(T data) {
this.data = data;
return this;
}
public static JsonResult ok() {
return build(SUCCESS);
}
public static JsonResult ok(String msg) {
return build(SUCCESS, msg);
}
public static <T> JsonResult<T> ok(T data) {
return build(SUCCESS, data);
}
public static JsonResult err() {
return build(EXCEPTION);
}
public static JsonResult err(String msg) {
return build(EXCEPTION, msg);
}
@Override
public String toString() {
return JsonUtil.to(this);
}
}
三、item service 商品服务
1.新建 spring boot 起步项目
选择依赖项
- 只选择 web
配置依赖 pom.xml
-
注意要填加 sp01-commons 项目依赖
-
注意 parent 标签中的内容需要改成父工程的
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sp02-itemservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp02-itemservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--这个地方输入sp01就可以自动出来下面这个标签-->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 application.yml
# 向注册中心注册的名字
spring:
application:
name: item-service
# item 8001
# user 8101
# order 8201
server:
port: 8001
配置主程序
- 默认代码,不需要修改
package cn.tedu.sp02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Sp02ItemserviceApplication {
public static void main(String[] args) {
SpringApplication.run(Sp02ItemserviceApplication.class, args);
}
}
2.项目结构
3.编写代码
1.ItemServiceImpl
package cn.tedu.sp02.service;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.server.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class ItemServiceImpl implements ItemService {
@Override
public List<Item> getItems(String orderId) {
log.info("获取商品列表,orderId="+orderId);
// Demo 数据
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(1,"商品1",1));
items.add(new Item(2,"商品2",4));
items.add(new Item(3,"商品3",1));
items.add(new Item(4,"商品4",2));
items.add(new Item(5,"商品5",6));
return items;
}
@Override
public void decreaseNumbers(List<Item> items) {
for (Item item : items) {
log.info("减少库存:"+item);
}
}
}
2.ItemController
-
如果存在 jackson-dataformat-xml 依赖,会根据请求协议头,可能返回 xml 或 json;可以强制返回 json:
produces=MediaType.APPLICATION_JSON_UTF8_VALUE
package cn.tedu.sp02.controller;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.server.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Slf4j
@RestController
public class ItemController {
@Autowired
private ItemService itemService;
@Value("${server.port}")
private int port;
@GetMapping("/{orderId}")
public JsonResult<List<Item>> getItems(@PathVariable String orderId) {
log.info("server.port="+port+", orderId="+orderId);
List<Item> items = itemService.getItems(orderId);
return JsonResult.ok().msg("查询成功").data(items);
}
//@RequestBody 完整接收请求协议体数据
@PostMapping("/decreaseNumber")
public JsonResult<?> decreaseNumber(@RequestBody List<Item> items) {
itemService.decreaseNumbers(items);
return JsonResult.ok().msg("减少库存成功");
}
@GetMapping("/favicon.ico")
public void ico(){
}
}
4.访问测试
根据orderid,查询商品
http://localhost:8001/35
减少商品库存
http://localhost:8001/decreaseNumber
使用postman,POST发送以下格式数据:
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]
四、user service 用户服务
1.新建 spring boot 起步项目
选择依赖项
- 只选择 web
配置依赖 pom.xml
-
注意要填加 sp01-commons 项目依赖
-
注意 parent 标签中的内容需要改成父工程的
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp03-userservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp03-userservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--这个地方输入sp01就可以自动出来下面这个标签-->
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 application.yml
spring:
application:
name: user-service
# item 8001
# user 8101
# order 8201
server:
port: 8101
sp:
user-service:
users: "[{\"id\":7, \"username\":\"abc\",\"password\":\"123\"},
{\"id\":8, \"username\":\"def\",\"password\":\"456\"},
{\"id\":9, \"username\":\"ghi\",\"password\":\"789\"}]"
配置主程序
- 默认代码,不需要修改
package cn.tedu.sp03;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Sp03UserserviceApplication {
public static void main(String[] args) {
SpringApplication.run(Sp03UserserviceApplication.class, args);
}
}
2.项目结构
3.编写代码
1.UserServiceImpl
package cn.tedu.sp03.service;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.UserService;
import cn.tedu.web.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.List;
@Slf4j
@Service
public class UserServiceImpl implements UserService {
//注入 yml中配置的 demo 数据
@Value("${sp.user-service.users}")
private String userJson;
@Override
public User getUser(Integer id) {
log.info("获取用户,id = "+id);
//TypeReference 利用匿名内部类继承语法,写泛型类型:List<User>
//userJson --> List<User>
List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {});
for (User u : list) {
if (u.getId().equals(id)) {
return u;
}
}
//不是7,8,9用户,返回一个写死的用户数据
return new User(id, "用户名:"+id, "密码:"+id);
}
@Override
public void addScore(Integer id, Integer score) {
//TODO 这里增加积分
log.info("增加用户积分,id = "+id+",score = "+score);
}
}
2.UserController
-
如果存在 jackson-dataformat-xml 依赖,会根据请求协议头,可能返回 xml 或 json;可以强制返回 json:
produces=MediaType.APPLICATION_JSON_UTF8_VALUE
package cn.tedu.sp03.controller;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.UserService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/{userId}")
public JsonResult<User> getUser(@PathVariable Integer userId) {
log.info("get user, userId="+userId);
User user = userService.getUser(userId);
return JsonResult.ok().msg("查询成功").data(user);
}
//http://localhost:8101/8/score?score=1000
@GetMapping("/{userId}/score")
public JsonResult addScore(
@PathVariable Integer userId,
Integer score) {
userService.addScore(userId, score);
return JsonResult.ok().msg("增加积分成功");
}
@GetMapping("/favicon.ico")
public void ico(){
}
}
4.访问测试
根据userid查询用户信息
http://localhost:8101/7
根据userid,为用户增加积分
http://localhost:8101/7/score?score=100
五、order service 订单服务
1.新建 spring boot 起步项目
选择依赖项
-
只选择 web
配置依赖 pom.xml
-
注意要填加 sp01-commons 项目依赖
-
注意 parent 标签中的内容需要改成父工程的
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp04-orderservice</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp04-orderservice</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 application.yml
spring:
application:
name: order-service
# 8001 8101 8201
server:
port: 8201
配置主程序
- 默认代码,不需要修改
package cn.tedu.sp04;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Sp04OrderserviceApplication {
public static void main(String[] args) {
SpringApplication.run(Sp04OrderserviceApplication.class, args);
}
}
2.项目结构
3.编写代码
1.OrderServiceImpl
package cn.tedu.sp04.service;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.server.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
@Override
public Order getOrder(String orderId) {
log.info("获取订单,orderId = "+orderId);
//TODO: 远程调用user-service,获取用户信息
//TODO: 调用item-service,获取商品信息
Order order = new Order();
order.setId(orderId);
// order.setItems(用户);
// order.setItems(商品列表);
return order;
}
@Override
public void addOrder(Order order) {
log.info("保存订单:"+order);
//TODO: 调用item-service,减少商品库存
//TODO: 调用user-service,增加用户积分
}
}
2.OrderController
package cn.tedu.sp04.controller;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.OrderService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@Slf4j
@RestController
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/{orderId}")
public JsonResult<Order> getOrder(@PathVariable String orderId) {
log.info("get order, id="+orderId);
Order order = orderService.getOrder(orderId);
return JsonResult.ok().data(order);
}
@GetMapping("/add")
public JsonResult<?> addOrder() {
//模拟post提交的数据
Order order = new Order();
order.setId("123abc");
order.setUser(new User(8,null,null));
order.setItems(
Arrays.asList(
new Item[] {
new Item(1,"aaa",2),
new Item(2,"bbb",1),
new Item(3,"ccc",3),
new Item(4,"ddd",1),
new Item(5,"eee",5),
}
)
);
orderService.addOrder(order);
return JsonResult.ok().msg("保存订单成功");
}
}
4.访问测试
根据orderid,获取订单
http://localhost:8201/123abc
保存订单,观察控制台日志输出
http://localhost:8201/add
六、eureka 注册与发现
Eureka四条运行机制:
1.客户端并启动时,会反复连接注册中心尝试注册,直到注册成功为止
2.客户端每30秒发送一次心跳数据,服务器连续3次收不到一个服务心跳,会删除它的注册信息
3.客户端每30秒拉去一次注册表,书信本次注册表缓存
4.自我保护模式
由于网络中断,15分钟内,85%服务器出现心跳异常,自动进入保护模式,自我保护模式下所有的注册信息都不删除
网络恢复后,自动退出保护模式
开发调式期间,可以关闭保护模式,避免影响调试
- 创建eureka项目
- 配置依赖 pom.xml
- 配置 application.yml
- 主程序启用 eureka 服务器
- 启动,访问测试
1.创建 eureka server 项目:sp05-eureka
新建 spring boot 起步项目
选择依赖项
- 只选择 eureka
-
eureka 依赖中已经包含了 ribbon
配置依赖 pom.xml
-
注意 parent 标签中的内容需要改成父工程的
-
注意删掉springcloud版本号和dependencyManagement中的内容
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp05-eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp05-eureka</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
删除如下:
application.yml
spring:
application:
name: eureka-server
server:
port: 2001
eureka:
server:
enable-self-preservation: false # 关闭自我保护模式
instance:
hostname: eureka1 # 主机名
client:
register-with-eureka: false # 针对单台服务器,不向自己注册,
fetch-registry: false # 针对单台服务器,不从自己拉取
配置主程序
2.修改 hosts 文件,添加 eureka 域名映射
C:\Windows\System32\drivers\etc\hosts
添加内容:
127.0.0.1 eureka1
127.0.0.1 eureka2
3.启动,并访问测试
http://eureka1:2001
七、service provider 服务提供者
-
修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
1.pom.xml 添加eureka依赖
2.application.yml 添加eureka注册配置
3.主程序启用eureka客户端
4.启动服务,在eureka中查看注册信息
1.pom.xml(sp05-eureka) 添加 eureka 客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2.application.yml 添加 eureka注册配置
编码格式设置
eureka:
client:
service-url:
# 可以从云服务商购买不同地点的eureka服务器
# 自己的服务器只能写 defaultZone
defaultZone: http://eureka1:2001/eureka
-
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒
-
defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示eureka 服务器的部署位置
-
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
3.主程序启用服务注册发现客户端
修改 item-service、user-service 和 order-service,
主程序添加
@EnableDiscoveryClient
注解
4.启动,并访问 eureka 查看注册信息
-
http://eureka1:2001
八、eureka 和 “服务提供者”的高可用
1.eureka 高可用
1.application.yml
application-eureka1.yml
# application-eureka1.yml
eureka:
instance:
hostname: eureka1
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka2:2002/eureka
application-eureka2.yml
# application-eureka2.yml
eureka:
instance:
hostname: eureka2
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka1:2001/eureka
2.STS 配置启动参数 –spring.profiles.active
- eureka1 启动参数:
--spring.profiles.active=eureka1 --server.port=2001
命令行运行时添加参数:
java -jar xxx.jar --spring.profiles.active=eureka1
3.访问 eureka 服务器,查看注册信息
http://eureka1:2001/
http://eureka2:2002/
4.eureka客户端注册时,向两个服务器注册
修改以下微服务
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
当一个 eureka 服务宕机时,仍可以连接另一个 eureka 服务
2.item-service 高可用
1.application.yml
spring:
application:
name: item-service
#server:
# port: 8001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
---
spring:
profiles: item1
server:
port: 8001
---
spring:
profiles: item2
server:
port: 8002
2.配置启动参数
- item-service-8001
--spring.profiles.active=item1
- item-service-8002
--spring.profiles.active=item2
3.启动测试
- 访问 eureka 查看 item-service 注册信息
- 访问两个端口测试
http://localhost:8001/35
http://localhost:8002/35
九、ribbon
ribbon 提供了负载均衡和重试功能
Feign集成Ribbon负载均衡和重试
- Feign集成Ribbon,默认实现负载均衡和重试
Ribbon的重试
远程调用失败,可以自动发起重试调用
- 异常
- 服务器宕机
- 后台服务阻塞超时
重试参数:
- MaxAutoRetries – 单台服务器的重试次数,模式0
- MaxAutoRetriesNextServer – 更换服务器的次数,默认1
- Readatimeout – 等待响应的超时时间,默认1000毫秒
- OkToRetryOnAllOperations – 是否对所有的类型都重试,默认只对GET请求重试
- ConnectTimeout – 与后台服务器建立连接的等待超时时间,默认1000毫秒
1.在sp04的pom文件中添加
2.在启动类添加下面注解
测试:
http://eureka1:2001
http://localhost:8201/y45t33
十、zuul API网关
zuul API 网关,为微服务应用提供统一的对外访问接口。
zuul 还提供过滤器,对所有微服务提供统一的请求权限校验。
继承Ribbon负载均衡和重试
集成Hystrix容错和限流
1.新建spring boot项目
zuul API 网关,为微服务应用提供统一的对外访问接口
配置依赖 pom.xml
-
注意要填加 sp01-commons 项目依赖
-
注意 parent 标签中的内容需要改成父工程的
-
添加zuul依赖
-
删除 spring-cloud.version
-
删除 dependencyManagement
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp06-zuul</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp06-zuul</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置 application.yml
-
zuul 路由配置可以省略,缺省以服务 id 作为访问路径
spring:
application:
name: zuul
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
配置主程序
- 添加 @EnableZuulProxy 注解
package cn.tedu.sp06zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableZuulProxy
@EnableDiscoveryClient
@SpringBootApplication
public class Sp06ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(Sp06ZuulApplication.class, args);
}
}
启动服务,访问测试
-
http://eureka1:2001
-
http://localhost:3001/item-service/35
-
http://localhost:3001/item-service/decreaseNumber
使用postman,POST发送以下格式数据:
[{“id”:1, “name”:“abc”, “number”:23},{“id”:2, “name”:“def”, “number”:11}] -
http://localhost:3001/user-service/7
-
http://localhost:3001/user-service/7/score?score=100
-
http://localhost:3001/order-service/123abc
-
http://localhost:3001/order-service/
2.zuul 请求过滤
1.定义过滤器,继承 ZuulFilter
在 sp06-zuul 项目中新建过滤器类
package cn.tedu.sp06zuul.filter;
import cn.tedu.web.util.JsonResult;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import lombok.ToString;
import org.apache.commons.lang.StringUtils;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
@Component
public class AccessFilter extends ZuulFilter {
// 过滤器的类型:pre、route、post、error
/**添加的过滤器的类型*/
@Override
public String filterType() {
// return "pre";
return FilterConstants.PRE_TYPE;
}
/**添加的过滤器的顺序号*/
@Override
public int filterOrder() {
return 6;//第6个,前面还有5个,默认加到末尾
}
/**针对当前请求进行判断,是否要执行下面的过滤代码*/
@Override
public boolean shouldFilter() {
// 调用后台商品服务需要检查权限
// 调用用户额订单可以直接访问
// 获得 RequestContext 对象
// 从上下文对象获取范文后台服务id
// 判断服务id是否是 "item-service"
RequestContext ctx = RequestContext.getCurrentContext();
String serviceId = (String) ctx.get(FilterConstants.SERVICE_ID_KEY);//相当于 "serviceId"
return "item-service".equals(serviceId);
}
/**过滤代码,检查用户权限*/
@Override
public Object run() throws ZuulException {
// http://localhost:3001/item-service/t45t4?token=4324huug
// 获取请求上下文对象
// 从上下文对象获取 request 对象
// 接收 token 参数
// 若没有 token,阻止继续调用,直接返回响应
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String token = request.getParameter("token");
if(StringUtils.isBlank(token)){
//阻止继续调用
ctx.setSendZuulResponse(false);
//直接返回响应
//JsonResult -- {code:400, msg:未登录, data:null}
String json = JsonResult.err().code(400).msg("Not Login,未登录").toString();
ctx.addZuulResponseHeader("Content-Type","application/json;charset=UTF-8");
ctx.setResponseBody(json);
}
// zuul当前版本中,这个返回值不起任何作用
return null;
}
}
2.访问测试
没有token参数不允许访问
http://localhost:3001/item-service/35
有token参数可以访问
http://localhost:3001/item-service/35?token=1234
3.Zuul + Ribbon
zuul + ribbon 负载均衡
zuul 已经集成了 ribbon,默认
已经实现了负载均衡
zuul + ribbon 重试
1.pom.xml 添加 spring-retry 依赖
-
需要 spring-retry 依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
2.配置 zuul 开启重试,并配置 ribbon 重试参数
-
需要开启重试,默认不开启
spring:
application:
name: zuul
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
retryable: true
#对所有服务的通用配置
ribbon:
MaxAutoRetries: 1
# 针对一个服务单独配置重试参数
item-service:
ribbon:
MaxAutoRetries: 0
-
OkToRetryOnAllOperations=true
对连接超时、读取超时都进行重试
-
MaxAutoRetriesNextServer
更换实例的次数
-
MaxAutoRetries
当前实例重试次数,尝试失败会更换下一个实例
3.Zull + Hystrix
Zull + Hystrix 降级
创建降级类
-
getRoute() 方法中指定应用此降级类的服务id,星号或null值可以通配所有服务
ItemFB
– 对商品服务降级
package cn.tedu.sp06zuul.fb;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ItemFB implements FallbackProvider {
/**
* 设置 调用哪个后台服务,会应用当前降级类
* "item-service" 表示只针对商品降级
* "*" 表示对所有服务都降级
* "null" 表示对所有服务都降级
* @return
*/
@Override
public String getRoute() {
return "item-service";//表示只针对商品降级
}
/**发回给客户端的降级响应*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse(){
@Override
public HttpHeaders getHeaders() {
HttpHeaders h = new HttpHeaders();
h.add("Content-Type","application/json;charset=UTF-8");
return h;
}
@Override
public InputStream getBody() throws IOException {
//JsonResult - {code:500,msg:调用商品失败,dara:null}
String json = JsonResult.err().code(500).msg("调用商品失败").toString();
return new ByteArrayInputStream(json.getBytes("UTF-8"));//把返回的数据封装到流中
}
//封装状态码和状态文本
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
//单独返回状态码
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
//单独返回状态文本
@Override
public String getStatusText() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
}
@Override
public void close() {
//用来关闭下面方法中的流
//BAIS(ByteArrayInputStream) 流中存数组的流,不占用底层系统资源,不需要关闭
}
};
}
}
OrderFB
– 对订单服务降级
package cn.tedu.sp06zuul.fb;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class OrderFB implements FallbackProvider {
/**
* 设置 调用哪个后台服务,会应用当前降级类
* "order-service" 表示只针对订单降级
* "*" 表示对所有服务都降级
* "null" 表示对所有服务都降级
* @return
*/
@Override
public String getRoute() {
return "order-service";//表示只针对订单降级
}
/**发回给客户端的降级响应*/
@Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
return new ClientHttpResponse(){
@Override
public HttpHeaders getHeaders() {
HttpHeaders h = new HttpHeaders();
h.add("Content-Type","application/json;charset=UTF-8");
return h;
}
@Override
public InputStream getBody() throws IOException {
//JsonResult - {code:500,msg:调用订单失败,dara:null}
String json = JsonResult.err().code(500).msg("调用订单失败").toString();
return new ByteArrayInputStream(json.getBytes("UTF-8"));//把返回的数据封装到流中
}
//封装状态码和状态文本
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
//单独返回状态码
@Override
public int getRawStatusCode() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.value();
}
//单独返回状态文本
@Override
public String getStatusText() throws IOException {
return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();
}
@Override
public void close() {
//用来关闭下面方法中的流
//BAIS(ByteArrayInputStream) 流中存数组的流,不占用底层系统资源,不需要关闭
}
};
}
}
访问测试
没有token参数不允许访问
http://localhost:3001/item-service/35
有token参数可以访问
http://localhost:3001/item-service/35?token=1234
http://localhost:3001/user-service/8
http://localhost:3001/user-service/8/score?score=1000
http://localhost:3001/order-service/8o7i6u5y4t
http://localhost:3001/order-service/add
Zull + Hystrix 熔断
整个链路达到一定的阈值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。
满足第一个条件的情况下,如果请求的错误百分比大于阈值,则会打开断路器,默认为50%。
Hystrix的逻辑,先判断是否满足第一个条件,再判断第二个条件,如果两个条件都满足,则会开启断路器
断路器打开 5 秒后,会处于半开状态,会尝试转发请求,如果仍然失败,保持打开状态,如果成功,则关闭断路器
降低 hystrix 超时时间,以便测试降级
zuul + Hystrix dashboard 监控
1.新建springboot项目 sp07-hystrix-dashboard
不添加任何依赖
项目结构
2.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>cn.tedu</groupId>
<artifactId>sp07-hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp07-hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.yml文件
server:
port: 4001
hystrix:
dashboard:
proxy-stream-allow-list:
- localhost
4.启动项 添加注解 @@EnableHystrixDashboard
package cn.tedu.sp07;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication
public class Sp07HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(Sp07HystrixDashboardApplication.class, args);
}
}
暴露 hystrix.stream 监控端点
-
zuul 已经包含 actuator 依赖
在sp06-zuul中配置文件中添加下列内容management: endpoints: web: exposure: include: hystrix.stream
-
查看暴露的监控端点
http://localhost:3001/actuator
http://localhost:3001/actuator/hystrix.stream
5.开启监控
前提:
http://localhost:3001/actuator/hystrix.stream
http://localhost:3002/actuator/hystrix.stream
这两个都要有访问日志信息,若没有,则访问下面服务
访问服务
http://localhost:3001/item-service/iu56y4t3?token=46332
http://localhost:3001/user-service/8
http://localhost:3001/user-service/8/score?score=1000
http://localhost:3001/order-service/8o7i6u5y4t
http://localhost:3001/order-service/add
http://localhost:3002/item-service/iu56y4t3?token=46332
http://localhost:3002/user-service/8
http://localhost:3002/user-service/8/score?score=1000
http://localhost:3002/order-service/8o7i6u5y4t
http://localhost:3002/order-service/add
http://localhost:3001/actuator/hystrix.stream
http://localhost:3002/actuator/hystrix.stream
http://localhost:4001/hystrix
启动 sp08-hystrix-dashboard,填入 zuul 的监控端点路径,开启监控
http://localhost:4001/hystrix
填入监控端点:
http://localhost:3001/actuator/hystrix.stream
在dos命令窗口输入下面指令
ab -n 20000 -c 50 http://localhost:3001/item-service/iu56y4t3?token=uy455tg3
来模仿压力测试
zuul + turbine 聚合监控
Turbine
聚合多台服务器的日志数据,提供给仪表盘显示
1.新建模块:sp08-turbine
2.添加依赖
eureka client
turbine
3.yml配置
聚合的服务:zuul,a,b,c 等服务
为聚合之后的日志数据命名:new String("default")
4.启动类注解:@EnableTurbine
合并日志地址:http://localhost:5001/turbine.stream
1.新建springboot项目 sp08-turbine
2.pom.xml文件
添加下面依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
</dependency>
3.yml文件
spring:
application:
name: turbine
# 2001 eureka
# 3001 zuul
# 4001 hystrix dashboard
server:
port: 5001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
turbine:
app-config: zuul
cluster-name-expression: new String("default")
4.启动类+注解 @EnableTurbine
package cn.tedu.sp08;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@EnableTurbine
@SpringBootApplication
public class Sp08TurbineApplication {
public static void main(String[] args) {
SpringApplication.run(Sp08TurbineApplication.class, args);
}
}
5.开启监控
前提:
http://localhost:3001/actuator/hystrix.stream
http://localhost:3002/actuator/hystrix.stream
这两个都要有访问日志信息,若没有,则访问下面服务
访问服务
http://localhost:3001/item-service/iu56y4t3?token=46332
http://localhost:3001/user-service/8
http://localhost:3001/user-service/8/score?score=1000
http://localhost:3001/order-service/8o7i6u5y4t
http://localhost:3001/order-service/add
http://localhost:3002/item-service/iu56y4t3?token=46332
http://localhost:3002/user-service/8
http://localhost:3002/user-service/8/score?score=1000
http://localhost:3002/order-service/8o7i6u5y4t
http://localhost:3002/order-service/add
下面的是turbine的日志访问
http://localhost:5001/turbine.stream
http://localhost:4001/hystrix
在dos命令窗口输入下面指令
ab -n 20000 -c 50 http://localhost:3001/item-service/iu56y4t3?token=uy455tg3
来模仿压力测试
十一、config 配置中心
yml 配置文件保存到 git 服务器,例如 github.com 或 gitee.com
微服务启动时,从服务器获取配置文件
1.github 上存放配置文件
在springcloud1项目下新建文件夹,命名为 config
将sp02,sp03,sp04三个项目的yml配置文件,复制到config项目,并改名
- item-service-dev.yml
- user-service-dev.yml
- order-service-dev.yml
在复制到config中的三个yml文件中填入下列内容
将 config 项目上传到 github
commit提交
创建仓库
push推送
查看远程仓库
2.config 服务器
1.新建模块:sp-config
2.添加依赖
- eureka client
- config server
3.yml文件
- 仓库的地址
- 存放配置文件夹路径
- 之后测试,如果有问题,果断换仓库
4.启动类注解:@EnableConfigServer
1.新建springboot项目 sp09-config
2.pom.xml
配置依赖 pom.xml
-
注意 parent 标签中的内容需要改成父工程的
-
删除 spring-cloud.version
-
删除 dependencyManagement
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud1</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>sp09-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp09-config</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3.yml文件
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/fish-river/springcloud1.git # https://gitee.com/用户名/仓库
search-paths: /config #/子目录/子目录
# eureka2001 zuul3001 dashboard4001 turbine5001
server:
port: 6001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
4.启动类 + 注解 @EnableConfigServer
package cn.tedu.sp09config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@EnableConfigServer
@SpringBootApplication
public class Sp09ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(Sp09ConfigApplication.class, args);
}
}
5.启动,访问测试
确认配置中心是否正确
-
http://eureka1:2001/
检查是否有
config-server
的注册信息 -
访问配置中心的配置文件
1
http://localhost:6001/item-service/dev
2
http://localhost:6001/user-service/dev
3
http://localhost:6001/order-service/dev
3.config 客户端
1.把2,3,4的配置文件全部注释掉
2.添加依赖:config cline
3.新建配置文件:bootstrap.yml
4.添加配置:
- eureka地址
- 指定配置中心的服务id:CONFIG-SERVER
- 指定下载配置文件和profile
5.
1.修改以下项目,从配置中心获取配置信息
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
pom.xml 添加 config 客户端依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
在三个项目中添加 bootstrap.yml
bootstrap.yml,引导配置文件,先于 application.yml 加载
item-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: item-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
user-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: user-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
order-service
spring:
cloud:
config:
discovery:
enabled: true
service-id: config-server
name: order-service
profile: dev
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka
2.启动服务,观察从配置中心获取配置信息的日志
4.配置刷新
十二、config bus + rabbitmq 消息总线配置刷新
post 请求消息总线刷新端点,服务器会向 rabbitmq 发布刷新消息,接收到消息的微服务会向配置服务器请求刷新配置信息
Rabbitmq
消息队列、消息服务、消息中间件、Broker
- Rabbitmq
- Activemq
- Rocketmq 阿里
- Kafka
- Tubemq 腾讯
1.rabbitmq 安装
搭建Rabbitmq服务器
-
克隆虚拟机”docker-base” –> rabbitmq
-
设置ip
./ip-static ip: 192.168.64.140
-
下载 rabbitmq 镜像
docker pull rabbitmq:management 或者从 code 下载 rabbit-image.gz 上传到服务器,然后执行镜像导入 docker load -i rabbit-image.gz
-
启动rabbitmq容器
关闭防火墙 systemctl stop firewalld systemctl disable firewalld 重启 docker 系统服务 systemctl restart docker mkdir /etc/rabbitmq vim /etc/rabbitmq/rabbitmq.conf # 添加两行配置: default_user = admin default_pass = admin docker run -d --name rabbit \ -p 5672:5672 \ -p 15672:15672 \ -v /etc/rabbitmq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf \ -e RABBITMQ_CONFIG_FILE=/etc/rabbitmq/rabbitmq.conf \ rabbitmq:management 访问管理控制台 http://192.168.64.140:15672 用户名密码是 admin
2.需要动态更新配置的微服务,添加 spring cloud bus 依赖,并添加 rabbitmq 连接信息
1.pom.xml 添加 spring cloud bus 依赖
修改以下微服务
-
item-service
-
user-service
-
order-service
使用 STS 编辑起步依赖,分别添加
bus
、
rabbitmq
依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-stream-binder-rabbit</artifactId>
</dependency>
2.配置文件中添加 rabbitmq 连接信息
在以下配置文件中修改:
- config中的4个配置文件
- sp09-config项目的application.yml
注意:
- 连接信息请修改成你的连接信息
- config项目需要提交
spring:
......
rabbitmq:
host: 192.168.64.140
port: 5672
username: admin
password: admin
3.config-server 暴露 bus-refresh 刷新端点
修改 sp09-config 项目的 application.yml, 暴露bus-refresh端点
management:
endpoints:
web:
exposure:
include: bus-refresh
-
查看刷新端点
http://localhost:6001/actuator
4.UserServiceImpl 添加 @RefreshScope 注解
-
只允许对添加了
@RefreshScope
或 @ConfigurationProperties 注解的 Bean
刷新配置
,可以将更新的配置数据注入到 Bean 中
-
重启配置中心, 再重启sp03, 查看暴露的刷新端点 查看暴露的刷新端点
http://localhost:6001/actuator
-
向config文件夹中的user-service-dev.yml文件添加一条10用户的数据
-
修改后提交推送到远程仓库
-
访问 user-service,查看动态更新的新用户数据
5.启动项目测试,到目前为止的全部测试
-
启动 140 rabbitmq 服务器
docker start rabbit
-
启动5,耐心等待完全启动完成
-
启动9,耐心等待完全启动完成
-
——— http://eureka1:2001 注册表中存在 config-server
http://localhost:6001/item-service/dev
http://localhost:6001/user-service/dev
http://localhost:6001/order-service/dev -
启动 2,3,4
-
——— 查看 2,3,4 的控制台,要看到连接 6001
-
启动6
-
——— http://localhost:6001/actuator
这里面要看到 bus-refresh
上面测试时到目前为止的成功测试
十三、sleuth 链路跟踪
通过sp04-orderservice访问sp02-itemservice和sp03-userservice
随着系统规模越来越大,微服务之间调用关系变得错综复杂,一条调用链路中可能调用多个微服务,任何一个微服务不可用都可能造整个调用过程失败
spring cloud sleuth 可以跟踪调用链路,分析链路中每个节点的执行情况
1.微服务中添加 spring cloud sleuth 依赖
修改以下微服务的 pom.xml,添加 sleuth 依赖
- sp02-item-service
- sp03-user-service
- sp04-order-service
分别在上面的几个服务添加添加 sleuth 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
2.在控制台查看链路跟踪日志
-
通过 zuul 网关,访问 order-service
http://localhost:3001/order-service/112233
四个微服务的控制台日志中,可以看到以下信息:
[服务id,请求id,span id,是否发送到zipkin]
-
请求id:请求到达第一个微服务时生成一个请求id,该id在调用链路中会一直向后面的微服务传递
-
span id:链路中每一步微服务调用,都生成一个新的id
[zuul,
6c24c0a7a8e7281a
,6c24c0a7a8e7281a,false]
[order-service,
6c24c0a7a8e7281a
,993f53408ab7b6e3,false]
[item-service,
6c24c0a7a8e7281a
,ce0c820204dbaae1,false]
[user-service,
6c24c0a7a8e7281a
,fdd1e177f72d667b,false]
十四、sleuth + zipkin 链路分析
zipkin 可以收集链路跟踪数据,提供可视化的链路分析
1.添加zipkin客户端
2.在06添加rabbitmq依赖
3.yml配置发送方式:rabbit
6.修改06的yml,添加rabbitmq连接配置
1.微服务添加 zipkin 起步依赖
修改以下微服务
- sp02-item-service
- sp03-user-service
- sp04-order-service
- sp06-zuul
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
在06中添加下列依赖(其实上面几个都得加,只不过其他三个都加过了,所以现在只在06中加)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
如果没有配置过 spring cloud bus,还需要添加
rabbitmq
依赖和连接信息
在6中的yml文件和config文件夹中的2,3,4中的yml文件添加如下配置
添加完之后,提交push到远程仓库中
2.zipkin 服务
1.下载 zipkin 服务器
- https://github.com/openzipkin/zipkin
2.启动 zipkin 时,连接到 rabbitmq
java -jar zipkin-server-2.23.4-exec.jar --zipkin.collector.rabbitmq.uri=amqp://admin:admin@192.168.64.140:5672
3.启动并访问服务,访问 zipkin 查看链路分析
-
http://localhost:3001/order-service/112233
-
访问 zipkin
http://localhost:9411/zipkin
十五、向eureka注册正确的ip地址
eureka客户端向eureka注册时, 会自动选择网卡, 并可能注册主机名而不是ip地址.
下面配置可以选择正确网卡的ip向eureka进行注册.
1.选择正确网卡
服务器有多块网卡,要选择正确网卡的ip地址向eureka进行注册
修改
bootstrap.yml
(若没有bootstrap.yml文件,则创建一个该文件,不能配置在application.yml文件中)
spring:
cloud:
inetutils:
ignored-interfaces: # 忽略的网卡
- VM.*
preferred-networks: # 要是用的网卡的网段
- 176\.201\.170\..+ # 自己电脑联网的网卡(正则表达式)
2.注册ip地址,而不是主机名
注册时,有可能自动选择主机名进行注册,而不使用ip地址. 主机名在局域网内有可能不会被正确的解析
最好使用ip地址进行注册,而不注册主机名
在应用配置
application.yml
中配置:
eureka:
instance:
prefer-ip-address: true # 使用ip进行注册
instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port} # 界面列表中显示的格式也显示ip