navigation

[react] Hooks

[Encapsulation 01- Design Pattern] Design principles and factory pattern (simple abstract approach) Adapter pattern Decorator pattern [Encapsulation 02- Design Pattern] Command pattern Enjoy pattern Composite pattern Agent pattern

[React from zero practice 01- background] code split [React from zero practice 02- background] permission control [React from zero practice 03- background] custom hooks [React from zero practice 04- background] docker-compose Deploy React + Egg +nginx+mysql [React From zero practice 05- background] Gitlab-CI using Docker automated deployment

[source code – Webpack01 – precompiler] AST abstract syntax tree [source code – Webpack02 – Precompiler] Tapable [source code – Webpack03] hand written webpack-compiler simple compilation process [source code] Redux React-redux01 [source] Axios [source] vuex [source -vue01] Data reactive and initialize render [source -vue02] Computed responsive – Initialize, access, Update Procedure [source -vue04] Watch Listening properties – Initialize and update [source -vue04] vue. set and vm.$set [source -vue05] vue.extend

[source -vue06] Vue. NextTick and VM.$nextTick [Deployment 01] Nginx [Deployment 02] Docker deployVUE project [Deployment 03] Gitlab-CI

[Data Structures and Algorithms 01] Binary search and sort

[Deep 01] Execution context [Deep 02] Prototype chain [Deep 03] Inheritance [Deep 04] Event loop [Deep 05] Curri Bias function [Deep 06] Function memory [Deep 07] Implicit conversions and operators [Deep 07] Browser caching mechanism (HTTP caching mechanism) [Deep 08] Front-end security [Deep 09] Deep copy [Deep 10] Debounce Throttle [Deep 10] Front-end routing [Deep 12] Front-end modularization [Deep 13] Observer mode Publish subscribe mode Bidirectional data binding [Deep 14] Canvas [Deep 15] webSocket Webpack HTTP and HTTPS CSS- Interview Handwriting Promise Data Structures and Algorithms – Binary Search and Sorting Js Design Patterns – Agents, policies, singletons

/ front-end learn java01 – SpringBoot combat environment configuration and the HelloWorld service [front-end learn java02 – SpringBoot combat] mybatis + mysql implementation song to add and delete [front-end learn java03 – SpringBoot combat] Lombok, log, Java04 -SpringBoot combat deployment [front-end science Java04 -SpringBoot combat] static resources + interceptor + front and back end file upload [front-end science Java05 -SpringBoot combat] common annotates + Redis implementation statistics function [front-end science Java06 -SpringBoot combat] inject + Swagger2 3.0 + unit test JUnit5 [Front-End science Java07 -SpringBoot real World] IOC scanner + transaction + Jackson [front-end science Java08 -SpringBoot real world summary 1-7 [java09-SpringBoot] Multi-module configuration + Mybatis-plus + single multi-module package deployment [Java10 -SpringBoot] Bean assignment conversion + parameter verification + global exception handling [java11-spring Security] configuration + memory + database = three ways to achieve RBAC [java12-spring Security] JWT [java13-SpringCloud] Eureka + RestTemplate + Zuul + Ribbon

(1) Pre-knowledge

(1) Some words

// @loadbalanced // Indicates a load balance based on the ribbonCopy the code

(2) Problems existing in single application

  • As the business gets more complex, the system gets bigger
  • The failure of one module may cause the collapse of the entire business system
  • Multiple teams manage data, which is prone to security vulnerabilities
  • Each module uses the same technology, it is difficult to choose a more suitable technical framework according to the actual situation, and there are great limitations
  • Module content is too complex, member changes need a long time to complete the work handover

(3) (distributed) and (cluster)

  • The cluster
    • (one server) cannot handle (high concurrent data accesses), so use (ten servers) to (share pressure)
    • The physical layer
  • distributed
    • Break a complex problem into smaller, simpler ones
    • Break a (large project) into (several microservices) to (work together)
    • Software Design

(4) architecture

  • Spring Cloud architecture diagram

  • component
    • Registry Eruka
    • Load balancing Ribbon
    • Monitoring Zipkin
    • The gateway zuul
    • Etc.

(二) Spring Cloud

(1) Registry Eureka

  • Eureka means discovered
  • The registry is also called the service center
  • The role of the registry
    • What is stored on the registry is (service name)
    • You can get it through Eureka, make a remote call through the service name, and don’t worry about it, just keep the same name
    • With a registry, no service can be called directly, but it needs to be called through the registry
  • conclusion
    • The main functions of Eureka are (service registration) and (service discovery).
    • Eureka is a service provider and a consumer.

(1.1) Eureka basic architecture

  • Eureka Server
    • Provide service registration and service discovery
  • Service Provider
    • Service provider
    • Register your service with Eureka so consumers can find it
  • Service Consumer
    • Service consumer
    • Get a list of registered services from Eurkea so you can consume the services

(3) Use Spring Cloud /Eureka in srpingBoot

  • Spring – the cloud’s official website
  • Actual project source code
  • Take the Eureka registry as an example
    • Registry Server
    • Service Provider Client

(1) Create a parent project

  • The parent project can select (spring-Assistant) to quickly create a Web service project that the parent project has
    • spring-boot-starter-web
    • spring-boot-start-test

(2) Add SpringCloud dependencies to the parent project’s POM.xml

<properties> <! </ spring-cloud-version > </properties> <! -- Submodules need to display the declaration referenced modules, for unified package management --> <! -- spring-cloud-dependencies --> <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> </dependencies> </dependencyManagement>Copy the code

(3) Create a new module, name it eurekaServer, and modify the pom.xml of the sub-module

  • Eureka is the registry, which is responsible for registering services and providing services, etc
  • The pom.xml below indicates that eurekaServer is a registered server
In child module - eurekaServer pom. Add in the XML file - < dependencies > < the dependency > < groupId > org. Springframework. Cloud < / groupId > <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>Copy the code

(4) Add the application. Yml configuration file to the resources of submodole-Eurekaserver

eurekaServer/resources/application.yml --- server: port: 8761 eureka: client: register-with-eureka: Select * from eureka server where eureka server is located; select * from eureka server where eureka server is located; DefaultZone: http://localhost:8761/eureka/ # registry access addressCopy the code

(5) Add the startup class in eurekaServer and add the @enableEurekaserver annotation

@enableEurekaserver @enableEurekaserver @enableEurekaserver @enableEurekaserver @enableEurekaserver @enableEurekaserver @enableEurekaserver Public class EurekaServerApplication {public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); }}Copy the code

(6) From step 1-5, there is a (crater) is to ensure that (springBoot) and (springCloud(of)The same version), otherwise it will report an error

(7) Start eurekaServer and access the configuration center address declared in Application. yml

  • Access:http://localhost:8761
  • Since the registry has not registered any services here, start registering yourself
    • namelyegister-with-eureka: true
    • Service start:up
    • Service closed:down
  • Open, the effect is as follows

(8) With the configuration center, we create a new eurekaClient to provide services

  • Create a module => eurekaClient
  • In the POM. XML of the eurekaClient module, do the following configuration
  • Configure client in application. Yml of eurekaClient module
  • Create a new startup class EurekaClientApplication in eurekaClient
1. eurekaClient/pom.xml
---

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
Copy the code
2. eurekaClient/resources/application.yml --- server: port: 8761 eureka: client: register-with-eureka: Select * from eureka server where eureka server is located; select * from eureka server where eureka server is located; DefaultZone: http://localhost:8761/eureka/ # registry access addressCopy the code
3. eurekaClient/java/com/example/eurekaClient/EurekaClientApplication --- @SpringBootApplication public class EurekaClientApplication { public static void main(String[] args) { SpringApplication.run(EurekaClientApplication.class, args); }}Copy the code
  • The access effect is as follows

(四) RestTemplate

  • RestTemplate is a REST service component provided by the Spring framework. The underlying layer is to encapsulate HTTP requests and responses and provide a way to access ERST services to simplify code development
  • One thing to note here
    • Is that callers connected through the RestTemplate do not have to register in the registry because anyone who invokes the service can

(1) Go ahead and create the sub-module – restTemplate – and then create the startup class

  • RestTemplateApplication
resTemplate/java/com.example.restTemplate/RestTemplateApplication --- @SpringBootApplication public class RestTemplateApplication { public static void main(String[] args) { SpringApplication.run(RestTemplateApplication.class, args); Public RestTemplate RestTemplate() {return new RestTemplate();} @autowired public RestTemplate() {return new RestTemplate(); // Return an instance of RestTemplate}}Copy the code

(2) Create a controller in the restTemplate module and passrestTemplate.getForObjectTo invoke theServices in the registry

  • restTemplate.getForObject
  • You only need to know the access address provided by the service, not the name of the service, and so on
resTemplate/java/com.example.restTemplate/controller/UserController --- @RestController public class UserController { @Autowired RestTemplate restTemplate; @ GetMapping ("/service - the user ") public user getServiceUser () {/ / through the restTemplate. GetForObject call registry service / / / / the first parameter is the service address The second parameter is the data returned by the receiving class return restTemplate. GetForObject (" http://localhost:8010/user ", the User. The class); }}Copy the code

(3) Start restTemplate, and then access /service-use visits to query services in the registry

(5) Service consumer

  • Create a new submodule /eurekaConsumer
  • In a module/eurekaConsumer/pom. XML is introduced into the clound – client dependence
  • In a module/eurekaConsumer/resouces/application. Yml configured in eureka
  • In the Java/com. Example. EurekaConsumer/EurekaConsumerApplication start class

(1) What is the difference between a consumer and an ordinary Module calling a service?

  • The difference is that the Consumer is registered in the registry, whereas the restTemplate above, for example, is not
  • Although registrie-registered services can be invoked, the restTemplate itself is not registered in the registry

(2) The specific steps are as follows

1. eurekaConsumer/pom.xml
---

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
Copy the code
2. eurekaConsumer/resouces/application.yml
---

server:
  port: 8030
spring:
  application:
    name: consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
Copy the code
(3) in a module/eurekaConsumer/Java/com. Example. EurekaConsumer/EurekaConsumerApplication - @ SpringBootApplication public class EurekaConsumerApplication { public static void main(String[] args) { SpringApplication.run(EurekaConsumerApplication.class, args); } @Bean public RestTemplate restTemplate() { return new RestTemplate(); }}Copy the code

(vi) Service gateway Zuul

  • Zuul is an open source (API gateway server) provided by Netfix, and it is the middle layer for all requests from clients and servers. An API is developed externally to import all requests into a unified entrance, and the specific implementation logic of the server is masked
  • ZuulCan be implemented
    • The reverse proxy
    • In the gateway internal dynamic routing, identity authentication, IP filtering, data monitoring, and other functions
    • Zuul comes with load balancing that allows you to modify the service provider's code

(1) Create a new module/zuul, and then add (eureka-client) and (zuul) dependencies in pom.xml

zuul/pom.xml
---

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>
</dependencies>
Copy the code

(2) new zuul/SRC/main/resources/application, yml and add zuul rules

server: port: 8040 spring: application: name: gateway eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ zuul: routes: the provider: / p / * * # to the provider service providers set mappingCopy the code

(3) Create the startup class ZuulApplication

// @enableZuulProxy: contains @enablezuulServers to set this class as the gateway startup class // @enableAutoConfiguration: EnableZuulProxy @EnableAutoConfiguration Public Class ZuulApplication Can help springBoot load all the eligible @Configuration configurations into the current IOC container { public static void main(String[] args) { SpringApplication.run(ZuulApplication.class, args); }}Copy the code

(七) Ribbon

  • Is a load balancing solution based on Netflix Ribbon
  • After registering the Ribbon at this center,The Ribbon can automatically invoke the help service consumer interface based on a load balancing algorithm, developers can also customize the Ribbon’s load balancing algorithm according to their needs.
    • polling
    • random
    • Weighted training in rotation
    • A weighted random
  • In practice, the Ribbon works in conjunction with Eureka. The Eureka Server provides a list of services that can be called. The Ribbon selects which service providers to call based on a specific load-balancing algorithm

(1) Specific process

  • The new module/ribbon
  • Add a eureka-client dependency in pom.xml
  • Create an application.yml configuration port, such as eureka
  • Create a new startup class, RibbonApplidation, note the annotations@LoadBalanced
    • @loadBalanced Declares a load balancing based on the Ribbon
    • The balance is balanced
1. pom.xml
---

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
Copy the code
2. application.yml
---

server:
  port: 8060
spring:
  application:
    name: ribbon
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
Copy the code
3. RibbonApplication --- package com.example.ribbon; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class RibbonApplication { public static void main(String[] args) { SpringApplication.run(RibbonApplication.class, args); } @bean @loadBalanced // Declare a Ribbon based load balancing RestTemplate RestTemplate() {return new RestTemplate();} @bean @loadBalanced // Declare a Ribbon based load balancing RestTemplate RestTemplate() {return new RestTemplate(); }}Copy the code
4. controller/UserController --- @RestController public class UserController { @Autowired RestTemplate restTemplate; @ GetMapping ("/ribbon - the user ") public user getServiceUser () {/ / through the restTemplate. GetForObject call registry service / / / / the first parameter is the service address The second argument is the receiving class of the data returned. That can use the registry service name instead of services provide specific address return restTemplate. GetForObject (" http://provider/user ", the User. The class); // return restTemplate.getForObject("http://localhost:8010/user", User.class); }}Copy the code