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

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

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

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

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

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

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

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

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

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

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

Spring Cloud Alibaba Introduction to microservice architecture is the easiest to understand. More related articles

  1. Spring Cloud Alibaba | Microservice distributed transaction Seata

    Spring Cloud Alibaba | Microservice distributed transaction Seata Used in the actual combat of this article Spring About version : SpringBoot:2.1.7.RELEASE Spring Cloud:Green ...

  2. Spring Cloud Build the microservices architecture

    Dalston edition because Brixton and Camden Version of the tutorial has stopped updating , So the author plans to 2017 Completed in the first half of Dalston Version of the tutorial ( The original plan is completed Camden Version tutorial , But because I wrote two Dalston ...

  3. Spring Cloud Build the microservices architecture ( One ) Service registration and discovery

    Spring Cloud brief introduction Spring Cloud It's based on Spring Boot Cloud application development tools implemented , It is based on JVM Configuration management in cloud application development based on . Service discovery . Circuit breaker . Intelligent routing . The micro broker . Control bus . Global lock ...

  4. Spring Cloud Build the microservices architecture ( Two ) Serving consumers

    Netflix Ribbon is an Inter Process Communication (IPC) cloud library. Ribbon primarily provides clie ...

  5. Spring Cloud Build the microservices architecture : The service gateway ( Routing configuration )【Dalston edition 】

    Reprint :http://blog.didispace.com/spring-cloud-starter-dalston-6-2/ original  2017-08-26   Zhai Yongchao  Spring Cloud Onlookers ...

  6. Cola Cloud be based on Spring Boot, Spring Cloud Build a microservice architecture enterprise development platform

    Cola Cloud be based on Spring Boot, Spring Cloud Build a microservice architecture enterprise development platform : https://gitee.com/leecho/cola-cloud

  7. Spring Cloud Build the microservices architecture ( 5、 ... and ) The service gateway

    Through the previous articles Spring Cloud Introduction of several core components in , We can already build a simple ( Not perfect enough ) Microservice architecture has been established . See the image below : We use Spring Cloud Netflix Medium Eureka Realized Services ...

  8. Spring Cloud Build the microservices architecture - The service gateway

    Through the previous articles Spring Cloud Introduction of several core components in , We can already build a simple ( Not perfect enough ) Microservice architecture has been established . See the image below : alt We use Spring Cloud Netflix Medium Eureka real ...

  9. Spring Cloud Build micro service architecture ---- A summary of the article

    Spring Cloud Build micro service architecture ---- Preface Original address :https://my.oschina.net/u/1000241/blog/882929 Spring Cloud Build micro service architecture ---- send ...

  10. Spring Cloud Build the microservices architecture ( 3、 ... and ) The message bus

      notes : This article is not suitable for 0 Basic learners read directly , Please read the author's blog about micro services completely first , If there is any doubt , You can read this article again , Address :http://blog.csdn.net/sosfnima/article/d ...

Random recommendation

  1. How to use PullToRefresh

    Here's a detailed explanation PullToRefresh The use of, ( One )-- Build a drop-down refresh listView PullToRefresh The use of, ( Two )--- rewrite BaseAdapter The implementation is complex XML The drop-down refresh PullToRe ...

  2. ECMall How to support SSL Connect to the configuration of the mail server

    First , Mainly ecmall The use of phpmailer Outdated Version , Encrypted connections are not supported . then , We have to make some adjustments to the corresponding code . 1. Cover phpmailer Please download it from the attachment : http://files.cnblogs. ...

  3. Complete from scratch Electron Desktop development (1) Build development environment

    [OTC] # Need knowledge 1. ordinary html.javascript.css knowledge , Namely web Front end introduction . 2. Application of simple command line , No, it doesn't matter , Just follow the code . 3. Let's not talk about downloading and installing . 4. Ben ...

  4. C# Code for converting Chinese characters into pinyin abbreviations

    using System; using System.Configuration; using System.Data; using System.Web; using System.Web.Secu ...

  5. Structs Schematic diagram

    Struts Open source architecture is well implemented MVC Pattern ,MVC namely Model-View-Controller Abbreviation , Is a common design pattern .MVC It weakens the coupling between business logic interface and data interface , And make the view layer more dynamic .MV ...

  6. iOS Mars coordinates related collation and solution summary ( turn )

    I've been dealing with location related code these days , Completely disgusted by the coordinates of Mars . Nausea list from CLLocationManager Take out the latitude and longitude and put it in mapView Displayed on the , It's wrong. ! from CLLocationManage ...

  7. When a function recurses , Recursion times to 900 a long time , Is to throw an exception exception RuntimeError(&#39;maximum recursion depth exceeded&#39;,)

    import subprocess import multiprocessing import urllib import sys import os import pymongo import si ...

  8. Spring Boot 2.0( Two ):Spring Boot 2.0 Taste fresh - dynamic Banner

    Spring Boot 2.0 Provides a lot of new features , One of them is a little egg : dynamic Banner, Today we'll try this first . Configuration dependency Use Spring Boot 2.0 First of all, you need to replace the project dependency package with the one just issued ...

  9. JDBC Some tips for accessing the database

    One . Connect 1. Use try with resources close JDBC resources The sample code is as follows : public List<User> getUser(int userId) { try (Conn ...

  10. PAT1097:Deduplication on a Linked List

    1097. Deduplication on a Linked List (25) The time limit 300 ms Memory limit 65536 kB Code length limit 16000 B The procedure of judging questions Standard author ...