SpringCloud
一、SpringCloud介绍
1spring cloud 介绍
spring cloud 是一系列框架的集合。它利用 spring boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用 spring boot 的开发风格做到一键启动和部署。spring cloud 并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过 spring boot 风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。
spring cloud 对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发自己的分布式系统基础设施,使用 spring cloud 一站式解决方案能在从容应对业务发展的同时大大减少开发成本。同时,随着近几年微服务架构和 docker 容器概念的火爆,也会让 spring cloud 在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、一站式的技术方案,意义可能会堪比当年 servlet 规范的诞生,有效推进服务端软件系统技术水平的进步。
2spring cloud 技术组成
eureka
微服务治理,服务注册和发现
ribbon
负载均衡、请求重试
hystrix
断路器,服务降级、熔断
feign
ribbon + hystrix 集成,并提供声明式客户端
hystrix dashboard 和 turbine
hystrix 数据监控
zuul
API 网关,提供微服务的统一入口,并提供统一的权限验证
config
配置中心
bus
消息总线, 配置刷新
sleuth+zipkin
链路跟踪
3Spring Cloud 对比 Dubbo
- Dubbo
- Dubbo只是一个远程调用(RPC)框架
- 默认基于长连接,支持多种序列化格式
- Spring Cloud
- 框架集
- 提供了一整套微服务解决方案(全家桶)
- 基于http调用, Rest API
4service - 服务
- 商品服务 item service,端口 8001
- 用户服务 user service,端口 8101
- 订单服务 order service,端口 8201
二、项目创建
1新建maven父级工程项目
调整SDK为1.8
设置修改maven为自己的maven,并设置File Encodings为UTF-8
修改pom文件添加以下依赖
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>cn.tedu</groupId>
<artifactId>order-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR11</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2新建commons通用项目
选中springcloud1项目新建module选择maven工程
在此pom文件中添加以下依赖
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sp01-commons</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<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>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
</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>
在此工程的main下的java文件夹创建cn.tedu包,在此包中创建sp01包和web包,在sp01包中放pojo包和service包,在pojo包中放置pojo包用来存放实体对象,在service包中存放业务接口;在web包中放util包用来放置工具类,结构如下图
内容如下:
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; //商品数量
public static void main(String[] args) {
Item a = new Item(5, "a", 12);
a.setName("b");
System.out.println(a.getName());
}
}
Order
package cn.tedu.sp01.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
private String id; // "g3g34tg-45345-63453452523"
private User user; //订单所属的用户
private List<Item> items; //订单中包含的商品列表
}
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;
}
ItemService
package cn.tedu.sp01.service;
import cn.tedu.sp01.pojo.Item;
import java.util.List;
// 商品的业务接口
public interface ItemService {
// 根据订单id,获取商品列表
List<Item> getItems(String orderId);
// 减少商品库存
void decreaseNumber(List<Item> items);
}
OrderService
package cn.tedu.sp01.service;
import cn.tedu.sp01.pojo.Order;
public interface OrderService {
// 获取订单
Order getOrder(String id);
// 添加订单
void addOrder(Order order);
}
UserService
package cn.tedu.sp01.service;
import cn.tedu.sp01.pojo.User;
public interface UserService {
// 获取用户
User getUser(Integer id);
// 增加积分
void addScore(Integer id, Integer score);
}
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);
}
}
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);
}
}
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.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());
}
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);
}
}
3新建sp02-itemservice
选中springcloud1右键module选择springboot项目,
更改pom.xml文件,将父工程设为opder-parent,添加web依赖
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<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>
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
更改yml文件,并添加以下配置
spring:
application:
name: item-service
server:
port: 8001
在sp02中添加以下两个controller包和service包,并添加ItemController类和ItemServiceImpl类
ItemServiceImpl
package cn.tedu.sp02.service;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@Slf4j
public class ItemServiceImpl implements ItemService {
// 获取订单的商品列表
@Override
public List<Item> getItems(String orderId) {
log.info("获取商品列表,orderId="+orderId);
ArrayList<Item> items = new ArrayList<>();
items.add(new Item(1, "商品1", 1));
items.add(new Item(2, "商品2", 5));
items.add(new Item(3, "商品3", 3));
items.add(new Item(4, "商品4", 1));
items.add(new Item(5, "商品5", 6));
return items;
}
// 减少商品库存
@Override
public void decreaseNumber(List<Item> items) {
// items.for
for (Item item : items) {
log.info("减少库存: "+item);
}
}
}
ItemController
package cn.tedu.sp02.controller;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Random;
@RestController
@Slf4j
public class ItemController {
@Autowired
private ItemService itemService;
// 获取订单的商品列表
/*
JsonResult 用来封装返回给客户端的数据
- code 状态码 200
- msg 提示消息 null
- data 数据对象 List<Item>
"{"code":0, "msg":"null", "data":[{},{},{}]}"
*/
@GetMapping("/{orderId}")
public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
List<Item> items = itemService.getItems(orderId);
JsonResult r = JsonResult.ok().data(items);
// 随机的延迟代码
// 90%概率会执行延迟代码
// if (Math.random() < 0.9) {
// //随机延迟时长 0 到 5秒
// int t = new Random().nextInt(5000);
// log.info("随机延迟: "+t);
// Thread.sleep(t);
// }
return r;
}
// 减少商品库存
/*
@RequestBody
接收客户端提交的参数
完整地接收 http 协议体数据,再转成java对象
*/
@PostMapping("/decreaseNumber")
public JsonResult<?> decreaseNumber(@RequestBody List<Item> items) {
itemService.decreaseNumber(items);
return JsonResult.ok().msg("减少商品库存成功");
}
}
Spring MVC 接收参数的几个注解
启动测试
根据orderid,查询商品
http://localhost:8001/1
减少商品库存
http://localhost:8001/decreaseNumber
使用postman,POST发送以下格式数据:
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]
4新建sp03-userservice
选中springcloud1右键新建module,选择springboot项目,
修改pom文件,将parent修改位order-parent,将sp01-commons的依赖加上,内容如下
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>
<dependency>
<groupId>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
修改配置文件为yml文件,并添加以下配置内容
spring:
application:
name: user-service
#item 8001
#user 8101
#order 8201
server:
port: 8101
# 自定义配置属性,测试用的用户数据
# [{id:7,username:a,password:b}, {}, {}]
sp:
user-service:
users: "[{\"id\":7, \"username\":\"abc\",\"password\":\"123\"},
{\"id\":8, \"username\":\"def\",\"password\":\"456\"},
{\"id\":9, \"username\":\"ghi\",\"password\":\"789\"}]"
在sp03包下新建controller包,service包,包中放UserController和UserServiceImpl
UserServiceImpl
package cn.tedu.sp03.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.core.type.TypeReference;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.UserService;
import cn.tedu.web.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
@Service
@Slf4j
public class UserServiceImpl implements UserService {
@Value("${sp.user-service.users}")
private String userJson;
@Override
public User getUser(Integer id) {
log.info("users json string : "+userJson);
List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {});
for (User u : list) {
if (u.getId().equals(id)) {
return u;
}
}
return new User(id, "name-"+id, "pwd-"+id);
}
@Override
public void addScore(Integer id, Integer score) {
// 这里增加积分
log.info("user "+id+" - 增加积分 "+score);
}
}
UserController
package cn.tedu.sp03.controller;
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 cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.UserService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
@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 u = userService.getUser(userId);
return JsonResult.ok(u);
}
@GetMapping("/{userId}/score")
public JsonResult addScore(
@PathVariable Integer userId, Integer score) {
userService.addScore(userId, score);
return JsonResult.ok();
}
}
访问测试
根据userid查询用户信息
http://localhost:8101/7
根据userid,为用户增加积分
http://localhost:8101/7/score?score=100
5新建sp04-orderservice
选中springcloud1右键新建module,选择springboot项目
调整pom文件,将pom文件中的parent换成order-parent,在依赖中添加上sp01-commons的依赖,删除重复的依赖。
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
修改配置文件为yml文件,添加以下配置内容
spring:
application:
name: order-service
server:
port: 8201
在sp04包下创建controller包和service包,在包中添加OrderServiceImpl和OrderController
OrderServiceImpl
package cn.tedu.sp04.service;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
@Override
public Order getOrder(String orderId) {
//TODO: 调用user-service获取用户信息
//TODO: 调用item-service获取商品信息
Order order = new Order();
order.setId(orderId);
return order;
}
@Override
public void addOrder(Order order) {
//TODO: 调用item-service减少商品库存
//TODO: 调用user-service增加用户积分
log.info("保存订单:"+order);
}
}
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.service.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(order);
}
@GetMapping("/")
public JsonResult addOrder() {
//模拟post提交的数据
Order order = new Order();
order.setId("123abc");
order.setUser(new User(7,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();
}
}
启动测试
根据orderid,获取订单
http://localhost:8201/123abc
保存订单,观察控制台日志输出
http://localhost:8201/
6创建eureka注册发现
选中springcloud1右键新建module选择springboot项目
调整pom文件的parent为order-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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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>
<!-- <spring-cloud.version>2020.0.3</spring-cloud.version>-->
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
修改配置文件为yml文件,并添加以下配置
spring:
application:
name: eureka-server
server:
port: 2001
# 1. 禁用保护模式
# 2. 配置主机名,集群中用这个名字互相区分
# 3. 针对单台服务器,不向自己注册、不从自己拉取
eureka:
server:
enable-self-preservation: false
instance:
hostname: 127.0.0.1
client:
register-with-eureka: false
fetch-registry: false
eureka 集群服务器之间,通过 hostname 来区分
eureka.server.enable-self-preservation
eureka 的自我保护状态:心跳失败的比例,在15分钟内是否超过85%,如果出现了超过的情况,Eureka Server会将当前的实例注册信息保护起来,同时提示一个警告,一旦进入保护模式,Eureka Server将会尝试保护其服务注册表中的信息,不再删除服务注册表中的数据。也就是不会注销任何微服务
eureka.client.register-with-eureka=false
不向自身注册
eureka.client.fetch-registry=false
不从自身拉取注册信息
eureka.instance.lease-expiration-duration-in-seconds
最后一次心跳后,间隔多久认定微服务不可用,默认90
主程序
添加 @EnableEurekaServer
package cn.tedu.sp05;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class Sp05EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(Sp05EurekaApplication.class, args);
}
}
启动访问
http://127.0.0.1:2001/
7service provider 服务提供者
修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
pom.xml 添加eureka依赖
application.yml 添加eureka注册配置
主程序启用eureka客户端
启动服务,在eureka中查看注册信息
(1)打开 item-service、user-service、order-service项目的pom文件,添加eureka客户端的依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
(2)打开 item-service、user-service、order-service项目的配置文件,添加eureka注册配置
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:2001/eureka
(3)重新启动 item-service、user-service、order-service项目
(4)查看eureka中查看注册信息
eureka.instance.lease-renewal-interval-in-seconds
心跳间隔时间,默认 30 秒
defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示 eureka 服务器的部署位置, 需要云服务器提供
eureka.client.registry-fetch-interval-seconds
拉取注册信息间隔时间,默认 30 秒
主程序启用服务注册发现客户端(加不加都可)
修改 item-service、user-service 和 order-service,
主程序添加 @EnableDiscoveryClient
注解
启动,并访问 eureka 查看注册信息
8eureka 和 “服务提供者”的高可用
idea使用service视图
item-service 高可用
启动参数 --server.port
可以覆盖yml中的端口配置
配置启动参数
--server.port=8001
启动8002,查看注册中心
eureka 高可用
添加两个服务器的 profile 配置文件
application-eureka1.yml
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://127.0.0.1:2002/eureka #eureka1启动时向eureka2注册
application-eureka2.yml
eureka:
instance:
hostname: 127.0.0.1
client:
register-with-eureka: true #profile的配置会覆盖公用配置
fetch-registry: true #profile的配置会覆盖公用配置
service-url:
defaultZone: http://127.0.0.1:2001/eureka #eureka1启动时向eureka2注册
eureka1启动参数
--spring.profiles.active=eureka1 --server.port=2001
eureka2启动参数
--spring.profiles.active=eureka2 --server.port=2002
如果在命令行运行,可以在命令行中添加参数:
java -jar xxx.jar --spring.profiles.active=eureka1 --server.port=2001
访问 eureka1 服务器,查看注册信息
http://localhost:2001/
访问 eureka2 服务器,查看注册信息
http://localhost:2002/
eureka客户端注册时,向两个服务器注册
修改以下微服务
- sp02-itemservice
- sp03-userservice
- sp04-orderservice
向两个注册中心注册
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:2001/eureka, http://127.0.0.1:2002/eureka
重启启动sp02-itemservice,sp03-userservice,sp04-orderservice项目,如果哪个eureka没有注册上服务直接重启即可
9feign + ribbon 负载均衡和重试
修改 04
添加 openfeign 依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
添加启动类注解 @EnableFeignClients
package cn.tedu.sp04;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableFeignClients
@SpringBootApplication
public class Sp04OrderserviceApplication {
public static void main(String[] args) {
SpringApplication.run(Sp04OrderserviceApplication.class, args);
}
}
声明式客户端接口
ItemClient
package cn.tedu.sp04.feign;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
// 调用哪个服务
// 从注册表可以得到这个服务的主机地址
@FeignClient(name = "item-service")
public interface ItemClient {
//获取订单的商品列表
// 指定调用的路径
@GetMapping("/{orderId}")
JsonResult<List<Item>> getItems(@PathVariable String orderId);
//减少商品库存
@PostMapping("/decreaseNumber")
JsonResult<?> decreaseNumber(@RequestBody List<Item> items);
}
UserClient
package cn.tedu.sp04.feign;
import cn.tedu.sp01.pojo.User;
import cn.tedu.web.util.JsonResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = "user-service")
public interface UserClient {
//获取用户
@GetMapping("/{userId}")
public JsonResult<User> getUser(@PathVariable Integer userId);
//增加用户积分
@GetMapping("/{userId}/score")
public JsonResult<?> addScore(@PathVariable Integer userId,
@RequestParam("score") Integer score);
}
修改 OrderServiceImpl,使用声明式客户端接口调用远程服务
package cn.tedu.sp04.service;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private ItemClient itemClient;
@Autowired
private UserClient userClient;
@Override
public Order getOrder(String id) {
log.info("获取订单,id="+id);
// 远程调用商品,获取商品列表
JsonResult<List<Item>> items = itemClient.getItems(id);
// 远程调用用户,获取用户
JsonResult<User> user = userClient.getUser(8);// 真实项目中应该获取已登录的用户id
Order order = new Order();
order.setId(id);
order.setUser(user.getData());
order.setItems(items.getData());
return order;
}
@Override
public void addOrder(Order order) {
log.info("添加订单,order="+order);
// 远程调用商品,减少商品库存
itemClient.decreaseNumber(order.getItems());
// 远程调用用户,增加积分
userClient.addScore(order.getUser().getId(), 1000);
}
}
10Zuul-Api网关
网关的作用
- 统一的调用入口
- 统一的权限校验
- 集成 Ribbon 负载均衡和重试
- 集成 Hystrix 系统容错和限流
选中springcloud1右键新建module,选择springboot项目
调整pom文件的parent为order-parent,添加zuul和sp01-commons依赖,pom文件内容为
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<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.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>cn.tedu</groupId>
<artifactId>sp01-commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
</dependencies>
</project>
在启动类添加@EnableZuulProxy 注解
package cn.tedu.sp06zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@EnableZuulProxy
@SpringBootApplication
public class Sp06ZuulApplication {
public static void main(String[] args) {
SpringApplication.run(Sp06ZuulApplication.class, args);
}
}
调整配置文件为yml文件,并添加以下配置内容
spring:
application:
name: zuul
server:
port: 3001
eureka:
client:
service-url:
defaultZone: http://eureka1:2001/eureka,http://eureka2:2002/eureka
# 转发规则、zuul路由规则
# **包含深层路径
# 下面的设置时默认设置,zuul 会根据注册表的注册信息进行配置,
# 最后手动配置,避免注册表不全
zuul:
routes:
item-service: /item-service/**
user-service: /user-service/**
order-service: /order-service/**
统一的权限校验
(1)定义过滤器,继承 ZuulFilter
(2)添加 @Component 注解,由 spring 自动创建实例
(3)zuul会在spring容器中找到过滤器实例,完成自动配置
(4)添加过滤类,继承ZuulFilter
package cn.tedu.sp06zuul.filter;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang3.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、routing、post、error
@Override
public String filterType() {
// return "pre";
return FilterConstants.PRE_TYPE;
}
// 位置序号
@Override
public int filterOrder() {
return 6;
}
// 判断针对当前请求,是否执行过滤代码
@Override
public boolean shouldFilter() {
/*
调用商品判断权限,
调用用户和订单,不检查权限
*/
//获得调用的服务id
RequestContext ctx = RequestContext.getCurrentContext();
String servicedId = (String) ctx.get(FilterConstants.SERVICE_ID_KEY);
return "item-service".equals(servicedId);
}
// 过滤代码
@Override
public Object run() throws ZuulException {
// http://localhost:3001/item-service/t34t34?token=t34t34t
// 获得 requst 对象
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
// 接收 token参数
String token = request.getParameter("token");
// 如果没有token,阻止继续访问,直接返回响应
if (StringUtils.isBlank(token)){
//阻止继续访问
ctx.setSendZuulResponse(false);
//直接返回响应
ctx.addZuulResponseHeader("Content-Type","text/html;charset=UTF-8");
ctx.setResponseBody("Not Login! 未登录!");
}
//这个返回值不起任何作用,返回任意都可以
return null;
}
}
启动测试登录
没有token,访问被拦截
http://localhost:3001/item-service/t34t34
有token,可以访问
http://localhost:3001/item-service/t34t34?token=t34t34t
**.zuul 集成 **
zuul 集成 ribbon 的负载均衡和重试
- 默认已经启用了负载均衡
- 默认不启用重试,避免造成后台服务压力倍增,出现大面积故障
zuul 启用ribbon重试
- 添加 spring-retry 依赖
- yml 配置启用重试: zuul.retryable=true
- 默认重试参数:
-
- ribbon.MaxAutoRetries=0
- ribbon.MaxAutoRetriesNextServer=1
- ribbon.ReadTimeout=1000
11创建hystrix dashboard
hystrix
类似 sentinel,系统的容错和限流
zuul 集成 hystrix,默认已经添加了 hystrix 依赖,并启用了 hystrix
hystrix 降级
当调用后台服务出错,可以执行当前服务中的一段代码,返回替代结果
- 实现 FallbackProvider 接口,添加降级代码
- 添加 @Component 注解,由spring自动创建实例
- zuul的自动配置会在spring容器中找到降级类的实例,自动完成配置
hystrix 熔断
- 10秒内20次请求(首先满足)
- 50%失败(异常,崩溃,超时),执行了降级代码
- 熔断后几秒,会进入**“半开”**状态,会尝试向后台服务发送一次客户端调用
如果调用成功,会自动恢复正常,关闭断路器
如果调用失败,继续保持打开状态
hystrix的仪表盘
对降级容错处理,熔断限流的情况进行监控,快速定单位错误
hystrix dashboard利用 actuator 工具来暴露 hystrix 的监控日志
新建 sp07-hystrix-dashboard
选中springcloud项目,右键新建module,选择springboot模块
调整parent为order-parent ,并且添加dashboard依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
pom文件为
<?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>order-parent</artifactId> <groupId>cn.tedu</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <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.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
调整配置文件为yml,并添加以下配置
# 仪表盘项目在系统中可以适应完全独立的项目# 可以不用连接注册中心,所以名字起不起都无所谓server: port: 4001# 允许从哪些服务器抓取日志hystrix: dashboard: proxy-stream-allow-list: localhost
在启动了中添加@EnableHystrixDashboard注解
package cn.tedu.sp07hystrixdashboard;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@EnableHystrixDashboard@SpringBootApplicationpublic class Sp07HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(Sp07HystrixDashboardApplication.class, args); }}
启动访问
http://localhost:4001/hystrix
actuator
spring 提供的一个项目日志监控工具,可以暴露系统的多种日志数据
- 健康状态
- 应用信息
- 系统环境变量、参数
- spring容器中所有的对象
- spring mvc映射的所有访问路径
- jvm虚拟机内存镜像
添加 actuator
- 添加 actuator 依赖(zuul依赖中已经包含,所以无需重新添加)
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
2.yml 配置暴露监控日志
# 暴露 actuator 所有监控日志# http://localhost:3001/actuatormanagement: endpoints: web: exposure: include: "*"
启动访问
http://localhost:3001/actuator
找到最下边的
http://localhost:3001/actuator/hystrix.stream
首先访问一下系统的路径,往后就可以看到
将此链接放到hystrix中,点击monitorStream就可以对请求链路监控,刷新系统的请求路径就可以看到仪表盘的变化
11使用 apache 的并发访问测试工具 ab
cmd运行:
访问商品,尝试压力测试
ab -n 20000 -c 50 http://localhost:3001/item-service/35?token=tyty1
可以看出仪表盘数值的变化
访问用户尝试压力测试
ab -n 20000 -c 100 http://localhost:3001/user-service/35?token=tyty1
12创建zuul网关的高并发
新建两个sp06zuul不同端口的服务
--server.port=3001
--server.port=3002
测试,两个3002网关的日志信息
13turbine
从多台服务器合并监控数据,
hystrix dashboard 可以从 turbine 抓取合并后的多台服务器数据同时展现
选中springcloud1右键新建module选择springboot项目
调整pom文件的parent为order-parent并添加turbine依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-turbine</artifactId> </dependency>
pom文件如下
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>cn.tedu</groupId>
<artifactId>sp08-turbine</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>sp08-turbine</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-turbine</artifactId>
</dependency>
</dependencies>
</project>
调整配置文件为yml文件,并添加以下配置
-
- app-config: 指定服务id,同时抓取该服务的多台服务器
- name: 给合并后的数据命名,默认名是 default
yml文件配置
# 基本配置
spring:
application:
name: turbine
server:
port: 5001
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:2001/eureka,http://127.0.0.1:2002/eureka
# turbine指定抓取的服务日志
turbine:
# 服务名,可以用逗号隔开,写多个服务
app-config: zuul
# 集群名称的表达式,默认default,不能直接写default(睡着了出现的设计问题)
cluster-name-expression: new String("default")
启动类添加注解@EnableTurbine
package cn.tedu.sp08turbine;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.turbine.EnableTurbine;@EnableTurbine@SpringBootApplicationpublic class Sp08TurbineApplication { public static void main(String[] args) { SpringApplication.run(Sp08TurbineApplication.class, args); }}
启动测试,可以同时获取zuul所有的访问路径的信息,将此路径放到hystrix中
http://localhost:5001/turbine.stream
启动压力测试
仪表盘可以看到变化
14config配置中心
之前学过Nacos配置中心,是将配置数据文件直接存储到了数据库里边
现在新学一个配置中心,spring cloud config,是将配置数据存到了git仓库,当项目启动的时候,从git仓库下载配置.
集中的管理和维护配置文件
准备本地的 git 仓库
新建文件夹 config
2,3,4项目的 application.yml 复制到 config 目录并改名为
item-service-dev.yml表示开发环境
item-service-test.yml表示测试环境
item-service-prod.yml表示生产环境
-
- item-service-dev.yml
- user-service-dev.yml
- order-service-dev.yml
在config文件夹中添加以下配置,当从配置中心下载配置时,不会覆盖本地配置
#从配置中心下载不覆盖本地配置 cloud: config: override-none: true
创建本地仓库并提交
已经创建过仓库,不要重复创建
推送远程仓库
在git新建仓库
远程仓库地址
https://gitee.com/lichao2113/springcloud.git
push到远程仓库push+ctrl+k
内容已被推送到仓库中
搭建配置中心的服务端:
新建sp09-config
选中springcloud项目,右键新建module,选择springboot项目
创建完成后,调整parent为order-parent,并添加依赖
<!--config注册中心服务端依赖--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <!--需要添加eureka注册中心客户端依赖,向eureka注册中心注册--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
pom文件内容为
<?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>order-parent</artifactId>
<groupId>cn.tedu</groupId>
<version>1.0-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>
<spring-cloud.version>2020.0.3</spring-cloud.version>
</properties>
<dependencies>
<!--config注册中心服务端依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--需要添加eureka注册中心客户端依赖,向eureka注册中心注册-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
</project>
更改配置文件为yml文件为
# 基础配置spring: application: name: config-server # 配置仓库的配置gituri/paths cloud: config: server: git: uri: https://gitee.com/lichao2113/springcloud.git # 如果仅有一层 可以只写文件夹名字,如果有多层文件夹目录, # 需要写出整个远端目录不用加仓库名 search-paths: configserver: port: 6001eureka: client: service-url: defaultZone: http://127.0.0.1:2001/eureka,http://127.0.0.1:2002/eureka
在著启动类上添加注解@EnableConfigServer
package cn.tedu.sp09config;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class Sp09ConfigApplication { public static void main(String[] args) { SpringApplication.run(Sp09ConfigApplication.class, args); }}
启动测试
可以访问以下路径返回的是yml格式的文件
http://localhost:6001/item-service-dev.yml
http://localhost:6001/user-service-dev.yml
http://localhost:6001/order-service-dev.yml
或者访问以下链接返回的是json字符串
http://localhost:6001/item-service/dev
http://localhost:6001/user-service/dev
http://localhost:6001/order-service/dev
配置中心的客户端
1.注释掉项目中sp02/sp03/sp04所有yml配置代码
2.为sp02/sp03/sp04添加config client依赖
<!--config--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
3.添加新的配置文件: bootstrap.yml
bootstrap.yml引导配置,springboot应用启动之前,要先执行的一些配置,需要放到引导配置文件中
在sp02中常见bootstrap.yml文件,并添加以下内容,需要向eureka注册,并从配置中心拉取配置,如果拉取不到配置,按照默认的配置启动。
bootstrap.yml
eureka: client: service-url: defaultZone: http://localhost:2001/eureka,http://localhost:2002/eurekaspring: cloud: config: discovery: enabled: true service-id: config-server #item-service-dev.yml name: item-service profile: dev
将此文件复制到sp03,sp04
sp03 bootstrap.yml
eureka: client: service-url: defaultZone: http://localhost:2001/eureka,http://localhost:2002/eurekaspring: cloud: config: discovery: enabled: true service-id: config-server #user-service-dev.yml name: user-service profile: dev
sp04 bootstrap.yml
eureka: client: service-url: defaultZone: http://localhost:2001/eureka,http://localhost:2002/eurekaspring: cloud: config: discovery: enabled: true service-id: config-server #order-service-dev.yml name: order-service profile: dev
4.添加配置:
- 连接eureka(因为config服务也注册到了eureka,所以需要先从注册中心获取config服务的地址)
- 指定配置中心的service-id
- 下载的配置文件 item-service dev
关闭sp02,sp03,sp04,重新启动,看到是从配置中心下载配置文件,然后启动
15添加Rabbitmq消息总线
在sp02,sp03,sp04,sp09中添加以下依赖
<!--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>
修改sp-09的yml配置,在spring下添加以下配置
#rabbitmq配置
rabbitmq:
host: 192.168.126.17
port: 5672
username: admin
password: admin
将新增的配置添加到config文件夹中的所有配置
修改好后将项目推送到git仓库中去
使用bus暴露一个/actuator/bus-refresh路径,调用路径bus会刷新配置
在sp09项目中添加actuator依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
修改sp09的yml文件,添加以下配置
management: endpoints: web: exposure: include: - bus-refresh - env
重启sp02,sp03,sp04,sp09项目
启动顺序
sp05->sp09->sp启动完之后再启动其他服务
访问http://localhost:6001/actuator
使用postman向http://localhost:6001/actuator/bus-refresh发送POST请求,即可获得配置刷新
访问过后可以看到sp02,sp03,sp04,会重新链接localhost:6001,下载配置文件,
出现一下情况说明eureka中没有注册config-server模块,解决办法,要么重启config-server,要么重启eureka
下载配置文件后不会把数据放到项目中去,需要加@RefreshScope注解,重新注入到业务对象
重新启动sp03项目
访问sp03项目,首先访问sp03 localhost:8101/99
在git上修改user-service-dev.yml
添加一条新的内容
访问http://localhost:6001/actuator/bus-refresh
再次访问
localhost:8101/99,发现内容已被更改
16sleuth链路追踪
更改sp04-orderservice的OrderServiceImpl文件使用feign调用item和user服务
package cn.tedu.sp04.service;
import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.service.OrderService;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private ItemClient itemClient;
@Autowired
private UserClient userClient;
@Override
public Order getOrder(String id) {
log.info("获取订单,id="+id);
// 远程调用商品,获取商品列表
JsonResult<List<Item>> items = itemClient.getItems(id);
// 远程调用用户,获取用户
JsonResult<User> user = userClient.getUser(8);// 真实项目中应该获取已登录的用户id
Order order = new Order();
order.setId(id);
order.setUser(user.getData());
order.setItems(items.getData());
return order;
}
@Override
public void addOrder(Order order) {
log.info("添加订单,order="+order);
// 远程调用商品,减少商品库存
itemClient.decreaseNumber(order.getItems());
// 远程调用用户,增加积分
userClient.addScore(order.getUser().getId(), 1000);
}
}
随着系统规模越来越大,微服务之间调用关系变得错综复杂,一条调用链路中可能调用多个微服务,任何一个微服务不可用都可能造整个调用过程失败
spring cloud sleuth 可以跟踪调用链路,分析链路中每个节点的执行情况
微服务中添加 spring cloud sleuth 依赖
修改以下微服务的 pom.xml,添加 sleuth 依赖
- sp02-item-service
- sp03-user-service
- sp04-order-service
- sp06-zuul
编辑起步依赖,分别 sleuth
依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
在控制台查看链路跟踪日志
- 通过 zuul 网关,访问 order-service
http://localhost:3001/order-service/112233
- 请求id:请求到达第一个微服务时生成一个请求id,该id在调用链路中会一直向后面的微服务传递
- span id:链路中每一步微服务调用,都生成一个新的id
17sleuth + zipkin 链路分析
zipkin 可以收集链路跟踪数据,提供可视化的链路分析
链路数据抽样比例
默认 10% 的链路数据会被发送到 zipkin 服务。可以配置修改抽样比例
在zuul中添加
spring:
sleuth:
sampler:
probability: 0.1
zipkin 服务
下载 zipkin 服务器
- https://github.com/openzipkin/zipkin
- http://localhost:9411/zipkin
微服务添加 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>
向sp06中添加rabbitmq消息总线,sp02,sp03,sp04,sp06中都要有rabbitmq
<!--rabbitmq消息总线-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置sp06的rabbitMQ连接
#rabbitmq配置
rabbitmq:
host: 192.168.126.17
port: 5672
username: admin
password: admin
配置连接方式,将下面配置复制到config目录下面的文件配置中
zipkin:
sender:
type: rabbit
重启修改的服务
启动zipkin,使用以下启动命令
cmd执行以下命令
java -jar zipkin-server-2.23.2-exec.jar --zipkin.collector.rabbitmq.uri=amqp://admin:admin@192.168.126.17:5672
访问http://localhost:3002/order-service/123
打开http://localhost:9411/zipkin/可以看到