Build a SpringCloud resource sharing project from 0 (day one)

Here is the split and rich functions of a springboot project written before, and the main structure is drawn casually

Gateway: gateway

Service registration and configuration: ailibaNacos

Service request forwarding: feign-client

Database: mysql persistent, redis authentication

Search function: es search engine, rabbitmq message queue for data synchronization between mysql and es

The front end is implemented with vue. Since the author does not know much about the front end, he focuses on interface testing when building the springcloud project. The first day is the creation of the project and the authentication of the gateway.

Table of contents

1. Open idea to create a springcloud parent project

2. Create a subproject

1. Open idea to create a springcloud parent project

Directly select the maven project and click Next

The project name and workpiece coordinates can be set by yourself, just click Finish.

  The src of the parent project can be deleted, because the main function of the parent project is to manage the overall dependency version. First, we add the parent project to the management of the springboot version. Note that the springboot and spring cloud versions must correspond here, otherwise the subsequent projects will not start. You can With my version here, you can also go to the official document to see the corresponding relationship.


  Next, manage the version and introduce the coordinates. You can choose dependencies according to your own needs. The dependency in dependencyManagement is the dependency management of spring cloud. You don’t need to specify the version when you introduce the sub-project, as long as the artifactId and groupId are fine. In addition, in properties The written version is introduced below using ${} for easy management.


            <!-- spring-cloud -->



            <!--  Small but complete tool library    -->


            <!--Nacos rely-->

2. Create a subproject

Here we take the gateway sub-project as an example. The creation process of sub-projects is the same. We right-click the parent project, click to create a new module, and set the coordinates and names by ourselves.

Every time a project is created, a module will be generated in the parent project

   After the sub-project is created, we also introduce dependencies. The public package is created to store some general classes such as return classes. Dependencies can be introduced through our own coordinates to reduce repetitive code. We will also call feign-client in this way .

        <!--nacos Service registration discovery dependency-->
        <!--gateway gateway rely-->
        <!--public package-->
        <!--redis rely-->
            <!--            <version>2.3.7.RELEASE</version>-->

After the dependency is imported, we add packages and startup classes to the sub-projects and have yml configuration files

springboot startup class:

package com.ityz.gateway;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class GatewayApplication {
    public static void main(String[] args) {,args);

yml configuration file: This also includes the registration of the service to nacos. The nacos service needs to be enabled. If you don’t need it, comment out the configuration. The following is the gateway routing and forwarding assertion configuration, because all subsequent requests are access to the gateway port, and the gateway performs forwarding and load balancing.

  port: 10010
    name: gateway
      server-addr: localhost:8848 # nacos address
        - id: user-service # Route ID, must be unique
          uri: lb://userService # The target address of the route, lb means load balancing
          predicates: # Routing assertion, to determine whether the request complies with the rules
            - Path=/user/** # Path assertion, to determine whether the path starts with /user, if so, it matches
        - id: file-service
          uri: lb://fileService
            - Path=/file/**
#      default-filters: #The default filter will take effect on all routing requests
#        - AddRequestHeader=Truth,ityz is freaking awesome! #set request header
        add-to-simple-url-handler-mapping: true  #Solve the problem that the options request is intercepted
            allowedOrigins: #Which sites are allowed to make cross-origin requests
              - "http://localhost:8090"
              - ""
            allowedMethods: #Allow cross-domain Ajax request method
              - "GET"
              - "POST"
              - "DELETE"
              - "PUT"
              - "OPTIONS"
            allowedHeaders: "*" #Information that is allowed to be carried in the request header
            allowedCredentials: true #Are cookie s allowed?
            maxAge: 360000 #The validity period of this cross-domain
    #    host: localhost
    port: 6379
    #    password: Zxy020729
        max-active: 8
    database: 0

Generally speaking, it is necessary to pay attention to the correspondence between the boot and cloud versions. The next part will share how to use token and redis for authentication.

Tags: Java Spring Spring Cloud

Posted by Agtronic on Wed, 18 Jan 2023 13:11:17 +1030