Spring Cloud 入门手册

Spring Cloud 入门手册

Spring Cloud 入门手册Spring CloudSpring Cloud 介绍Spring Cloud 是一系列框架的集合。他利用 SpringBoot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务注册、配置中心、消息总线、负载均衡、断路器、数据监控等,都可以用 SpringBoot 的开发风格做到一键启动和部署。Spring Cloud 并没有重复制造轮子,它只是将目前各家公司开发的比较成熟、经得起实际考验的服务框架组合起来,通过 SpringBoot 风格进行再封装屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套简单易懂、易部署和易维护的分布式系统开发工具包。

Spring Cloud 对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发自己的分布式系统基础设施,使用 Spring Cloud 一站式解决方案能在从容应对业务发展的同时大大减少开发成本。同时,随着近几年微服务架构和 Docker 容器概念的火爆,也会让 Spring Cloud 在未来越来越“云”化的软件开发风格中立有一席之地,尤其是在目前五花八门的分布式解决方案中提供了标准化的、一站式的技术方案,意义可能会堪比当年 Servlet 规范的诞生,有效推进服务端软件系统技术水平的进步。

Spring Cloud 技术组成 Eureka

微服务治理,服务注册和发现

Ribbon

负载均衡、请求重试

Hystrix

断路器,服务降级、熔断

Feign

Ribbon + Hystrix 集成,并提供声明式客户端

Hystrix dashboard 和 Turbine

Hystrix 数据监控

Zuul

API 网关,提供微服务的统一入口,并提供统一的权限验证

Config

配置中心

Bus

消息总线, 配置刷新

Sleuth + Zipkin

链路跟踪

Spring Cloud 对比 DubboDubbo Dubbo只是一个远程调用(RPC)框架默认基于长连接,支持多种序列化格式Spring Cloud 框架集提供了一整套微服务解决方案(全家桶)基于http调用,RestAPIservice - 服务商品服务 item-service,端口8001用户服务 user-service,端口8101订单服务 order-service,端口8201image-20210820231007665springcloud01 父项目新建 maven 项目image-20210823232419265pom.xml代码语言:javascript复制

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">

4.0.0

pom

org.springframework.boot

spring-boot-starter-parent

2.3.9.RELEASE

cn.tedu

order-parent

1.0-SNAPSHOT

8

8

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

org.springframework.cloud

spring-cloud-dependencies

Hoxton.SR11

pom

import

删除 src 目录commons 通用项目新建 maven 项目image-20210823232927944pom.xml代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp01-commons

8

8

com.fasterxml.jackson.module

jackson-module-parameter-names

2.9.8

com.fasterxml.jackson.datatype

jackson-datatype-jdk8

2.9.8

com.fasterxml.jackson.datatype

jackson-datatype-jsr310

2.9.8

com.fasterxml.jackson.datatype

jackson-datatype-guava

2.9.8

org.projectlombok

lombok

1.18.20

javax.servlet

javax.servlet-api

3.1.0

org.slf4j

slf4j-api

1.7.26

org.apache.commons

commons-lang3

3.9

Java 源文件image-20210820232005501pojoItem代码语言:javascript复制package cn.tedu.sp01.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import java.io.Serializable;

/**

* @ClassName Item

* @Description

* @Author keke

* @Time 2021/7/16 17:45

* @Version 1.0

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class Item implements Serializable {

private static final long serialVersionUID = 8754047948579682479L;

private Integer id;

private String name;

private Integer number;

}User代码语言:javascript复制package cn.tedu.sp01.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import java.io.Serializable;

/**

* @ClassName User

* @Description

* @Author keke

* @Time 2021/7/16 17:48

* @Version 1.0

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class User implements Serializable {

private static final long serialVersionUID = 3239690393225870683L;

private Integer id;

private String username;

private String password;

}Order代码语言:javascript复制package cn.tedu.sp01.pojo;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

import java.io.Serializable;

import java.util.List;

/**

* @ClassName Order

* @Description

* @Author keke

* @Time 2021/7/16 17:49

* @Version 1.0

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class Order implements Serializable {

private static final long serialVersionUID = 8099430514561346415L;

private String id;

private User user;

private List items;

}serviceItemService代码语言:javascript复制package cn.tedu.sp01.service;

import cn.tedu.sp01.pojo.Item;

import java.util.List;

/**

* @ClassName ItemService

* @Description 商品的业务接口

* @Author keke

* @Time 2021/7/16 17:51

* @Version 1.0

*/

public interface ItemService {

/**

* 获取一个订单的商品列表

*/

List getItems(String orderId);

/**

* 减少商品库存

*/

void decreaseNumber(List items);

}UserService代码语言:javascript复制package cn.tedu.sp01.service;

import cn.tedu.sp01.pojo.User;

/**

* @ClassName UserService

* @Description

* @Author keke

* @Time 2021/7/16 17:56

* @Version 1.0

*/

public interface UserService {

/**

* 获取用户

*/

User getUser(Integer id);

/**

* 增加用户积分

*/

void addScore(Integer id, Integer score);

}OrderService代码语言:javascript复制package cn.tedu.sp01.service;

import cn.tedu.sp01.pojo.Order;

/**

* @ClassName OrderService

* @Description

* @Author keke

* @Time 2021/7/16 17:58

* @Version 1.0

*/

public interface OrderService {

/**

* 获取订单

*/

Order getOrder(String id);

/**

* 保存订单

*/

void addOrder(Order order);

}utilCookieUtil代码语言:javascript复制package cn.tedu.sp01.web.util;

import javax.servlet.http.Cookie;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @author Administrator

*/

public class CookieUtil {

/**

*

* @param response

* @param name

* @param value

* @param domain

* @param path

* @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 (cookies != null) {

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代码语言:javascript复制package cn.tedu.sp01.web.util;

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;

import org.apache.commons.lang3.StringUtils;

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;

/**

* @author Administrator

*/

@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 from(URL url, Class 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 from(InputStream inputStream, Class c) {

try {

return mapper.readValue(inputStream, c);

} catch (IOException e) {

log.error("jackson from error, type: {}", c, e);

return null;

}

}

public static V from(File file, Class 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 from(Object jsonObj, Class c) {

try {

return mapper.readValue(jsonObj.toString(), c);

} catch (IOException e) {

log.error("jackson from error, json: {}, type: {}", jsonObj, c, e);

return null;

}

}

public static V from(String json, Class 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 from(URL url, TypeReference 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 from(InputStream inputStream, TypeReference type) {

try {

return mapper.readValue(inputStream, type);

} catch (IOException e) {

log.error("jackson from error, type: {}", type, e);

return null;

}

}

public static V from(File file, TypeReference 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 from(Object jsonObj, TypeReference type) {

try {

return mapper.readValue(jsonObj.toString(), type);

} catch (IOException e) {

log.error("jackson from error, json: {}, type: {}", jsonObj, type, e);

return null;

}

}

public static V from(String json, TypeReference type) {

try {

return mapper.readValue(json, type);

} catch (IOException e) {

log.error("jackson from error, json: {}, type: {}", json, type, e);

return null;

}

}

public static String to(List list) {

try {

return mapper.writeValueAsString(list);

} catch (JsonProcessingException e) {

log.error("jackson to error, obj: {}", list, e);

return null;

}

}

public static String to(V v) {

try {

return mapper.writeValueAsString(v);

} catch (JsonProcessingException e) {

log.error("jackson to error, obj: {}", v, e);

return null;

}

}

public static void toFile(String path, List list) {

try (Writer writer = new FileWriter(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 void toFile(String path, V v) {

try (Writer writer = new FileWriter(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 ArrayList getList(String json, String key) {

if (StringUtils.isEmpty(json)) {

return null;

}

String string = getString(json, key);

return from(string, new TypeReference>() {});

}

public static 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 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 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代码语言:javascript复制package cn.tedu.sp01.web.util;

import cn.tedu.sp01.pojo.Item;

import cn.tedu.sp01.pojo.Order;

import cn.tedu.sp01.pojo.User;

import lombok.Getter;

import lombok.Setter;

import java.util.List;

/**

* @author Administrator

*/

@Getter

@Setter

public class JsonResult {

/**

* 成功

*/

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().code(code).msg(msg);

}

public static JsonResult build(int code, T data) {

return new JsonResult().code(code).data(data);

}

public static JsonResult build(int code, String msg, T data) {

return new JsonResult().code(code).msg(msg).data(data);

}

public JsonResult code(int code) {

this.code = code;

return this;

}

public JsonResult msg(String msg) {

this.msg = msg;

return this;

}

public JsonResult data(T data) {

this.data = data;

return this;

}

public JsonResult> data(List items) {

this.data = (T) items;

return (JsonResult>) this;

}

public JsonResult data(User user) {

this.data = (T) user;

return (JsonResult) this;

}

public JsonResult data(Order order) {

this.data = (T) order;

return (JsonResult) this;

}

public static JsonResult ok() {

return build(SUCCESS);

}

public static JsonResult ok(String msg) {

return build(SUCCESS, msg);

}

public static JsonResult 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 商品服务新建项目配置依赖 pom.xml配置 application.yml配置主程序编写代码新建 maven 项目image-20210823233333060pom.xml要添加 sp01-commons 项目依赖代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp02-itemservice

8

8

cn.tedu

sp01-commons

1.0-SNAPSHOT

application.yml代码语言:javascript复制# 在注册中心中注册的服务id(服务名称)

spring:

application:

name: item-service

server:

port: 8001主程序代码语言:javascript复制package cn.tedu.sp02;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @ClassName Sp02ItemserviceApplication

* @Description

* @Author keke

* @Time 2021/7/17 14:34

* @Version 1.0

*/

@SpringBootApplication

public class Sp02ItemserviceApplication {

public static void main(String[] args) {

SpringApplication.run(Sp02ItemserviceApplication.class, args);

}

}Java 源文件image-20210821102811777ItemServiceImpl代码语言:javascript复制package cn.tedu.sp02.item.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;

/**

* @ClassName ItemServiceImpl

* @Description

* @Author keke

* @Time 2021/7/17 14:49

* @Version 1.0

*/

@Service

@Slf4j

public class ItemServiceImpl implements ItemService {

@Override

public List getItems(String orderId) {

ArrayList list = new ArrayList<>();

list.add(new Item(1, "商品 1", 1));

list.add(new Item(2, "商品 2", 2));

list.add(new Item(3, "商品 3", 3));

list.add(new Item(4, "商品 4", 4));

list.add(new Item(5, "商品 5", 5));

return list;

}

@Override

public void decreaseNumber(List items) {

for (Item item : items) {

log.info("减少商品库存:" + item);

}

}

}ItemController代码语言:javascript复制package cn.tedu.sp02.item.controller;

import cn.tedu.sp01.pojo.Item;

import cn.tedu.sp01.service.ItemService;

import cn.tedu.sp01.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.GetMapping;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PostMapping;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import java.util.Random;

/**

* @ClassName ItemController

* @Description

* @Author keke

* @Time 2021/7/17 14:51

* @Version 1.0

*/

@RestController

@Slf4j

public class ItemController {

@Autowired

private ItemService itemService;

@Value("${server.port}") // 注入配置的端口号

private int port;

/**

* 获取商品列表,商品列表封装在JSONResult对象中,再返回给客户端

* @param orderId

* @return

*/

@GetMapping("/{orderId}")

public JsonResult> getItems(@PathVariable String orderId){

log.info("获取订单商品列表,orderId=" + orderId);

if (Math.random() < 0.9){

// 随机延迟时长

int t = new Random().nextInt(5000);

log.info("延迟:" + t);

try {

Thread.sleep(t);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

List items = itemService.getItems(orderId);

return JsonResult.ok().msg("port=" + port).data(items);

}

/**

* 减少商品库存

* @RequestBody 从客户端提交的http协议体数据中接收JSON数据

*/

@PostMapping("/decreaseNumber")

public JsonResult decreaseNumber(@RequestBody List items){

itemService.decreaseNumber(items);

return JsonResult.ok().msg("减少商品库存成功");

}

}Spring MVC 接收参数的几个注解image-20210821103119206访问测试根据 orderId,查询商品 http://localhost:8001/35

减少商品库存 http://localhost:8001/decreaseNumber

使用 postman,POST 发送以下格式数据:

代码语言:javascript复制[

{

"id": 1,

"name": "abc",

"number": 23

},

{

"id": 2,

"name": "def",

"number": 11

}

]image-20210821104058427user-service 用户服务新建项目配置依赖 pom.xml配置 application.yml配置主程序编写代码新建 maven 项目image-20210821112737640pom.xml要添加 sp01-commons 依赖代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp03-userservice

8

8

cn.tedu

sp01-commons

1.0-SNAPSHOT

application.yml代码语言:javascript复制server:

port: 8101

spring:

application:

name: user-service

# 自定义配置属性

# 配置测试用的用户数据{id: , username: , password: }

sp:

user-service:

users: "[{\"id\": 7, \"username\": \"abc\", \"password\": \"123\"},

{\"id\": 8, \"username\": \"def\", \"password\": \"456\"},

{\"id\": 9, \"username\": \"ghi\", \"password\": \"789\"},

{\"id\": 99, \"username\": \"aaa\", \"password\": \"bbb\"}]"主程序代码语言:javascript复制package cn.tedu.sp03;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

/**

* @ClassName Sp03UserserviceApplication

* @Description

* @Author keke

* @Time 2021/7/17 15:50

* @Version 1.0

*/

@SpringBootApplication

public class Sp03UserserviceApplication {

public static void main(String[] args) {

SpringApplication.run(Sp03UserserviceApplication.class, args);

}

}Java 源文件image-20210821115357835UserServiceImpl代码语言:javascript复制package cn.tedu.sp03.user.service;

import cn.tedu.sp01.pojo.User;

import cn.tedu.sp01.service.UserService;

import cn.tedu.sp01.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;

/**

* @ClassName UserServiceImpl

* @Description

* @Author keke

* @Time 2021/7/17 16:01

* @Version 1.0

*/

@Service

@Slf4j

public class UserServiceImpl implements UserService {

// 注入yml中配置的测试用的用户数据

@Value("${sp.user-service.users}")

private String userJson;

@Override

public User getUser(Integer id) {

// userJson --> List

List list = JsonUtil.from(userJson, new TypeReference>() {});

for (User user : list) {

if (user.getId().equals(id)) {

return user;

}

}

// 如果没有找到用户,这里返回一个写死的用户数据

return new User(id, "用户名" + id, "密码" + id);

}

@Override

public void addScore(Integer id, Integer score) {

log.info("增加用户积分, userId=" + id + ", score=" + score);

}

}UserController代码语言:javascript复制package cn.tedu.sp03.user.controller;

import cn.tedu.sp01.pojo.User;

import cn.tedu.sp01.service.UserService;

import cn.tedu.sp01.web.util.JsonResult;

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;

/**

* @ClassName UserController

* @Description

* @Author keke

* @Time 2021/7/17 16:11

* @Version 1.0

*/

@RestController

public class UserController {

@Autowired

private UserService userService;

/**

* 获取用户

*/

@GetMapping("{userId}")

public JsonResult getUser(@PathVariable Integer userId){

User user = userService.getUser(userId);

return JsonResult.ok().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("增加用户积分成功");

}

}访问测试根据 userId 查询用户信息 http://localhost:8101/7

根据 userId 为用户增加积分 http://localhost:8101/7/score?score=100

order-service 订单服务新建项目配置依赖 pom.xml配置 application.yml配置主程序编写代码新建 maven 项目image-20210821120358485pom.xml要添加 sp01-commons 项目依赖代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp04-orderservice

8

8

cn.tedu

sp01-commons

1.0-SNAPSHOT

application.yml代码语言:javascript复制spring:

application:

name: order-service

server:

port: 8201主程序代码语言:javascript复制package cn.tedu.sp04;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.openfeign.EnableFeignClients;

/**

* @ClassName Sp04OrderserviceApplication

* @Description

* @Author keke

* @Time 2021/7/17 16:28

* @Version 1.0

*/

@SpringBootApplication

@EnableFeignClients

public class Sp04OrderserviceApplication {

public static void main(String[] args) {

SpringApplication.run(Sp04OrderserviceApplication.class, args);

}

}Java 源文件image-20210821121033654OrderServiceImpl代码语言:javascript复制package cn.tedu.sp04.order.service;

import cn.tedu.sp01.pojo.Order;

import cn.tedu.sp01.service.OrderService;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Service;

/**

* @ClassName OrderServiceImpl

* @Description

* @Author keke

* @Time 2021/7/17 20:07

* @Version 1.0

*/

@Service

@Slf4j

public class OrderServiceImpl implements OrderService {

@Override

public Order getOrder(String id) {

log.info("获取订单, orderId =" + id);

// TODO:远程调用商品获取商品列表

// TODO:远程调用用户获取用户数据

Order order = new Order();

order.setId(id);

return order;

}

@Override

public void addOrder(Order order) {

log.info("添加订单:" + order);

// TODO:远程调用商品,减少商品库存

// TODO:远程调用用户,增加用户积分

}

}OrderController代码语言:javascript复制package cn.tedu.sp04.order.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.sp01.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;

/**

* @ClassName OrderController

* @Description

* @Author keke

* @Time 2021/7/17 20:08

* @Version 1.0

*/

@RestController

@Slf4j

public class OrderController {

@Autowired

private OrderService orderService;

/**

* 获取订单

*/

@GetMapping("/{orderId}")

public JsonResult getOrder(@PathVariable String orderId){

Order order = orderService.getOrder(orderId);

return JsonResult.ok().data(order);

}

/**

* 添加订单

*/

@GetMapping("/")

public JsonResult addOrder(){

Order order = new Order();

order.setId("56u5645y54");

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("添加订单成功");

}

}访问测试根据 orderId,获取订单 http://localhost:8201/123abc

保存订单,观察窗控制台日志输出 http://localhost:8201/

service 访问测试汇总item-service根据 orderId,查询商品 http://localhost:8001/35

减少商品库存 http://localhost:8001/decreaseNumber

使用 postman,POST 发送以下格式数据:

代码语言:javascript复制[

{

"id": 1,

"name": "abc",

"number": 23

},

{

"id": 2,

"name": "def",

"number": 11

}

]image-20210821104058427user-service根据 userId 查询用户信息 http://localhost:8101/7

根据 userId 为用户增加积分 http://localhost:8101/7/score?score=100

order-service根据 orderId,获取订单 http://localhost:8201/123abc

保存订单,观察窗控制台日志输出 http://localhost:8201/

eureka 注册与发现在这里插入图片描述创建 eureka 项目配置依赖 pom.xml配置 application.yml主程序启用 eureka 服务器启动,访问测试新建 maven 项目在这里插入图片描述pom.xml代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp05-eureka

8

8

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

application.yml代码语言:javascript复制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 eureka 集群服务器之间,通过 eureka.instance.hostname 来区分

eureka.server,enable-self-perservation

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代码语言:javascript复制package cn.tedu.sp05;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**

* @ClassName Sp05EurekaApplication

* @Description

* @Author keke

* @Time 2021/7/17 21:30

* @Version 1.0

*/

@SpringBootApplication

// 触发eureka服务器的自动配置

@EnableEurekaServer

public class Sp05EurekaApplication {

public static void main(String[] args) {

SpringApplication.run(Sp05EurekaApplication.class, args);

}

}修改 hosts 文件,添加 eureka 映射C:\Windows\System32\drivers\etc\hosts

添加内容

代码语言:javascript复制127.0.0.1 eureka1

127.0.0.1 eureka2启动,并访问测试http://eureka1:2001image-20210824001018634service provider 服务提供者image-20210821122452430修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器pom.xml 添加 eureka 依赖application.yml添加 eureka 注册信息启动服务,在 eureka 中查看注册信息pom.xml 添加 eureka 客户端依赖利用 EditStarts 添加

image-20210824001417504上面的操作会在 pom.xml 中添加以下依赖

代码语言:javascript复制

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

application.yml 添加 eureka 注册配置代码语言:javascript复制# defaultZone 表示默认地点

# 如果使用云服务,服务商可以通过不同的eureka服务器

# 如果没有云服务,就只能写defaultZone

eureka:

client:

service-url:

defaultZone: http://eureka1:2001/eureka eureka.instance.lease-renewal-interval-in-seconds

心跳间隔时间,默认30秒

eureka.client.service-url.defaultZone

默认位置,可以修改为具体地点,表示 eureka 服务器的部署位置,需要云服务器提供

eureka.client.registry-fetch-interval-seconds

拉取注册时间间隔时间,默认30秒

启动服务,在 eureka 中查看注册信息image-20210824105321096http://eureka1:2001[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QVN76cUE-1639477451465)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210824105434536.png)]

eureka 和 “服务提供者” 的高可用[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bcVuAzBT-1639477451465)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210824105606886.png)]

item-service 高可用启动参数 --server.port 可以覆盖 yml 中的端口配置

配置启动参数item-service-8001代码语言:javascript复制--server.port=8001[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gtjSfNJC-1639477451466)(https://i.loli.net/2021/11/29/a4tO59TLhUjYQ2F.png)]

image-20210824110306300image-20210824110508432item-service-8002代码语言:javascript复制--server.port=8002image-20210824110558701image-20210824111607095启动测试访问 eureka 查看 item-service 注册信息image-20210824112134310 访问两个端口测试

http://localhost:8001/35

http://localhost:8002/35

eureka 高可用添加两个服务器的 profile 配置文件

application-eureka1.yml代码语言:javascript复制eureka:

instance:

hostname: eureka1

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

fetch-registry: true

register-with-eureka: true

service-url:

defaultZone: http://eureka2:2002/eurekaapplication-eureka2.yml代码语言:javascript复制1eureka:

instance:

hostname: eureka2

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

fetch-registry: true

register-with-eureka: true

service-url:

defaultZone: http://eureka1:2001/eureka配置启动参数 --spring.profiles.active 和 --server.porteureka1 启动参数代码语言:javascript复制--spring.profiles.active=eureka1

--server.port=2001image-20210824131104706image-20210824131300938eureka2 启动参数代码语言:javascript复制--spring.profiles.active=eureka2

--server.port=2002image-20210824131734898image-20210824131832010image-20210824131919936

如果在命令行运行,可以在命令行中添加参数:

代码语言:javascript复制java -jar xxx.jar --spring.profiles.active=eureka1 --server.port=2001访问 eureka 服务器,查看注册信息http://eureka1:2001/image-20210824132653117http://eureka2:2002/image-20210824132755658eureka 客户端注册时,向两个服务器注册修改以下微服务

sp02-itemservicesp03-userservicesp04-orderservice代码语言:javascript复制eureka:

client:

service-url:

defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka 当一个 eureka 服务宕机时,仍可以连接另一个 eureka 服务

order-service 调用商品库存服务和用户服务image-20210825103453784修改 sp04-orderservice 项目,添加 feign,调用 item-service 和 user-service

pom.xml主程序ItemClientUserClientOrderServiceImplpom.xml在父项目的 pom.xml 利用 EditStarts 添加

image-20210825104520918代码如下:

代码语言:javascript复制

org.springframework.cloud

spring-cloud-starter-openfeign

主程序代码语言:javascript复制package cn.tedu.sp04;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.openfeign.EnableFeignClients;

/**

* @ClassName Sp04OrderserviceApplication

* @Description

* @Author keke

* @Time 2021/8/23 23:56

* @Version 1.0

*/

@EnableFeignClients

@SpringBootApplication

public class Sp04OrderserviceApplication {

public static void main(String[] args) {

SpringApplication.run(Sp04OrderserviceApplication.class, args);

}

}ItemClient代码语言:javascript复制package cn.tedu.sp04.order.feign;

import cn.tedu.sp01.pojo.Item;

import cn.tedu.sp01.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;

/**

* @ClassName ItemClient

* @Description 三项配置:

* 1.调用哪个服务

* 2.调用服务的哪个路径

* 3.向这个路径提交什么参数

* @Author keke

* @Time 2021/7/18 16:52

* @Version 1.0

*/

@FeignClient(name = "item-service")

public interface ItemClient {

/**

* 远程调用商品,获取商品列表

* @param orderId

* @return

*/

@GetMapping("/{orderId}")

JsonResult> getItems(@PathVariable("orderId") String orderId);

/**

* 远程调用商品,减少商品库存

* @param items

* @return

*/

@PostMapping("/decreaseNumber")

JsonResult decreaseNumber(@RequestBody List items);

}UserClient代码语言:javascript复制package cn.tedu.sp04.order.feign;

import cn.tedu.sp01.pojo.User;

import cn.tedu.sp01.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;

/**

* @ClassName UserClient

* @Description

* @Author keke

* @Time 2021/7/18 16:56

* @Version 1.0

*/

@FeignClient(name = "user-service")

public interface UserClient {

/**

* 远程调用用户,获取用户

* @param userId

* @return

*/

@GetMapping("/{userId}")

JsonResult getUser(@PathVariable("userId") Integer userId);

/**

* 远程调用用户,增加用户积分

* @param userId

* @param score

* @return

*/

@GetMapping("/{userId}/score")

JsonResult addScore(@PathVariable("userId") Integer userId,

@RequestParam("score") Integer score);

}OrderServiceImpl代码语言:javascript复制package cn.tedu.sp04.order.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.sp01.web.util.JsonResult;

import cn.tedu.sp04.feign.ItemClient;

import cn.tedu.sp04.feign.UserClient;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.List;

/**

* @ClassName OrderServiceImpl

* @Description

* @Author keke

* @Time 2021/7/17 20:07

* @Version 1.0

*/

@Service

@Slf4j

public class OrderServiceImpl implements OrderService {

@Autowired

private ItemClient itemClient;

@Autowired

private UserClient userClient;

@Override

public Order getOrder(String id) {

log.info("获取订单, orderId =" + id);

// TODO:远程调用商品获取商品列表

JsonResult> items = itemClient.getItems(id);

// TODO:远程调用用户获取用户数据

JsonResult user = userClient.getUser(8);

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);

// TODO:远程调用商品,减少商品库存

itemClient.decreaseNumber(order.getItems());

// TODO:远程调用用户,增加用户积分

userClient.addScore(order.getUser().getId(), 1000);

}

}启动服务,访问测试[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KXCkEJTZ-1639477451483)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210825113001069.png)]

根据 orderId,获取订单 http://localhost:8201/123abc保存订单 http://localhost:8201/zuul API 网关image-20210824221658473zuul API 网关,为微服务应用提供同一段对外访问接口。

zuul 还提供过滤器,对所有微服务提供统一的请求校验

新建 sp06-zuul 项目image-20210824223738007pom.xml需要添加 sp01-commons 依赖代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp06-zuul

8

8

org.springframework.cloud

spring-cloud-starter-netflix-zuul

cn.tedu

sp01-commons

1.0-SNAPSHOT

org.springframework.retry

spring-retry

application.ymlzuul 路由配置可以省略,缺省以服务 id 作为访问路径代码语言:javascript复制server:

port: 3001

spring:

application:

name: zuul

eureka:

client:

service-url:

defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

instance:

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

zuul:

routes:

item-service: /item-service/**

user-service: /user-service/**

order-service: /order-service/**主程序添加 @EnableZuulProxy 注解

代码语言:javascript复制package cn.tedu.sp06;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

/**

* @ClassName Sp06ZuulApplication

* @Description

* @Author keke

* @Time 2021/7/18 20:39

* @Version 1.0

*/

@SpringBootApplication

@EnableZuulProxy

public class Sp06ZuulApplication {

public static void main(String[] args) {

SpringApplication.run(Sp06ZuulApplication.class, args);

}

}启动服务,访问测试image-20210824225029081http://eureka1:2001

http://localhost:3001/item-service/35

http://localhost:3001/item-service/decreaseNumber

使用 postman,POST 发送以下格式数据

代码语言:javascript复制[

{

"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/

配置 zuul 开启重试,并配置 ribbon 重试参数需要开启重试,默认不开启代码语言:javascript复制zuul:

routes:

item-service: /item-service/**

user-service: /user-service/**

order-service: /order-service/**

# 启用重试

retryable: true

ribbon:

ConnectTimeout: 1000

ReadTimeout: 1000

MaxAutoRetriesNextServer: 1

MaxAutoRetries: 1

# 针对一个指定的后台服务,来配置重试参数

item-service:

ribbon:

MaxAutoRetries: 0

# 暴露actuator的所有监控数据

management:

endpoints:

web:

exposure:

include: "*"zuul 降级创建降级类getRoute()方法中指定应用降级类的服务 id,星号或 null 值可以通配所有服务ItemFallback代码语言:javascript复制package cn.tedu.sp06.fallback;

import cn.tedu.sp01.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 org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.nio.charset.StandardCharsets;

/**

* @ClassName ItemFallback

* @Description 通过zuul网关,调用后台商品服务失败时,会执行这段降级代码,向客户端返回降级结果

* @Author keke

* @Time 2021/7/19 11:25

* @Version 1.0

*/

@Component

public class ItemFallback implements FallbackProvider {

/**

* 设置当前降级类,是针对哪个服务的降级类

* - item-service:只针对商品服务降级

* - *: 针对所有服务都应用当前降级类

* - null: 针对所有服务都应用当前降级类

* @return

*/

@Override

public String getRoute() {

return "item-service";

}

/**

* 发送给客户端的降级结果,封装在response对象中

* @param route

* @param cause

* @return

*/

@Override

public ClientHttpResponse fallbackResponse(String route, Throwable cause) {

return new ClientHttpResponse() {

@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();

}

/**

* 用来关闭流

* ByteArrayInputStream不需要关闭

*/

@Override

public void close() {

}

@Override

public InputStream getBody() throws IOException {

String json = JsonResult.err().code(500)

.msg("调用商品服务失败").toString();

return new

ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));

}

@Override

public HttpHeaders getHeaders() {

// Content-Type:application/json;charset=UTF-8

HttpHeaders httpHeaders = new HttpHeaders();

httpHeaders.add("Content-Type", "application/json;charset=UTF-8");

return httpHeaders;

}

};

}

}OrderFallback代码语言:javascript复制package cn.tedu.sp06.fallback;

import cn.tedu.sp01.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 org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.nio.charset.StandardCharsets;

/**

* @ClassName OrderFallback

* @Description 通过zuul网关,调用后台商品服务失败时,会执行这段降级代码,向客户端返回降级结果

* @Author keke

* @Time 2021/7/19 11:25

* @Version 1.0

*/

@Component

public class OrderFallback implements FallbackProvider {

/**

* 设置当前降级类,是针对哪个服务的降级类

* - order-service:只针对订单服务降级

* - *: 针对所有服务都应用当前降级类

* - null: 针对所有服务都应用当前降级类

* @return

*/

@Override

public String getRoute() {

return "order-service";

}

/**

* 发送给客户端的降级结果,封装在response对象中

* @param route

* @param cause

* @return

*/

@Override

public ClientHttpResponse fallbackResponse(String route, Throwable cause) {

return new ClientHttpResponse() {

@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();

}

/**

* 用来关闭流

* ByteArrayInputStream不需要关闭

*/

@Override

public void close() {

}

@Override

public InputStream getBody() throws IOException {

String json = JsonResult.err().code(500)

.msg("调用订单服务失败").toString();

return new

ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));

}

@Override

public HttpHeaders getHeaders() {

// Content-Type:application/json;charset=UTF-8

HttpHeaders httpHeaders = new HttpHeaders();

httpHeaders.add("Content-Type", "application/json;charset=UTF-8");

return httpHeaders;

}

};

}

}LoginFilter代码语言:javascript复制package cn.tedu.sp06.filter;

import cn.tedu.sp01.web.util.JsonResult;

import com.netflix.zuul.ZuulFilter;

import com.netflix.zuul.context.RequestContext;

import com.netflix.zuul.exception.ZuulException;

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;

/**

* @ClassName LoginFilter

* @Description

* @Author keke

* @Time 2021/7/18 21:19

* @Version 1.0

*/

@Component

public class LoginFilter extends ZuulFilter {

/**

* 过滤器类型: pre, routing, post, error

* @return

*/

@Override

public String filterType() {

return FilterConstants.PRE_TYPE;

}

/**

* 位置顺序号,zuul的前置过滤器默认有5个

* 其中第5个过滤器中,在上下文对象中放入了serviceId

* 在后面过滤器中,才能使用serviceId

* @return

*/

@Override

public int filterOrder() {

return 6;

}

/**

* 判断针对当前请求,是否要执行过滤代码

* @return

*/

@Override

public boolean shouldFilter() {

/*

调用item-service,需要检查权限

调用其他服务不判断权限,可以直接访问

*/

// 获取当前请求调用的服务id

RequestContext currentContext = RequestContext.getCurrentContext();

String serviceId = (String) currentContext.get(FilterConstants.SERVICE_ID_KEY);

// 判断id是不是item-service

return "item-service".equalsIgnoreCase(serviceId);

}

/**

* 过滤代码

* @return

* @throws com.netflix.zuul.exception.ZuulException

*/

@Override

public Object run() throws ZuulException {

// 得到request对象

RequestContext currentContext = RequestContext.getCurrentContext();

HttpServletRequest request = currentContext.getRequest();

// 用request接收token参数

String token = request.getParameter("token");

// 如果收不到token,阻止向后台转发,直接向客户端返回响应

if (StringUtils.isBlank(token)) {

// 阻止向后台服务转发

currentContext.setSendZuulResponse(false);

// 向客户端直接发送响应

String json = JsonResult.err()

.code(JsonResult.NOT_LOGIN)

.msg("Not login!")

.toString();

// http 协议头属性 Content-Type:application/json;charset=UTF-8

currentContext.addZuulRequestHeader("Content-Type",

"application/json;charset=UTF-8");

currentContext.setResponseBody(json);

}

// 在当前zuul版本中,没有使用这个返回值

return null;

}

}启动服务,访问测试http://localhost:3001/item-service/35image-20210825113821235http://localhost:3001/actuator/hystrix.streamimage-20210826115519967Hystrix dashboard 断路器仪表盘image-20210825230613201Hystrix 对请求的降级和熔断,可以产生监控信息,hystrix dashboard 可以实时的进行监控

新建 maven 项目配置依赖 pom.xml配置 application.yml主程序启用 eureka 服务器启动,访问测试新建 maven 项目image-20210825232659347pom.xml代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp07-hystrix-dashboard

8

8

org.springframework.cloud

spring-cloud-starter-netflix-hystrix-dashboard

application.yml代码语言:javascript复制server:

port: 4001

spring:

application:

name: hystrix-dashboard

# 允许抓取日志的服务器列表

hystrix:

dashboard:

proxy-stream-allow-list: localhost

eureka:

client:

service-url:

defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

instance:

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}主程序添加 @EnableHystrixDashBoard代码语言:javascript复制package cn.tedu.sp07;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

/**

* @ClassName Sp07HystrixDashboardApplication

* @Description

* @Author keke

* @Time 2021/7/19 14:29

* @Version 1.0

*/

@SpringBootApplication

@EnableHystrixDashboard

public class Sp07HystrixDashboardApplication {

public static void main(String[] args) {

SpringApplication.run(Sp07HystrixDashboardApplication.class, args);

}

}启动服务,访问测试在这里插入图片描述http://localhost:4001/hystrix

在这里插入图片描述填入 hystrix 的监控端点,开启监控http://localhost:3001/actuator/hystrix.stream在这里插入图片描述通过 hystrix 访问服务多次,观察监控信息http://localhost:3001/item-service/35?token=2wasa2f3

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

在这里插入图片描述在这里插入图片描述hystrix 熔断整个链路达到一定的阈值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。

满足第一个条件的情况下,如果请求的错误百分比大于阈值,则打开断路器,默认为50%。

hystrix 的逻辑,先判断是否满足第一个条件,再判断第二个条件,如果两个条件都满足,则会开启断路器,断路器打开5秒后,会处于半开状态,会尝试转发请求,如果仍然失败,保持打开状态,如果成功,则关闭断路器

使用 Apache 的并发访问测试工具 ab http://httpd.apache.org/docs/current/platform/windows.html#down

image-20210826134711022用 ab 工具,以并发50次,来发送20000个请求代码语言:javascript复制ab -n 20000 -c 50 http://localhost:3001/item-service/35?token=2sswbbbbw断路器为 open,所有请求都会短路,直接降级执行 fallback 方法image-20210826140149498hystrix 配置http://github.com/Netflix/Hystrix/wiki/Configuration

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

请求超时时间,超时后触发失败降级

hystrix.command.default.circuitBreaker.requestVolumeThreshold

10秒内请求数量,默认20,如果没有达到该数量,即使请求全部失败,也不会触发断路器打开

hystrix.command.default.circuitBreaker.errorThresoldPercentage

失败请求百分比,达到该比例则触发断路器打开

hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds

断路器打开多长时间后,再次允许尝试访问(半开),仍失败则继续保持打开状态,如成功访问则关闭断路器,默认5000

hystrix + turbine 集群聚合监控image-20210826143302830hystrix dashboard 一次只能监控一个服务实例,使用 turbine 可以汇集监控信息,将聚合后的信息提供给 hystrix dashboard 来集中展示和监控

新建 maven 项目image-20210826143843477pom.xml代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp08-turbine

8

8

org.springframework.cloud

spring-cloud-starter-netflix-turbine

application.yml代码语言:javascript复制spring:

application:

name: turbine

server:

port: 5001

eureka:

instance:

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

service-url:

defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

# 从zuul的两台服务器聚合hystrix日志

turbine:

app-config: zuul

cluster-name-expression: new String("default")主程序添加 @EnableTurbine 注解代码语言:javascript复制package cn.tedu.sp08;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.turbine.EnableTurbine;

/**

* @ClassName Sp08TurbineApplication

* @Description

* @Author keke

* @Time 2021/7/19 22:25

* @Version 1.0

*/

@EnableTurbine

@SpringBootApplication

public class Sp08TurbineApplication {

public static void main(String[] args) {

SpringApplication.run(Sp08TurbineApplication.class, args);

}

}启动服务,访问测试 8201服务器产生监控数据:

http://localhost:8201/abc123

http://localhost:8201/

turbine 监控路径

http://localhost:5001/turbine.stream

在 hystrix dashboard 中填入turbine 监控路径,开启监控

http://localhost:4001/hystrix

turbine聚合了order-service两台服务器的hystrix监控信息

image-20210826164319002config 配置中心image-20210826165348475yml 配置文件保存到 git 服务器,例如 github.com或者gitee.com

微服务启动时,从服务器获取配置文件

git 上存放配置文件新建文件夹,命名为 config将 sp02,sp03,sp04 三个项目的yml配置文件,复制到 config 文件夹,并改名item-service-dev.ymluser-service-dev.ymlorder-service-dev.yml最后注释掉三个项目中的 application.yml 文件

禁止配置中心的配置信息覆盖客户端配置默认配置中心配置优先级高,配置中心配置会覆盖客户端的所有配置,包括命令行的参数配置,这样我们在 item-service 中配置的端口号启动参数会无效

item-service 启动参数

--server.port=8081--server.port=8082我们可以设置禁止配置中心的配置将客户端配置覆盖掉

在三个配置文件中添加下面的配置

代码语言:javascript复制spring:

# 让配置中心的配置,不覆盖项目的本地配置和命令参数

cloud:

config:

override-none: true将项目上传到 gitimage-20210826172108551image-20210826172157362新建 maven 项目image-20210826172418801pom.xml代码语言:javascript复制

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">

order-parent

cn.tedu

1.0-SNAPSHOT

4.0.0

sp09-config

8

8

org.springframework.cloud

spring-cloud-config-server

application.yml代码语言:javascript复制server:

port: 6001

spring:

application:

name: config-server

# 连接git仓库,在指定目录下找到配置文件

cloud:

config:

server:

git:

uri: https://gitee.com/Jasonakeke/CGBVProjects

search-paths: codes/springcloud1/config

eureka:

instance:

prefer-ip-address: true

# 界面列表中显示的格式也显示ip

instance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

client:

service-url:

defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

management:

endpoints:

web:

exposure:

include: bus-refresh主程序添加 @EnableConfigServer 注解代码语言:javascript复制package cn.tedu.sp09;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.config.server.EnableConfigServer;

/**

* @ClassName Sp09ConfigApplication

* @Description

* @Author keke

* @Time 2021/7/20 13:41

* @Version 1.0

*/

@EnableConfigServer

@SpringBootApplication

public class Sp09ConfigApplication {

public static void main(String[] args) {

SpringApplication.run(Sp09ConfigApplication.class, args);

}

}sp02,sp03,sp04 的 pom.xml 添加依赖代码语言:javascript复制

org.springframework.cloud

spring-cloud-starter-config

启动服务,访问测试先启动 sp05-eureka,再启动 sp09-config,最后启动 sp02-itemservice, sp03-userservice, sp04-orderservice

image-20210826180857398

相关推荐