spring cloud是一系列框架的汇合。它利用spring boot的开发便利性奇妙地简化了分布式系统基础设施的开发,如服务发现注册、配置核心、音讯总线、负载平衡、断路器、数据监控等,都能够用spring boot 的开发格调做到一键启动和部署。
spring cloud并没有反复制作轮子,它只是将目前各家公司开发的比拟成熟、经得起理论考验的服务框架组合起来,通过spring boot格调进行再封装屏蔽掉了简单的配置和实现原理,最终给开发者留出了一套简略易懂、易部署和易保护的分布式系统开发工具包。
spring cloud 对于中小型互联网公司来说是一种福音,因为这类公司往往没有实力或者没有足够的资金投入去开发本人的分布式系统基础设施,应用spring cloud一站式解决方案能在从容应对业务倒退的同时大大减少开发成本。同时,随着近几年微服务架构和docker容器概念的火爆,也会让spring cloud 在将来越来越“云”化的软件开发格调中立有一席之地,尤其是在目前形形色色的分布式解决方案中提供了标准化的、一站式的技术计划,意义可能会堪比当年servlet标准的诞生,无效推动服务端软件系统技术水平的提高。
spring cloud
Eureka----注册核心
Cinfig----配置核心&默认Git存储
Bus----音讯总线&动静配置刷新
Ribbon----负载平衡&重试
Hystrix----降级&熔断
Feign----申明式客户端&集成Ribbon&集成Hystrix
Zuul----API网关&过滤器&集成Ribbon&集成Hystrix
Hystrix dashboard----Hystrix仪表盘
Turbine----聚合Hystrix监控数据
Sleuth+Zipkin----链路跟踪
Spring Cloud 比照 Dubbo
Dubbo----Dubbo只是一个近程调用(RPC)框架,默认基于长连贯,反对多种序列化格。
Spring Cloud
Spring Cloud----框架集,提供了一整套微服务解决方案(全家桶),基于http调用,Rest API
一、service-服务
- 商品服务 item service,端口 8001
- 用户服务 user service,端口 8101
- 订单服务 order service,端口 8201
二、commons通用我的项目
新建maven我的项目
减少依赖--pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.tedu</groupId> <artifactId>sp01-commons</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp01-commons</name> <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.6</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> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build></project>
pojo
Item
package cn.tedu.sp01.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Item{ private Integer id;//id private String name;//名称 private Integer number;//数量}
User
package cn.tedu.sp01.pojo;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class User { private Integer id; //id private String username; //用户名 private String password; //用户明码}
order
package cn.tedu.sp01.pojo;import java.util.List;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;@Data@NoArgsConstructor@AllArgsConstructorpublic class Order { private String id;//id private User user;//用户 private List<Item> items;//缩小商品库存&减少用户积分}
service
ItemService
package cn.tedu.sp01.service;import java.util.List;import cn.tedu.sp01.pojo.Item;public interface ItemService{ List<Item> getItems(String orderId); void decreaseNumbers(List<Item> list);}
UserService
package cn.tedu.sp01.service;import cn.tedu.sp01.pojo.User;public interface UserService{ User getUser(Integer id); void addScore(Integer id,Integer score);}
OrderService
package cn.tedu.sp01.service;import cn.tedu.sp01.pojo.Order;public interface OrderService{ Order getOrder(String orderId); void addOrder(Order order);//保留商品}
util
CookieUtil
package cn.tedu.web.util;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class CookieUtil { /** * @param response * @param name * @param value * @param maxAge */ public static void setCookie(HttpServletResponse response, String name, String value, String domain, String path, int maxAge) { Cookie cookie = new Cookie(name, value); if(domain != null) { cookie.setDomain(domain); } cookie.setPath(path); cookie.setMaxAge(maxAge); response.addCookie(cookie); } public static void setCookie(HttpServletResponse response, String name, String value, int maxAge) { setCookie(response, name, value, null, "/", maxAge); } public static void setCookie(HttpServletResponse response, String name, String value) { setCookie(response, name, value, null, "/", 3600); } public static void setCookie(HttpServletResponse response, String name) { setCookie(response, name, "", null, "/", 3600); } /** * @param request * @param name * @return */ public static String getCookie(HttpServletRequest request, String name) { String value = null; Cookie[] cookies = request.getCookies(); if (null != cookies) { for (Cookie cookie : cookies) { if (cookie.getName().equals(name)) { value = cookie.getValue(); } } } return value; } /** * @param response * @param name * @return */ public static void removeCookie(HttpServletResponse response, String name, String domain, String path) { setCookie(response, name, "", domain, path, 0); }}
JsonUtil
package cn.tedu.web.util;import java.io.File;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Writer;import java.math.BigDecimal;import java.math.BigInteger;import java.net.URL;import java.nio.charset.StandardCharsets;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.List;import org.apache.commons.lang3.StringUtils;import com.fasterxml.jackson.annotation.JsonInclude;import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.core.type.TypeReference;import com.fasterxml.jackson.databind.DeserializationFeature;import com.fasterxml.jackson.databind.JsonNode;import com.fasterxml.jackson.databind.ObjectMapper;import com.fasterxml.jackson.databind.SerializationFeature;import com.fasterxml.jackson.databind.node.ObjectNode;import com.fasterxml.jackson.datatype.guava.GuavaModule;import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class JsonUtil { private static ObjectMapper mapper; private static JsonInclude.Include DEFAULT_PROPERTY_INCLUSION = JsonInclude.Include.NON_DEFAULT; private static boolean IS_ENABLE_INDENT_OUTPUT = false; private static String CSV_DEFAULT_COLUMN_SEPARATOR = ","; static { try { initMapper(); configPropertyInclusion(); configIndentOutput(); configCommon(); } catch (Exception e) { log.error("jackson config error", e); } } private static void initMapper() { mapper = new ObjectMapper(); } private static void configCommon() { config(mapper); } private static void configPropertyInclusion() { mapper.setSerializationInclusion(DEFAULT_PROPERTY_INCLUSION); } private static void configIndentOutput() { mapper.configure(SerializationFeature.INDENT_OUTPUT, IS_ENABLE_INDENT_OUTPUT); } private static void config(ObjectMapper objectMapper) { objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN); objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT); objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY); objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY); objectMapper.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS); objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS); objectMapper.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII); objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN); objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES); objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")); objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES); objectMapper.registerModule(new ParameterNamesModule()); objectMapper.registerModule(new Jdk8Module()); objectMapper.registerModule(new JavaTimeModule()); objectMapper.registerModule(new GuavaModule()); } public static void setSerializationInclusion(JsonInclude.Include inclusion) { DEFAULT_PROPERTY_INCLUSION = inclusion; configPropertyInclusion(); } public static void setIndentOutput(boolean isEnable) { IS_ENABLE_INDENT_OUTPUT = isEnable; configIndentOutput(); } public static <V> V from(URL url, Class<V> c) { try { return mapper.readValue(url, c); } catch (IOException e) { log.error("jackson from error, url: {}, type: {}", url.getPath(), c, e); return null; } } public static <V> V from(InputStream inputStream, Class<V> c) { try { return mapper.readValue(inputStream, c); } catch (IOException e) { log.error("jackson from error, type: {}", c, e); return null; } } public static <V> V from(File file, Class<V> c) { try { return mapper.readValue(file, c); } catch (IOException e) { log.error("jackson from error, file path: {}, type: {}", file.getPath(), c, e); return null; } } public static <V> V from(Object jsonObj, Class<V> c) { try { return mapper.readValue(jsonObj.toString(), c); } catch (IOException e) { log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), c, e); return null; } } public static <V> V from(String json, Class<V> c) { try { return mapper.readValue(json, c); } catch (IOException e) { log.error("jackson from error, json: {}, type: {}", json, c, e); return null; } } public static <V> V from(URL url, TypeReference<V> type) { try { return mapper.readValue(url, type); } catch (IOException e) { log.error("jackson from error, url: {}, type: {}", url.getPath(), type, e); return null; } } public static <V> V from(InputStream inputStream, TypeReference<V> type) { try { return mapper.readValue(inputStream, type); } catch (IOException e) { log.error("jackson from error, type: {}", type, e); return null; } } public static <V> V from(File file, TypeReference<V> type) { try { return mapper.readValue(file, type); } catch (IOException e) { log.error("jackson from error, file path: {}, type: {}", file.getPath(), type, e); return null; } } public static <V> V from(Object jsonObj, TypeReference<V> type) { try { return mapper.readValue(jsonObj.toString(), type); } catch (IOException e) { log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), type, e); return null; } } public static <V> V from(String json, TypeReference<V> type) { try { return mapper.readValue(json, type); } catch (IOException e) { log.error("jackson from error, json: {}, type: {}", json, type, e); return null; } } public static <V> String to(List<V> list) { try { return mapper.writeValueAsString(list); } catch (JsonProcessingException e) { log.error("jackson to error, obj: {}", list, e); return null; } } public static <V> String to(V v) { try { return mapper.writeValueAsString(v); } catch (JsonProcessingException e) { log.error("jackson to error, obj: {}", v, e); return null; } } public static <V> void toFile(String path, List<V> list) { try (Writer writer = new FileWriter(new File(path), true)) { mapper.writer().writeValues(writer).writeAll(list); writer.flush(); } catch (Exception e) { log.error("jackson to file error, path: {}, list: {}", path, list, e); } } public static <V> void toFile(String path, V v) { try (Writer writer = new FileWriter(new File(path), true)) { mapper.writer().writeValues(writer).write(v); writer.flush(); } catch (Exception e) { log.error("jackson to file error, path: {}, obj: {}", path, v, e); } } public static String getString(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).toString(); } else { return null; } } catch (IOException e) { log.error("jackson get string error, json: {}, key: {}", json, key, e); return null; } } public static Integer getInt(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).intValue(); } else { return null; } } catch (IOException e) { log.error("jackson get int error, json: {}, key: {}", json, key, e); return null; } } public static Long getLong(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).longValue(); } else { return null; } } catch (IOException e) { log.error("jackson get long error, json: {}, key: {}", json, key, e); return null; } } public static Double getDouble(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).doubleValue(); } else { return null; } } catch (IOException e) { log.error("jackson get double error, json: {}, key: {}", json, key, e); return null; } } public static BigInteger getBigInteger(String json, String key) { if (StringUtils.isEmpty(json)) { return new BigInteger(String.valueOf(0.00)); } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).bigIntegerValue(); } else { return null; } } catch (IOException e) { log.error("jackson get biginteger error, json: {}, key: {}", json, key, e); return null; } } public static BigDecimal getBigDecimal(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).decimalValue(); } else { return null; } } catch (IOException e) { log.error("jackson get bigdecimal error, json: {}, key: {}", json, key, e); return null; } } public static boolean getBoolean(String json, String key) { if (StringUtils.isEmpty(json)) { return false; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).booleanValue(); } else { return false; } } catch (IOException e) { log.error("jackson get boolean error, json: {}, key: {}", json, key, e); return false; } } public static byte[] getByte(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } try { JsonNode node = mapper.readTree(json); if (null != node) { return node.get(key).binaryValue(); } else { return null; } } catch (IOException e) { log.error("jackson get byte error, json: {}, key: {}", json, key, e); return null; } } public static <T> ArrayList<T> getList(String json, String key) { if (StringUtils.isEmpty(json)) { return null; } String string = getString(json, key); return from(string, new TypeReference<ArrayList<T>>() {}); } public static <T> String add(String json, String key, T value) { try { JsonNode node = mapper.readTree(json); add(node, key, value); return node.toString(); } catch (IOException e) { log.error("jackson add error, json: {}, key: {}, value: {}", json, key, value, e); return json; } } private static <T> void add(JsonNode jsonNode, String key, T value) { if (value instanceof String) { ((ObjectNode) jsonNode).put(key, (String) value); } else if (value instanceof Short) { ((ObjectNode) jsonNode).put(key, (Short) value); } else if (value instanceof Integer) { ((ObjectNode) jsonNode).put(key, (Integer) value); } else if (value instanceof Long) { ((ObjectNode) jsonNode).put(key, (Long) value); } else if (value instanceof Float) { ((ObjectNode) jsonNode).put(key, (Float) value); } else if (value instanceof Double) { ((ObjectNode) jsonNode).put(key, (Double) value); } else if (value instanceof BigDecimal) { ((ObjectNode) jsonNode).put(key, (BigDecimal) value); } else if (value instanceof BigInteger) { ((ObjectNode) jsonNode).put(key, (BigInteger) value); } else if (value instanceof Boolean) { ((ObjectNode) jsonNode).put(key, (Boolean) value); } else if (value instanceof byte[]) { ((ObjectNode) jsonNode).put(key, (byte[]) value); } else { ((ObjectNode) jsonNode).put(key, to(value)); } } public static String remove(String json, String key) { try { JsonNode node = mapper.readTree(json); ((ObjectNode) node).remove(key); return node.toString(); } catch (IOException e) { log.error("jackson remove error, json: {}, key: {}", json, key, e); return json; } } public static <T> String update(String json, String key, T value) { try { JsonNode node = mapper.readTree(json); ((ObjectNode) node).remove(key); add(node, key, value); return node.toString(); } catch (IOException e) { log.error("jackson update error, json: {}, key: {}, value: {}", json, key, value, e); return json; } } public static String format(String json) { try { JsonNode node = mapper.readTree(json); return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(node); } catch (IOException e) { log.error("jackson format json error, json: {}", json, e); return json; } } public static boolean isJson(String json) { try { mapper.readTree(json); return true; } catch (Exception e) { log.error("jackson check json error, json: {}", json, e); return false; } } private static InputStream getResourceStream(String name) { return JsonUtil.class.getClassLoader().getResourceAsStream(name); } private static InputStreamReader getResourceReader(InputStream inputStream) { if (null == inputStream) { return null; } return new InputStreamReader(inputStream, StandardCharsets.UTF_8); }}
三、item service 商品服务
- 新建我的项目
- 配置依赖 pom.xml
- 配置 application.yml
- 配置主程序
- 编写代码
新建 spring boot 起步我的项目
抉择依赖项--只选web
pom.xml--sp01commons我的项目依赖
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.1.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.tedu</groupId> <artifactId>sp02-itemservice</artifactId> <version>0.0.1-SNAPSHOT</version> <name>sp02-itemservice</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>cn.tedu</groupId> <artifactId>sp01-commons</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build></project>
application.properties---改为application.yml
spring: application: name: item-serviceserver: port:8001
主程序默认代码--不须要批改
ItemServiceImpl
package cn.tedu.sp02.item.service;import java.util.ArrayList;import java.util.List;import org.springframework.stereotype.Service;import cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.service.ItemService;import lombok.extern.slf4j.Slf4j;@Slf4j@Servicepublic class ItemServiceImpl implements ItemService {@Override public List<Item> getItems(String orderId) { ArrayList<Item> list = new ArrayList<Item>(); 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 decreaseNumbers(List<Item> list){ for(Item item: list){ log.info("缩小库存-"+item)}}}
ItemController
package cn.tedu.sp02.item.controller;import java.util.List;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 cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.service.ItemService;import cn.tedu.web.util.JsonResult;import lombok.extern.slf4j.Slf4j;@Slf4j@RestControllerpublic class ItemController { @Autowired private ItemService itemService; @Value("${server.port}") private int port; @GetMapping("/{orderId}") public JsonResult<List<Item>> getItems(@PathVariable String orderId) { log.info("server.port="+port+", orderId="+orderId); List<Item> items = itemService.getItems(orderId); return JsonResult.ok(items).msg("port="+port); } @PostMapping("/decreaseNumber") public JsonResult decreaseNumber(@RequestBody List<Item> items) { itemService.decreaseNumbers(items); return JsonResult.ok(); }}