current position:Home>Spring cloud Alibaba microservice architecture introduction is the easiest to understand

Spring cloud Alibaba microservice architecture introduction is the easiest to understand

2022-01-27 00:57:36 itxiaoshen

Introduction to microservice Architecture

Spring Cloud Alibaba The recommended micro service ecosystem architecture is based on the hierarchical architecture and is implemented as follows :

  • Access layer : The outermost layer is LVS+Keepalived, It can withstand hundreds of thousands of high concurrent flow peaks , Then through the inner layer nginx The cluster forwards the client requests to the server based on the load balancing policy JAVA Back end technology stack Spring Cloud Gateway colony ;

  • Business middle level :Spring Cloud Gateway Microservices go through Nacos Get the route configuration information and the discovery of the route backend micro service provider , adopt OAuth2 Do unified login Authorization , And integrate Sentinel Limit the current for the request 、 Fuse 、 Reduce , be based on dubbo Protocol performance RPC Make microservice calls or service aggregation calls , The call between the back-end microservices also adopts dubbo Agreed rpc, For the server that needs distributed transactions, it can use Seata Realization .

  • Technical middle floor : The data storage layer includes memory 、 database 、 Full text search engine storage layer ; The basic service layer provides common basic component functions of distributed systems ; The log collection layer adopts ELK

  • System monitoring layer : Distributed Link Tracking 、 Container based monitoring and alarm

The technical points involved in micro service ecology are as follows

Maven Introduction to integration engineering

shopping-demo Source code address
https://gitee.com/yongzhebuju/shopping

Function introduction


This example mainly uses... For microservices Nacos Realize the reading of configuration center 、 Service registration and service discovery , The microservice gateway implements routing strategies and integrates them sentinel Achieve current limiting , Use... Between microservices Dubbo High performance RPC To call .

This case mainly includes the following demo modular

commons: Public service module , Public storage pojo Entity class and micro service interface module , such as Dubbo Service provider interface definition 、 be based on Open Feign Remote call service provider interface definition, etc , Common module pom Some public reference dependencies can be configured, such as spring-cloud-starter-alibaba-nacos-config and spring-cloud-starter-alibaba-nacos-discovery etc. , In this way, other micro services only need to rely on common modules

gateway: Microservice gateway module , Responsible for microservice routing 、 Authorized certification 、 Micro service aggregation and other functional processing

users: User module , Provide access to the user interface

good: Commodity module , Provide commodity interface , Need to call the user interface

Core source code and configuration

Engineering parent pom The file mainly contains Spring Boot、Spring Cloud、Spring Cloud Alibaba My father depends on

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.itxs</groupId>
    <artifactId>shopping</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
    </parent>

    <modules>
        <module>shopping_commons</module>
        <module>shopping_goods</module>
        <module>shopping_users</module>
        <module>shopping_gateway</module>
    </modules>

    <properties>
        <java.verson>1.8</java.verson>
        <spring.cloud.verison>Hoxton.SR12</spring.cloud.verison>
        <spring.cloud.alibaba.verison>2.2.1.RELEASE</spring.cloud.alibaba.verison>
    </properties>

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

</project>

Microservices yml The configuration file , Microservices are configured in Nacos Configuration center , Each microservice local configuration file only needs to configure the service name 、 The active environment and the address of the configuration center 、 Configuration file extension 、 Namespace and group . The following is the gateway configuration file , Other module configuration files are similar

spring:
  profiles:
    active: dev
  cloud:
    nacos:
      config:
        server-addr: localhost:8848
        file-extension: yaml
        namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
        group: shopping
  application:
    name: gateway

commons Entity classes and expose get user interface services

package com.itxs.entity;

import java.io.Serializable;

public class User implements Serializable {
    private String name;
    private Integer age;

    public User(String name, Integer age) {
        this.name = name;
        this.age = age;
    }
}
package com.itxs.service;

import com.itxs.entity.User;

public interface UserService {
    User getUser(String userId);
}

users The microservice obtains the user interface implementation

package com.itxs.service;

import com.itxs.entity.User;
import org.apache.dubbo.config.annotation.Service;

@Service
public class UserServiceImpl implements UserService{
    @Override
    public User getUser(String userId) {
        System.out.println("userId:"+userId);
        return new User("zhangsan",16);
    }
}

users controller Implementation class , Also available here http Protocol call mode

package com.itxs.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @RequestMapping("/users/1")
    public String getUser(){
        return "hello users";
    }
}

goods controller

package com.itxs.controller;

import com.itxs.entity.User;
import com.itxs.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GoodsController {

    @Reference
    UserService userService;

    @RequestMapping("/goods/1")
    public String getGoods(){
        return "hello goods";
    }

    @RequestMapping("/goods/user")
    public String getUserInfo(){
        User user = userService.getUser("a1001");
        return user.toString();
    }
}

Nacos Configuration center

Start a local Nacos server End , Visit local nacos Management interface http://localhost:8848/nacos, The default port is 8848, Default user password nacos/nacos, stay dev There is a gateway under the namespace 、 user 、 Product micro service configuration file , All use shoopping Group

image-20210822231838971

users-dev.yaml

server:
  port: 8081
spring:
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.3.3:8848
        namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
        group: shopping
    sentinel:
      enabled: true
      transport:
        dashboard: localhost:8888
        port: 8719
  application:
    name: users

goods-dev.yaml

server:
  port: 8082
spring:
  profiles:
    active: dev
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.3.3:8848
        namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
        group: shopping
    sentinel:
      enabled: true
      transport:
        dashboard: localhost:8888
        port: 8729
  application:
    name: goods

gateway-dev.yaml

server:
  port: 8083
spring:
  profiles:
    active: dev
  main:
    allow-bean-definition-overriding: true
  cloud:
    nacos:
      discovery:
        server-addr: 192.168.3.3:8848
        namespace: 54c53c3a-6008-4ecc-90fe-2ffcae64b95b
        group: shopping
    sentinel:
      enabled: true
      transport:
        dashboard: localhost:8080
        port: 8719
    gateway:
      discovery:
        locator:
          lowerCaseServiceId: true
          enabled: true
      routes:
        - id: users_route
          uri: lb://users
          predicates:
            - Path=/users/**

        - id: goods_route
          uri: lb://goods
          predicates:
            - Path=/goods/**    
  application:
    name: gateway
management:
  endpoints:
    web:
      exposure:
        include: "*"

Sentinel Console

adopt Sentinel The source code project starts Sentinel Console , It's a Spring Boot project

image-20210822232421237

Visit local Sentinel Console interface http://localhost:8080/, The default port is 8080, Default user password sentinel/sentinel, Because there is no persistence function for the time being , So just came in, the content is empty

image-20210822232751529

Microservices start

Start the gateway 、 user 、 Three microservices of goods , The user micro service port is 8081, The commodity micro service port is 8082, The gateway microservice port is 8083

image-20210822233701053

Access directly without a gateway goods Microservices http://localhost:8082/goods/1, go http Method call interface

image-20210822235420340

Configure our access to user services through gateway routing http://localhost:8083/users/users/1 , The access result is correct

image-20210822233923040

Continue to access commodity interface services http://localhost:8083/goods/goods/1 , The access result is correct

image-20210822234321581 Access goods and services and call user services http://localhost:8083/goods/goods/user , The access result is correct

image-20210822234413673

Sentinel The console sets the lower limit flow rule , in the light of goods/goods/user This contact link is used for flow control setting

image-20210822234605494

image-20210822234734339

image-20210822234755197

When we access it twice a second, we will still access it normally , We press... Continuously and quickly F5 Refresh will appear Blocked by Sentinel: FlowException, This is the default Sentinel Return current limiting , We can also implement custom current limit prompt

image-20210822234905722

** My blog website **IT Little god www.itxiaoshen.com

copyright notice
author[itxiaoshen],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201270057347253.html

Random recommended