微服务实践之路

Posted by NotGeek on December 11, 2018

微服务实践之路

议程

  • What
  • Why
  • How
  • Q&A

What

  • 微服务是什么?
  • SOA又是什么?
  • SOA有什么?
  • SOA不是什么?
  • Monolithic是什么?

微服务是什么?

​ 微服务是一种细粒度的SOA。

软件设计原则:开闭原则,

细粒度,粗粒度,整体职责划分清楚。

职责范围越来越细

  • 分包
    • 分模块
      • model
        • 分布式

分而治之。逐渐地分出更明确的东西。

技术怎么应用于业务,没有人告诉你,没人教会你划分你的结构

划分结构

SOA是什么?

SOA是面向服务的架构

SOA是解决冗余的问题

SOA有什么?

特征

  • 面向服务(Service-Oriented)
  • 松耦合(Loose-Coupling)
  • 模块化(Modular)
  • 分布式计算(Distributed Computing)
  • 平台无关性(Independent PlatForm)
  • 集中管理(Center Government)

实现业务

为什么面向服务?

以为是分布式计算。

单进程,方法和方法调用生成字节码,是字节码和字节码之间的调用。

臃肿 就是我问你要一块钱,你给我一个钱包,还不告诉我,它在那个口袋。

服务治理:就是SOA中的集中管理

SOA:2000年的时候都提出标准了。

人在无能的时候喜欢用名字来进行包装。

面向服务

服务要小。

尽量让客户端尽量的小。最好方式,Integer,String 很松散的耦合。

很松散地耦合。

分布式计算:

  • 对称模式

  • 不对称模式(CS)。

服务的职责性。

平台无关性

系统,协议,语言无关

SOA有什么

代表技术

  • Web Services
  • Message Queue
  • ESB
同步异步

RPC 同步

  • Message Queue

MQ 是异步

  • ESB
  1. 同步是强依赖, 1 + 1 你要立马给我。
  2. 异步是弱依赖,提高了吞吐量。24*7
    • Reactive、Eventing sourcing都是流式计算,都是奇技淫巧。
    • 形而上学,才是好同学
面试时问你,你们一次请求多长时间

该计算多长时间就需要多长时间。我解决不了这个问题,后台优化可以解决这个问题。但吞吐量我可以用异步的方式去提高。

Tomcat,netty最大的线程数

  • QPS—–10
  • QPS—–20

一个tomcat假如说,一秒10个请求,然后采用异步,我一秒可以处理200个请求,但是请求的结果不能够及时的返回。

最终一致性是利用MQ来是实现的。
ESB是企业服务总线。

SOA不是什么?

​ SOA 不等于 Monolithic(巨石系统)

Monolithic 是什么

1544505775116

Why

  • 单体,稳定。
  • 服务治理,不太稳定。

为什么要用微服务?

  • 效率的需要
  • 质量的需要
  • 稳定的需要
  • 运维的需要
  • 成长的需要

  • WebService为什么不行?

    不是不行, WSDL

    太臃肿了。

    基于文本格式协议,非常笨重。

  • Ajax:Async JavaScript And Xml

    现在 ajax 不是 XML 因为 XML太重了!!!

    现在 是 Ajj

  • Rest也是文本协议,

    序列化和反序列化也很麻烦。

稳定,

不要一个功能影响到其它功能。

DevOps开发懂运维

中间件
  • activitemq
  • Kafka

为什么不必微服务?

  • 场景单一

  • 逻辑简单
  • 业务渐逝
  • “老成持重”(收付款,你拿微服务改造,老板会拿你开刀。有些行业稳定大于一切
  • 技术盲从

简单的服务,你用 微服务 有什么而用呢。

K8S:

  • 能力够不够

  • 心态够不够

怎么实现微服务

  • 心态
  • 技术
  • 思想

心态

  • “子路有闻,未之能行,唯恐有闻”

  • “不患无位,患所以立”

  • “攻乎异端,斯害也已”

  • “过则无惮改”

技术

  • 虚拟化技术(Docker等)

  • 方法论(DDD、EIP)

  • 中间件(Java EE、Spring Boot、Spring Cloud等)

DDD

  • 有界上下文(Bounded Context)

  • 持续集成(Continuous integration)

  • 上下文映射(Context Map)

JSR 规范

How

  • 虚拟化技术(Docker 等)
  • 方法论(DDD、EIP)
  • 中间件(Java EE 、Spring Boot 、Jfinal)

中间件

  • Java EE
  • Spring Boot
  • Jfinal

思想:

  • 少谈 “ 敏捷

  • 推崇 “ 简洁

  • 学习 “ 狄仁杰

源码一般用多态避免

if else

​ 单一的职责。

闲谈思想

  • 英雄不问出处,流氓不问岁数。
  • 程序员高智商,低情商
面试怎么看
  • 20看技术
  • 30看沟通技巧

  • 人要突破主观上的观念是很难的。
    • “文人相亲不要相轻”

#####

外部化配置

(spring Boot 2.0.1 release)
  1. Externalized Configuration

    Spring Boot lets you externalize your configuration so that you can work with the same application code in different environments. You can use properties files, YAML files, environment variables, and command-line arguments to externalize configuration. Property values can be injected directly into your beans by using the @Value annotation, accessed through Spring’s Environment abstraction, or be bound to structured objects through @ConfigurationProperties.

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overriding of values. Properties are considered in the following order:

Spring引导允许您外部化配置,以便可以使用相同的应用程序不同环境中的代码。您可以使用属性文件、YAML文件、环境变量和用于外部化配置的命令行参数。属性值可以直接注入到您的通过使用@Value注释(通过Spring的环境抽象访问)或be来访问bean通过@ConfigurationProperties绑定到结构化对象。

Spring引导使用一个非常特殊的PropertySource顺序,该顺序被设计为允许合理的覆盖的值。属性的考虑顺序如下:

  1. Devtools global settings properties on your home directory (~/.spring-bootdevtools. properties when devtools is active).
  2. @TestPropertySource annotations on your tests.
  3. @SpringBootTest#properties annotation attribute on your tests.
  4. Command line arguments.
  5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
  6. ServletConfig init parameters.
  7. ServletContext init parameters.
  8. JNDI attributes from java:comp/env.
  9. Java System properties (System.getProperties()). 10.OS environment variables. 11.A RandomValuePropertySource that has properties only in random.*. 12.Profile-specific application properties outside of your packaged jar (application- {profile}.properties and YAML variants). 13.Profile-specific application properties packaged inside your jar (application- {profile}.properties and YAML variants). 14.Application properties outside of your packaged jar (application.properties and YAML variants). 15.Application properties packaged inside your jar (application.properties and YAML variants). 16.@PropertySource annotations on your @Configuration classes. 17.Default properties (specified by setting SpringApplication.setDefaultProperties).

org.springframework.beans.factory.annotation.Value

@Value 是 Spring 中的注解

@TestPropertySource 也是 Spring 中的注解

​ 你可以用相同的代码程序在不同的环境。

Spring Boot 1.5 以后 管理关闭

1
management.security.enabled=false

Spring Boot 2.x 以后:

1
management.endpoints.web.exposure.include=*
1
2
# 随机端口
server.port=0
@value
1
2
3
4
5
6
@Data
@Component
public class Person {
    @Value("${person.name}")
    private String name;
}
@ConfigurationProperties

技术的选型要恰到好处

比如 propertiesyml 的,都可以,只不过,哪个简单用哪个。

医生治病,把你治个半死,再把你病治好,又有什么用呢?

设计要有技巧

  • 先有 Spring
    • 再有 Spring Boot
  • 先有 ServletConfig WEB-INF/servlet.xml
    • 在 ServletContext
  • 先有 System java.lang.System
    • 在 系统的环境变量 environment

使用的范围越大越有限,离你的系统越近越有限。

java.lang.System
1
2
3
4
5
6
7
8
9
// 系统的环境变量
public static String getenv(String name) {
    SecurityManager sm = getSecurityManager();
    if (sm != null) {
        sm.checkPermission(new RuntimePermission("getenv."+name));
    }

    return ProcessEnvironment.getenv(name);
}

org.springframework.boot.test.context.ConfigFileApplicationContextInitializer

  • org.springframework.boot.test.context.ConfigFileApplicationContextInitializer#initialize
    • org.springframework.boot.context.config.ConfigFileApplicationListener#addPropertySources
1
2
3
4
5
6
7
public class YamlPropertySourceLoader implements PropertySourceLoader {
    public YamlPropertySourceLoader() {
    }

    public String[] getFileExtensions() {
        return new String[]{"yml", "yaml"};
    }
1
2
3
4
5
6
7
8
9
public class PropertiesPropertySourceLoader implements PropertySourceLoader {
    private static final String XML_FILE_EXTENSION = ".xml";

    public PropertiesPropertySourceLoader() {
    }

    public String[] getFileExtensions() {
        return new String[]{"properties", "xml"};
    }

yml yaml properties xml 都可以。

Spring 简化了很多的 JAVA 开发。提供了很多便捷的东西。

propertiesLoader 可以自己实现 #loader(...) 方法。

​ 凡事都是由原因的,这个原因体现在源代码的实现,所有的实现都是最终由代码去承载的。你会发现,用单一的原则,国外的人从不用 If else 他们用 多态,定义一个接口,用不同的实现取代 if else ,在很多开源框架中都是这样子的。

org.springframework.core.io.Resource

  • classpass...
  • Filename....
  • Byte...

​ 外国一般用 for循环 支持就支持,不支持就不支持。

技巧只是一种帮助。 从本质入手。

​ 有些人大学没毕业,代码还是写的挺 6 的。偏听则暗,兼听则明。

专科 ,知耻近乎勇。

​ 要逼迫一下自己也挺好的。

手淫互联网,意淫大数据。

大数据:存储和计算

开发工具不重要

Spring Boot 不存在从零开始。

要学一点文科的东西。

  1. Logging

    Spring Boot uses Commons Logging for all internal logging but leaves the underlying log implementation open. Default configurations are provided for Java Util Logging, Log4J2, and Logback. In each case, loggers are pre-configured to use console output with optional file output also available.

他都是假设你什么都会, 然后 Spring Boot 文档也很少。

什么场景采用什么样的技术。

  • 集成开发测试,要求同存异。
  • 注解东西要多写。
  • 元信息。一个是语法。一个是语义

贫血,充血,有没有

数据没有价值,通过筛选得出的信息才是有价值的

  • 大数据:戏子
  • AI:骗子

大数据取决于人工的决策

培训啊

数据结构你要了解
  • 时间复杂度
  • 空间复杂度

信息工程技术:IT:information technology

把信息以低成本的方式传递给每一个人。

Spring 版本

  • 1.0 XML+
  • 1.2 开始 Annotation
  • 2.0 开始注解的提升
  • 2.5 开始提升了很多 Spring MVC
  • 3.0 Annotation 全面替代 XML
  • 4.0添加条件判断。 Annotation java.util.concurrent.locks.Condition 条件判断 org.springframework.boot.autoconfigure.condition.ConditionalOnClass