A Preliminary Understanding of Spring Cloud

I. Basic Description of MicroService

Reasons for MicroService Architecture: Solving the shortcomings of single application framework.

Monolith framework: The project organization in which all code and functions are included in a WAR package is called Monolith.

Disadvantages of Monolith framework:

Compiling, deploying and testing are difficult.

Difficult to expand;

The choice of technology is difficult.

The meaning of microservices:

Micro-services: a split of a large service; each service runs in its own independent process; services interact directly with each other; deploy independently; and the technology choices of their respective services are very free.

Advantages of microservices:

Easy to compile, deploy and test.

Easy to expand;

Easy choice of technology;

High fault tolerance;

 

Scenarios applicable:

Single Application Architecture: Small and Medium-sized Projects (Relatively Fewer Functions) crm Logistics Inventory Management, etc.

Microsoft Architecture: Large-scale Projects (More Functions) Mall erp, etc.

 

2. MicroService Implementation Technology - spring Provides Solutions (Springboot + spring cloud)

MicroService is just a kind of architecture idea, which puts forward the design principle of microservice.

Achievement: _____________

Single service: spring boot

Spring Boot is a fast-configuring scaffold that can quickly develop individual micro services based on Spring Boot.

Multiple Services (Service Governance Framework):

Since there are many micro services in the micro-service architecture, service governance framework is needed, and spring cloud is a service governance toolkit based on Spring Boot implementation.

Service governance framework:

Spring cloud: Spring family (described in this article);

Dubbo: Alibaba's open source, included by apache.

 

Three: Architecture of Spring Cloud

 

Five God Beasts (Core):

Service Discovery - Netflix Eureka

Customer Service Load Balancing-Netflix Ribbon/Feign

Service Gateway - Netflix Zuul

Circuit Breaker-Netflix Hystrix

Distributed Configuration - Spring Cloud Config

 

All components: 19

Spring Cloud integrates 19 sub-projects, including one or more third-party components or frameworks!

 

Spring Cloud Tool Framework

1. Spring Cloud Config configuration center, using git centralized management program configuration.

2. Spring Cloud Netflix integrates many Netflix open source software

3. Spring Cloud Bus Message Bus, which uses distributed messages to connect services and service instances to propagate state changes in a cluster.

4. Spring Cloud for Cloud Foundry uses Pivotal Cloudfoundry to integrate your application

5. Spring Cloud Cloud Foundry Service Broker provides a starting point for the establishment of service agents that manage cloud hosting services.

6. Spring Cloud Cluster is based on Zookeeper, Redis, Hazelcast, Consul to realize the abstraction and Realization of leadership election and civil status model.

7. Spring Cloud Consul implements service discovery and configuration management based on Hashicorp Consul.

8. Spring Cloud Security provides load balancing for OAuth2 rest client and authentication header forwarding in Zuul proxy

9. Spring Cloud Sleuth Spring Cloud application's distributed tracking system is compatible with Zipkin, HTrace and ELK.

10. Spring Cloud Data Flow, a cloud local program and operation model, forms a data micro-service on a structured platform.

11. Spring Cloud Stream is based on Redis, Rabbit and Kafka to implement message micro-service. Simple declaration model is used to send and receive messages in Spring Cloud application.

Spring Cloud Stream App Starters provides spring integration for external systems based on Spring Boot

13. Spring Cloud Task's short life cycle microservices add functionality and non-functionality to the Spring Booot application's simple declaration.

    14,Spring Cloud Task App Starters

Spring Cloud Zookeeper Service Discovery and Configuration Management is based on Apache Zookeeper.

16. Spring Cloud for Amazon Web Services integrates rapidly with Amazon Web Services.

17. Spring Cloud Connectors make it easy for PaaS applications to connect to back-end services such as databases and message brokerage services on various platforms.

18. Spring Cloud Starters (project terminated and merged with other projects after Angel.SR2)

19. Spring Cloud CLI plug-in uses Groovy to quickly create Spring Cloud component applications.

 

Of course, it's also increasing...

 

Fourth: Introduction to Spring cloud

 

4.1 Create a common maven project and guide

 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.SR1</spring-cloud.version>
        <springboot.version>2.0.5.RELEASE</springboot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${springboot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

  

4.2 Create a project module in the project as a registry

4.2.1 pom guide for registry module

<dependencies>
        <!--springboot Support-->
        <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>
        </dependency>

        <!--Eureka Server-side support-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>

    </dependencies>

4.2.2 Configuration File Configuration of Registry Module (application.yml)

Create a configuration file in the resources directory of the registry module, where the port number is written casually, but it can't already exist.

Note: The hierarchical structure of yml files must be strictly observed.

server:
  port: 4001
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false #Do you want to register with eureka
    fetchRegistry: false #Represents whether registration information is obtained from Eureka Server
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/  stand-alone configuration

4.2.3 Startup Class of Registry Module

Create startup classes in the root directory with at least one level directory in the java directory as the root directory

Note: You can't use the java directory as the root directory directly

@SpringBootApplication
@EnableEurekaServer//The logo is eureka Server
public class EnrekaServerApplication_4001 {
    public static void main(String[] args) {
        SpringApplication.run(EnrekaServerApplication_4001.class);
    }
}

4.2.4 Test

Start the startup class and then access localhost:4001 in the browser

Note: Errors caused by self-protection may occur, but they do not affect the use, do not care.

 

4.3 Create a project module as a common module in the main project

Because both providers and consumers need a domain class, create a common module to store the domain class

public class User {
    private Long id;
    private String name;

    public User(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public User() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

 

 

4.4 Create a project module as a service provider in the main project

4.4.1 service provider module pom Guide

 <dependencies>
        <!--Common Code Dependency-->
        <dependency>
            <groupId>cn.wang</groupId>
            <artifactId>User_interface</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!--springboot Support-->
        <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>
        </dependency>

        <!--eureka Client support: Note: The imported client here is relative to eureka The registry says that we register services to the registry. The registry is the server and our service is the client.-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

 

   

4.4.2 application.yml configuration file for service provider module (Ibid.)

server:
  port: 5001
spring:
  application:
    name: USER-PROVIDER #Do not use underscores
eureka:
  client:
    serviceUrl:
     defaultZone: http://localhost:4001/eureka /  tells the service provider where to register the service and the address of the registry

 

 

   

4.4.3 Startup Class of Service Provider Module (Ibid.)

@SpringBootApplication
@EnableEurekaClient//The expression is eureka Client
public class UserProviderApplication_5001 {
    public static void main(String[] args) {
        SpringApplication.run(UserProviderApplication_5001.class);
    }
}

 

 

 

 

controller of 4.4.4 Service Provider Module

 

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/hh/{id}")
    public User getUser(@PathVariable("id")Long id){
        return new User(id,"I'm a server.");
    }

}

 

 

 

4.5 Create a project module as a service consumer in the main project

4.4.1 pom Guide for Service Consumer Module

<dependencies>
    <!--Common Code Dependency-->
    <dependency>
        <groupId>cn.wang</groupId>
        <artifactId>User_interface</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <!--springboot Support-->
    <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>
    </dependency>

</dependencies>

 

 

 

 

4.4.2 Configuration file for service consumer module (Ibid.)

server:
  port: 6001
spring:
  application:
    name: USER-CONSUMER

 

 

 

 

4.4.3 Startup Class of Service Consumer Module (Ibid.)

@SpringBootApplication
public class UserConsumerAppliction_6001 {
    public static void main(String[] args) {
        SpringApplication.run(UserConsumerAppliction_6001.class);
    }
}

 

 

 

 

4.4.4 Service Consumer Module config

Role: Make RestTemplate a bean to facilitate controller injection into RestTemplate objects.

//The purpose is to RestTemplate Examples submitted spring Management, which relies on injection when it can be used directly ok No more, no more. new object
@Configuration
public class CfgBean {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

 

 

 

controller of 4.4.5 Service Consumer Module

//It has nothing to do with the registry. It's just a normal remote call.
@RestController
@RequestMapping("/consumer")
public class UserController {
    public static final String URL_PREFLX="http://localhost:5001";

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/user/{id}")
    public User getUser(@PathVariable("id")Long id){
        //Calling remote services http request
        String url=URL_PREFLX+"/user/hh/"+id;
        return restTemplate.getForObject(url,User.class );
    }
}

 

  

Test: Start the registry, service provider and service consumer through the startup class.

Registry test: localhost:4001

Service Provider Testing: localhost:5001/user/hh/id

Service consumer testing: localhost:6001/consumer/user/id

Tags: Java Spring SpringBoot Zookeeper

Posted on Tue, 13 Aug 2019 05:14:29 -0700 by fellixombc