Spring Cloud 入门手册

文章目录

  • Spring Cloud 入门手册
    • Spring Cloud
      • Spring Cloud 介绍
      • Spring Cloud 技术组成
      • Spring Cloud 对比 Dubbo
    • service - 服务
    • springcloud01 父项目
      • 新建 maven 项目
      • pom.xml
      • 删除 src 目录
    • commons 通用项目
      • 新建 maven 项目
      • pom.xml
      • Java 源文件
      • pojo
        • Item
        • User
        • Order
      • service
        • ItemService
        • UserService
        • OrderService
      • util
        • CookieUtil
        • JsonUtil
        • JsonResult
    • item-service 商品服务
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • Java 源文件
        • ItemServiceImpl
        • ItemController
      • Spring MVC 接收参数的几个注解
      • 访问测试
    • user-service 用户服务
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • Java 源文件
        • UserServiceImpl
        • UserController
      • 访问测试
    • order-service 订单服务
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • Java 源文件
        • OrderServiceImpl
        • OrderController
      • 访问测试
    • service 访问测试汇总
    • eureka 注册与发现
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • 修改 hosts 文件,添加 eureka 映射
      • 启动,并访问测试
    • service provider 服务提供者
      • pom.xml 添加 eureka 客户端依赖
      • application.yml 添加 eureka 注册配置
      • 启动服务,在 eureka 中查看注册信息
    • eureka 和 "服务提供者" 的高可用
      • item-service 高可用
        • 配置启动参数
        • 启动测试
      • eureka 高可用
        • application-eureka1.yml
        • application-eureka2.yml
        • 配置启动参数 `--spring.profiles.active` 和 `--server.port`
        • 访问 eureka 服务器,查看注册信息
      • eureka 客户端注册时,向两个服务器注册
    • order-service 调用商品库存服务和用户服务
      • pom.xml
      • 主程序
      • ItemClient
      • UserClient
      • OrderServiceImpl
      • 启动服务,访问测试
    • zuul API 网关
      • 新建 sp06-zuul 项目
      • pom.xml
      • application.yml
      • 主程序
      • 启动服务,访问测试
      • 配置 zuul 开启重试,并配置 ribbon 重试参数
      • zuul 降级
        • 创建降级类
          • ItemFallback
          • OrderFallback
        • LoginFilter
      • 启动服务,访问测试
    • Hystrix dashboard 断路器仪表盘
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • 启动服务,访问测试
        • 填入 hystrix 的监控端点,开启监控
        • hystrix 熔断
        • 使用 Apache 的并发访问测试工具 ab
      • hystrix 配置
    • hystrix + turbine 集群聚合监控
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • 启动服务,访问测试
    • config 配置中心
      • git 上存放配置文件
        • 新建文件夹,命名为 config
        • 将 sp02,sp03,sp04 三个项目的yml配置文件,复制到 config 文件夹,并改名
        • 禁止配置中心的配置信息覆盖客户端配置
      • 将项目上传到 git
      • 新建 maven 项目
      • pom.xml
      • application.yml
      • 主程序
      • sp02,sp03,sp04 的 pom.xml 添加依赖
      • 启动服务,访问测试

Spring Cloud

Spring 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 dashboardTurbine
    Hystrix 数据监控

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

  • Config
    配置中心

  • Bus
    消息总线, 配置刷新

  • Sleuth + Zipkin
    链路跟踪

Spring Cloud 对比 Dubbo

  • Dubbo

    • Dubbo只是一个远程调用(RPC)框架
    • 默认基于长连接,支持多种序列化格式
  • Spring Cloud
    • 框架集
    • 提供了一整套微服务解决方案(全家桶)
    • 基于http调用,RestAPI

service - 服务

  • 商品服务 item-service,端口8001
  • 用户服务 user-service,端口8101
  • 订单服务 order-service,端口8201

springcloud01 父项目

新建 maven 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><packaging>pom</packaging><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.9.RELEASE</version><relativePath/></parent><groupId>cn.tedu</groupId><artifactId>order-parent</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></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><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>Hoxton.SR11</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>
</project>

删除 src 目录

commons 通用项目

新建 maven 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://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><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>com.fasterxml.jackson.datatype</groupId><artifactId>jackson-datatype-guava</artifactId><version>2.9.8</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</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>

Java 源文件

pojo

Item

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

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

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<Item> items;
}

service

ItemService

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<Item> getItems(String orderId);/*** 减少商品库存*/void decreaseNumber(List<Item> items);
}

UserService

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

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

util

CookieUtil

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

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> 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, 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, 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(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(path, true)) {mapper.writer().writeValues(writer).write(v);writer.flush();} catch (Exception e) {log.error("jackson to file error, path: {}, obj: {}", path, v, e);}}public static String getString(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).toString();} else {return null;}} catch (IOException e) {log.error("jackson get string error, json: {}, key: {}", json, key, e);return null;}}public static Integer getInt(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).intValue();} else {return null;}} catch (IOException e) {log.error("jackson get int error, json: {}, key: {}", json, key, e);return null;}}public static Long getLong(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).longValue();} else {return null;}} catch (IOException e) {log.error("jackson get long error, json: {}, key: {}", json, key, e);return null;}}public static Double getDouble(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).doubleValue();} else {return null;}} catch (IOException e) {log.error("jackson get double error, json: {}, key: {}", json, key, e);return null;}}public static BigInteger getBigInteger(String json, String key) {if (StringUtils.isEmpty(json)) {return new BigInteger(String.valueOf(0.00));}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).bigIntegerValue();} else {return null;}} catch (IOException e) {log.error("jackson get biginteger error, json: {}, key: {}",json, key, e);return null;}}public static BigDecimal getBigDecimal(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).decimalValue();} else {return null;}} catch (IOException e) {log.error("jackson get bigdecimal error, json: {}, key: {}",json, key, e);return null;}}public static boolean getBoolean(String json, String key) {if (StringUtils.isEmpty(json)) {return false;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).booleanValue();} else {return false;}} catch (IOException e) {log.error("jackson get boolean error, json: {}, key: {}", json, key, e);return false;}}public static byte[] getByte(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}try {JsonNode node = mapper.readTree(json);if (null != node) {return node.get(key).binaryValue();} else {return null;}} catch (IOException e) {log.error("jackson get byte error, json: {}, key: {}", json, key, e);return null;}}public static <T> ArrayList<T> getList(String json, String key) {if (StringUtils.isEmpty(json)) {return null;}String string = getString(json, key);return from(string, new TypeReference<ArrayList<T>>() {});}public static <T> String add(String json, String key, T value) {try {JsonNode node = mapper.readTree(json);add(node, key, value);return node.toString();} catch (IOException e) {log.error("jackson add error, json: {}, key: {}, value: {}",json, key, value, e);return json;}}private static <T> void add(JsonNode jsonNode, String key, T value) {if (value instanceof String) {((ObjectNode) jsonNode).put(key, (String) value);} else if (value instanceof Short) {((ObjectNode) jsonNode).put(key, (Short) value);} else if (value instanceof Integer) {((ObjectNode) jsonNode).put(key, (Integer) value);} else if (value instanceof Long) {((ObjectNode) jsonNode).put(key, (Long) value);} else if (value instanceof Float) {((ObjectNode) jsonNode).put(key, (Float) value);} else if (value instanceof Double) {((ObjectNode) jsonNode).put(key, (Double) value);} else if (value instanceof BigDecimal) {((ObjectNode) jsonNode).put(key, (BigDecimal) value);} else if (value instanceof BigInteger) {((ObjectNode) jsonNode).put(key, (BigInteger) value);} else if (value instanceof Boolean) {((ObjectNode) jsonNode).put(key, (Boolean) value);} else if (value instanceof byte[]) {((ObjectNode) jsonNode).put(key, (byte[]) value);} else {((ObjectNode) jsonNode).put(key, to(value));}}public static String remove(String json, String key) {try {JsonNode node = mapper.readTree(json);((ObjectNode) node).remove(key);return node.toString();} catch (IOException e) {log.error("jackson remove error, json: {}, key: {}", json, key, e);return json;}}public static <T> String update(String json, String key, T value) {try {JsonNode node = mapper.readTree(json);((ObjectNode) node).remove(key);add(node, key, value);return node.toString();} catch (IOException e) {log.error("jackson update error, json: {}, key: {}, value: {}", json, key, value, e);return json;}}public static String format(String json) {try {JsonNode node = mapper.readTree(json);return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node);} catch (IOException e) {log.error("jackson format json error, json: {}", json, e);return json;}}public static boolean isJson(String json) {try {mapper.readTree(json);return true;} catch (Exception e) {log.error("jackson check json error, json: {}", json, e);return false;}}private static InputStream getResourceStream(String name) {return JsonUtil.class.getClassLoader().getResourceAsStream(name);}private static InputStreamReader getResourceReader(InputStream inputStream) {if (null == inputStream) {return null;}return new InputStreamReader(inputStream, StandardCharsets.UTF_8);}
}

JsonResult

package cn.tedu.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<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 JsonResult<List<Item>> data(List<Item> items) {this.data = (T) items;return (JsonResult<List<Item>>) this;}public JsonResult<User> data(User user) {this.data = (T) user;return (JsonResult<User>) this;}public JsonResult<Order> data(Order order) {this.data = (T) order;return (JsonResult<Order>) 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);}@Overridepublic String toString() {return JsonUtil.to(this);}
}

item-service 商品服务

  1. 新建项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 配置主程序
  5. 编写代码

新建 maven 项目

pom.xml

  • 要添加 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.0http://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><artifactId>sp02-itemservice</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>cn.tedu</groupId><artifactId>sp01-commons</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>

application.yml

# 在注册中心中注册的服务id(服务名称)
spring:application:name: item-serviceserver:port: 8001

主程序

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 源文件

ItemServiceImpl

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 {@Overridepublic List<Item> getItems(String orderId) {ArrayList<Item> 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;}@Overridepublic void decreaseNumber(List<Item> items) {for (Item item : items) {log.info("减少商品库存:" + item);}}
}

ItemController

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 {@Autowiredprivate ItemService itemService;@Value("${server.port}") // 注入配置的端口号private int port;/*** 获取商品列表,商品列表封装在JSONResult对象中,再返回给客户端* @param orderId* @return*/@GetMapping("/{orderId}")public JsonResult<List<Item>> 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<Item> items = itemService.getItems(orderId);return JsonResult.ok().msg("port=" + port).data(items);}/*** 减少商品库存* @RequestBody 从客户端提交的http协议体数据中接收JSON数据*/@PostMapping("/decreaseNumber")public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){itemService.decreaseNumber(items);return  JsonResult.ok().msg("减少商品库存成功");}
}

Spring MVC 接收参数的几个注解

访问测试

根据 orderId,查询商品 http://localhost:8001/35

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

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

[{"id": 1,"name": "abc","number": 23},{"id": 2,"name": "def","number": 11}
]

user-service 用户服务

  1. 新建项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 配置主程序
  5. 编写代码

新建 maven 项目

pom.xml

  • 要添加 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.0http://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><artifactId>sp03-userservice</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>cn.tedu</groupId><artifactId>sp01-commons</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies>
</project>

application.yml

server:port: 8101spring: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\"}]"

主程序

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 源文件

UserServiceImpl

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;@Overridepublic User getUser(Integer id) {// userJson --> List<User>List<User> list = JsonUtil.from(userJson, new TypeReference<List<User>>() {});for (User user : list) {if (user.getId().equals(id)) {return user;}}// 如果没有找到用户,这里返回一个写死的用户数据return new User(id, "用户名" + id, "密码" + id);}@Overridepublic void addScore(Integer id, Integer score) {log.info("增加用户积分, userId=" + id + ", score=" + score);}
}

UserController

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 {@Autowiredprivate UserService userService;/*** 获取用户*/@GetMapping("{userId}")public JsonResult<User> 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 订单服务

  1. 新建项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 配置主程序
  5. 编写代码

新建 maven 项目

pom.xml

  • 要添加 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.0http://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><artifactId>sp04-orderservice</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>cn.tedu</groupId><artifactId>sp01-commons</artifactId><version>1.0-SNAPSHOT</version></dependency></dependencies></project>

application.yml

spring:application:name: order-serviceserver:port: 8201

主程序

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 源文件

OrderServiceImpl

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 {@Overridepublic Order getOrder(String id) {log.info("获取订单, orderId =" + id);// TODO:远程调用商品获取商品列表// TODO:远程调用用户获取用户数据Order order = new Order();order.setId(id);return order;}@Overridepublic void addOrder(Order order) {log.info("添加订单:" + order);// TODO:远程调用商品,减少商品库存// TODO:远程调用用户,增加用户积分}
}

OrderController

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 {@Autowiredprivate OrderService orderService;/*** 获取订单*/@GetMapping("/{orderId}")public JsonResult<Order> 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 发送以下格式数据:

[{"id": 1,"name": "abc","number": 23},{"id": 2,"name": "def","number": 11}
]

  • user-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 注册与发现

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wrf0JzOj-1639477451459)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210821122452430.png)]

  1. 创建 eureka 项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 主程序启用 eureka 服务器
  5. 启动,访问测试

新建 maven 项目

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-laqLD0TF-1639477451460)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210821122903650.png)]

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://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><artifactId>sp05-eureka</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency></dependencies>
</project>

application.yml

spring:application:name: eureka-serverserver:port: 2001eureka:server:enable-self-preservation: falseinstance:hostname: eureka1client:register-with-eureka: falsefetch-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
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

添加内容

127.0.0.1    eureka1
127.0.0.1   eureka2

启动,并访问测试

  • http://eureka1:2001

service provider 服务提供者

  • 修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器
  1. pom.xml 添加 eureka 依赖
  2. application.yml添加 eureka 注册信息
  3. 启动服务,在 eureka 中查看注册信息

pom.xml 添加 eureka 客户端依赖

利用 EditStarts 添加

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

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml 添加 eureka 注册配置

# 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 中查看注册信息

  • http://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
--server.port=8001

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gtjSfNJC-1639477451466)(https://i.loli.net/2021/11/29/a4tO59TLhUjYQ2F.png)]

  • item-service-8002
--server.port=8002

启动测试

  • 访问 eureka 查看 item-service 注册信息

  • 访问两个端口测试

    http://localhost:8001/35

    http://localhost:8002/35

eureka 高可用

添加两个服务器的 profile 配置文件

application-eureka1.yml

eureka:instance:hostname: eureka1prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}client:fetch-registry: trueregister-with-eureka: trueservice-url:defaultZone: http://eureka2:2002/eureka

application-eureka2.yml

1eureka:instance:hostname: eureka2prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}client:fetch-registry: trueregister-with-eureka: trueservice-url:defaultZone: http://eureka1:2001/eureka

配置启动参数 --spring.profiles.active--server.port

  • 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

访问 eureka 服务器,查看注册信息

  • http://eureka1:2001/

  • http://eureka2:2002/

eureka 客户端注册时,向两个服务器注册

修改以下微服务

  • sp02-itemservice
  • sp03-userservice
  • sp04-orderservice
eureka:client:service-url:defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eureka

当一个 eureka 服务宕机时,仍可以连接另一个 eureka 服务

order-service 调用商品库存服务和用户服务

修改 sp04-orderservice 项目,添加 feign,调用 item-service 和 user-service

  1. pom.xml
  2. 主程序
  3. ItemClient
  4. UserClient
  5. OrderServiceImpl

pom.xml

在父项目的 pom.xml 利用 EditStarts 添加

代码如下:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

主程序

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

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<List<Item>> getItems(@PathVariable("orderId") String orderId);/*** 远程调用商品,减少商品库存* @param items* @return*/@PostMapping("/decreaseNumber")JsonResult<?> decreaseNumber(@RequestBody List<Item> items);
}

UserClient

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<User> getUser(@PathVariable("userId") Integer userId);/*** 远程调用用户,增加用户积分* @param userId* @param score* @return*/@GetMapping("/{userId}/score")JsonResult<?> addScore(@PathVariable("userId") Integer userId,@RequestParam("score") Integer score);
}

OrderServiceImpl

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 {@Autowiredprivate ItemClient itemClient;@Autowiredprivate UserClient userClient;@Overridepublic Order getOrder(String id) {log.info("获取订单, orderId =" + id);// TODO:远程调用商品获取商品列表JsonResult<List<Item>> items = itemClient.getItems(id);// TODO:远程调用用户获取用户数据JsonResult<User> user = userClient.getUser(8);Order order = new Order();order.setId(id);order.setUser(user.getData());order.setItems(items.getData());return order;}@Overridepublic 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 网关

zuul API 网关,为微服务应用提供同一段对外访问接口。

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

新建 sp06-zuul 项目

pom.xml

  • 需要添加 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.0http://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><artifactId>sp06-zuul</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-zuul</artifactId></dependency><dependency><groupId>cn.tedu</groupId><artifactId>sp01-commons</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId></dependency></dependencies>
</project>

application.yml

  • zuul 路由配置可以省略,缺省以服务 id 作为访问路径
server:port: 3001spring:application:name: zuuleureka:client:service-url:defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekainstance:prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-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 注解

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

启动服务,访问测试

  • http://eureka1:2001

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

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

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

    [{"id": 1,"name": "abc","number": 23},{"id": 2,"name": "def","number": 11}
    ]
    
  • http://localhost:3001/user-service/7

  • http://localhost:3001/user-service/7/score?score=100

  • http://localhost:3001/order-service/123abc

  • http://localhost:3001/order-service/

配置 zuul 开启重试,并配置 ribbon 重试参数

  • 需要开启重试,默认不开启
zuul:routes:item-service: /item-service/**user-service: /user-service/**order-service: /order-service/**# 启用重试retryable: trueribbon:ConnectTimeout: 1000ReadTimeout: 1000MaxAutoRetriesNextServer: 1MaxAutoRetries: 1# 针对一个指定的后台服务,来配置重试参数
item-service:ribbon:MaxAutoRetries: 0# 暴露actuator的所有监控数据
management:endpoints:web:exposure:include: "*"

zuul 降级

创建降级类

  • getRoute()方法中指定应用降级类的服务 id,星号或 null 值可以通配所有服务
ItemFallback
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*/@Overridepublic String getRoute() {return "item-service";}/*** 发送给客户端的降级结果,封装在response对象中* @param route* @param cause* @return*/@Overridepublic ClientHttpResponse fallbackResponse(String route, Throwable cause) {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR;}@Overridepublic int getRawStatusCode() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR.value();}@Overridepublic String getStatusText() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();}/*** 用来关闭流* ByteArrayInputStream不需要关闭*/@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {String json = JsonResult.err().code(500).msg("调用商品服务失败").toString();return new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));}@Overridepublic HttpHeaders getHeaders() {// Content-Type:application/json;charset=UTF-8HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Content-Type", "application/json;charset=UTF-8");return httpHeaders;}};}
}
OrderFallback
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*/@Overridepublic String getRoute() {return "order-service";}/*** 发送给客户端的降级结果,封装在response对象中* @param route* @param cause* @return*/@Overridepublic ClientHttpResponse fallbackResponse(String route, Throwable cause) {return new ClientHttpResponse() {@Overridepublic HttpStatus getStatusCode() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR;}@Overridepublic int getRawStatusCode() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR.value();}@Overridepublic String getStatusText() throws IOException {return HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase();}/*** 用来关闭流* ByteArrayInputStream不需要关闭*/@Overridepublic void close() {}@Overridepublic InputStream getBody() throws IOException {String json = JsonResult.err().code(500).msg("调用订单服务失败").toString();return new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));}@Overridepublic HttpHeaders getHeaders() {// Content-Type:application/json;charset=UTF-8HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.add("Content-Type", "application/json;charset=UTF-8");return httpHeaders;}};}
}

LoginFilter

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*/@Overridepublic String filterType() {return FilterConstants.PRE_TYPE;}/*** 位置顺序号,zuul的前置过滤器默认有5个* 其中第5个过滤器中,在上下文对象中放入了serviceId* 在后面过滤器中,才能使用serviceId* @return*/@Overridepublic int filterOrder() {return 6;}/*** 判断针对当前请求,是否要执行过滤代码* @return*/@Overridepublic boolean shouldFilter() {/*调用item-service,需要检查权限调用其他服务不判断权限,可以直接访问*/// 获取当前请求调用的服务idRequestContext currentContext = RequestContext.getCurrentContext();String serviceId = (String) currentContext.get(FilterConstants.SERVICE_ID_KEY);// 判断id是不是item-servicereturn "item-service".equalsIgnoreCase(serviceId);}/*** 过滤代码* @return* @throws com.netflix.zuul.exception.ZuulException*/@Overridepublic 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-8currentContext.addZuulRequestHeader("Content-Type","application/json;charset=UTF-8");currentContext.setResponseBody(json);}// 在当前zuul版本中,没有使用这个返回值return null;}
}

启动服务,访问测试

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

  • http://localhost:3001/actuator/hystrix.stream

Hystrix dashboard 断路器仪表盘

Hystrix 对请求的降级和熔断,可以产生监控信息,hystrix dashboard 可以实时的进行监控

  1. 新建 maven 项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 主程序启用 eureka 服务器
  5. 启动,访问测试

新建 maven 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://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><artifactId>sp07-hystrix-dashboard</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies>
</project>

application.yml

server:port: 4001spring:application:name: hystrix-dashboard# 允许抓取日志的服务器列表
hystrix:dashboard:proxy-stream-allow-list: localhosteureka:client:service-url:defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekainstance:prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}

主程序

  • 添加 @EnableHystrixDashBoard
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);}
}

启动服务,访问测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5c5TpzIn-1639477451491)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210825234115204.png)]

http://localhost:4001/hystrix

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-U9tp3FU2-1639477451491)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210825234241481.png)]

填入 hystrix 的监控端点,开启监控

  • http://localhost:3001/actuator/hystrix.stream

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aDciwEv3-1639477451492)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210826120138637.png)]

  • 通过 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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FdeDRMbf-1639477451492)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210826121033480.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GNX4qiZW-1639477451493)(Spring%20Cloud%20%E5%85%A5%E9%97%A8%E6%89%8B%E5%86%8C.assets/image-20210826121212474.png)]

hystrix 熔断

整个链路达到一定的阈值,默认情况下,10秒内产生超过20次请求,则符合第一个条件。

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

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

使用 Apache 的并发访问测试工具 ab

http://httpd.apache.org/docs/current/platform/windows.html#down

  • 用 ab 工具,以并发50次,来发送20000个请求
ab -n 20000 -c 50 http://localhost:3001/item-service/35?token=2sswbbbbw
  • 断路器为 open,所有请求都会短路,直接降级执行 fallback 方法

hystrix 配置

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 集群聚合监控

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

新建 maven 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://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><artifactId>sp08-turbine</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-turbine</artifactId></dependency></dependencies>
</project>

application.yml

spring:application:name: turbineserver:port: 5001eureka:instance:prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-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: zuulcluster-name-expression: new String("default")

主程序

  • 添加 @EnableTurbine 注解
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监控信息

config 配置中心

yml 配置文件保存到 git 服务器,例如 github.com或者gitee.com

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

git 上存放配置文件

新建文件夹,命名为 config

将 sp02,sp03,sp04 三个项目的yml配置文件,复制到 config 文件夹,并改名

  • item-service-dev.yml
  • user-service-dev.yml
  • order-service-dev.yml

最后注释掉三个项目中的 application.yml 文件

禁止配置中心的配置信息覆盖客户端配置

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

item-service 启动参数

  • --server.port=8081
  • --server.port=8082

我们可以设置禁止配置中心的配置将客户端配置覆盖掉

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

spring:# 让配置中心的配置,不覆盖项目的本地配置和命令参数cloud:config:override-none: true

将项目上传到 git

新建 maven 项目

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://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><artifactId>sp09-config</artifactId><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>
</project>

application.yml

server:port: 6001spring:application:name: config-server# 连接git仓库,在指定目录下找到配置文件cloud:config:server:git:uri: https://gitee.com/Jasonakeke/CGBVProjectssearch-paths: codes/springcloud1/configeureka:instance:prefer-ip-address: true# 界面列表中显示的格式也显示ipinstance-id: ${spring.cloud.client.ip-address}:${spring.application.name}:${server.port}client:service-url:defaultZone: http://eureka1:2001/eureka, http://eureka2:2002/eurekamanagement:endpoints:web:exposure:include: bus-refresh

主程序

  • 添加 @EnableConfigServer 注解
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 添加依赖

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId>
</dependency>

启动服务,访问测试

先启动 sp05-eureka,再启动 sp09-config,最后启动 sp02-itemservice, sp03-userservice, sp04-orderservice

Spring Cloud 入门手册相关推荐

  1. Spring Cloud 入门 之 Config 篇(六)

    一.前言 随着业务的扩展,为了方便开发和维护项目,我们通常会将大项目拆分成多个小项目做成微服务,每个微服务都会有各自配置文件,管理和修改文件起来也会变得繁琐.而且,当我们需要修改正在运行的项目的配置时 ...

  2. Spring Cloud 入门 之 Zuul 篇(五)

    一.前言 随着业务的扩展,微服务会不对增加,相应的其对外开放的 API 接口也势必增多,这不利于前端的调用以及不同场景下数据的返回,因此,我们通常都需要设计一个 API 网关作为一个统一的 API 入 ...

  3. Spring Cloud 入门 之 Hystrix 篇(四)

    一.前言 在微服务应用中,服务存在一定的依赖关系,如果某个目标服务调用慢或者有大量超时造成服务不可用,间接导致其他的依赖服务不可用,最严重的可能会阻塞整条依赖链,最终导致业务系统崩溃(又称雪崩效应). ...

  4. Spring Cloud 入门 之 Feign 篇(三)

    一.前言 在上一篇文章<Spring Cloud 入门 之 Ribbon 篇(二)> 中介绍了 Ribbon 使用负载均衡调用微服务,但存在一个问题:消费端每个请求方法中都需要拼接请求服务 ...

  5. Spring Cloud 入门 之 Ribbon 篇(二)

    一.前言 上一篇<Spring Cloud 入门 之 Eureka 篇(一)> 介绍了微服务的搭建,服务注册与发现.但在文章中留了一个小尾巴--如何正确使用 Eureka 进行服务发现并调 ...

  6. Spring Cloud 入门 之 Eureka 篇(一)

    一.前言 Spring Cloud 是一系列框架的有序集合.它利用 Spring Boot 的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据 ...

  7. Spring Cloud入门教程(二):客户端负载均衡(Ribbon)

    对于大型应用系统负载均衡(LB:Load Balancing)是首要被解决一个问题.在微服务之前LB方案主要是集中式负载均衡方案,在服务消费者和服务提供者之间又一个独立的LB,LB通常是专门的硬件,如 ...

  8. Spring Cloud入门-Admin服务监控中心(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Spring Boot Admin 简介 创建admin-server模块 创建admin-client模块 监控信息演示 结合注册中心使用 修改 ...

  9. Spring Cloud入门-Sentinel实现服务限流、熔断与降级(Hoxton版本)

    文章目录 Spring Cloud入门系列汇总 摘要 Sentinel简介 安装Sentinel控制台 创建sentinel-service模块 限流功能 创建RateLimitController类 ...

  10. Spring Cloud 入门教程(二): 配置管理

    使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们与Spring ...

最新文章

  1. pythonif语句缩进_Python中多行IF语句缩进的代码样式?
  2. 给IC 应届毕业生的 忠告【转载】
  3. 10-穿墙代理的设置 | 01.数据抓取 | Python
  4. C#中的DBNull、Null、和String.Empty解释
  5. 华为手机云闪付付款码如何截图_云闪付乘车码,它带着优惠又来了
  6. linux安装gtk命令,Ubuntu 下安装 GTK2.0
  7. 收拾老家发现的老版纸币,现在还能用吗?
  8. 前端aes加密_前端安全攻防解析
  9. rest syntax(parameters)
  10. 服务应用突然宕机了?别怕,Dubbo 帮你自动搞定服务隔离!
  11. win7简单命令让PC变身无线热点,共享M9上网
  12. 蓝牙iBeacon工作原理
  13. 免费的WinCC语音报警控件
  14. php计算面积,PHP中长方形的面积怎么求
  15. IDEA常用插件、设置、注释
  16. ChatGPT 账号被封?推荐 5 款国外网站,完美替代!
  17. 【20210416期AI简报】微软分层ViT模型开源、 DIY一只“眼睛”摄像头
  18. linux开通本地ip连接,SSH 连接本地虚拟机 Linux
  19. 搭建ZeroTier的Moon服务器
  20. 联想笔记本打开虚拟机服务器,联想thinkpad笔记本如何开启虚拟化技术

热门文章

  1. 单相PWM整流器工作原理与调制方向判断
  2. 计算机用户删除了怎么恢复出厂设置,四种电脑快速恢复出厂设置方法
  3. 故障解决:端口已被占用 1080
  4. 软件定义无线电的实时频谱分析仪相关原理介绍(二)——射频,中频和基带信号
  5. 下载iPhone APP软件历史版本教程
  6. 数字图像处理期末复习总结
  7. asp.net core部署在docker容器中运行
  8. 计算机焊板原理,计算机组成原理电路板焊接(74LS373和74LS138).doc
  9. 关于CIE RGB色坐标图转换到CIE XYZ色坐标图
  10. 网络管理与维护基本知识