Software Architecture

Microservices with Spring Cloud

微服务架构

The microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery. There is a bare minimum of centralized management of these services, which may be written in different programming languages and use different data storage technologies.

Monoliths & Microservice

height:300px

https://martinfowler.com/microservices/

Breaking Monolithic to Smaller Services

height:450px

Microservices with Spring

https://spring.io/blog/2015/07/14/microservices-with-spring

基础设施

height:400px

https://www.bilibili.com/video/BV115411s7ks?p=2 2:30

paulc4/microservices-demo

Client-side load-balancing


@Service
public class WebAccountsService {

    @Autowired
    @LoadBalanced
    protected RestTemplate restTemplate;


    public Account findByNumber(String accountNumber) {

        logger.info("findByNumber() invoked: for " + accountNumber);
        return restTemplate.getForObject(serviceUrl + "/accounts/{number}",
                Account.class, accountNumber);
    }


}

Monoliths vs. Microservice

height:450px

集成发布

height:400px

Micro services can only be deployed independently if tests are independent & if your Continuous Delivery Pipeline works

组织管理

height:400px

PetClinic微服务版

height:400px

https://www.bilibili.com/video/BV115411s7ks?p=3 4:30

spring-petclinic/spring-petclinic-microservices

断路器

It's common for software systems to make remote calls to software running in different processes, probably on different machines across a network. One of the big differences between in-memory calls and remote calls is that remote calls can fail, or hang without a response until some timeout limit is reached. What's worse if you have many callers on a unresponsive supplier, then you can run out of critical resources leading to cascading failures across multiple systems.

断路器

The basic idea behind the circuit breaker is very simple. You wrap a protected function call in a circuit breaker object, which monitors for failures. Once the failures reach a certain threshold, the circuit breaker trips, and all further calls to the circuit breaker return with an error, without the protected call being made at all. Usually you'll also want some kind of monitor alert if the circuit breaker trips.

https://www.bilibili.com/video/BV115411s7ks?p=4 3:50

sa-spring/spring-circuitbreaker

Spring Boot

height:400px

So Easy

@SpringBootApplication
@RestController
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }

    
    @GetMapping("/")
    public String hello(){
        return "Hello";
    }
}

Starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

A starter (also self-starter, cranking motor, or starter motor) is a device used to rotate (crank) an internal-combustion engine so as to initiate the engine's operation under its own power.

Spring Starters

  • Starters are a set of convenient dependency descriptors that you can include in your application.
  • You get a one-stop shop for all the Spring and related technologies that you need without having to hunt through sample code and copy-paste loads of dependency descriptors.
  • For example, if you want to get started using Spring and JPA for database access, include the spring-boot-starter-data-jpa dependency in your project.

Starter Example

height:430px

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-project/spring-boot-starters

Auto Configure

height:430px

H2 Console

height:430px

How it boots?

height:430px

小结

height:400px


spring-petclinic/spring-petclinic-microservices