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
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.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> </parent>
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.
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Hoxton.SR10</spring-cloud.version> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <hutool.version>5.5.8</hutool.version> <java.version>1.8</java.version> <druid.version>1.1.10</druid.version> <fastjson.version>1.2.62</fastjson.version> <mybatisplus.version>3.5.1</mybatisplus.version> </properties> <dependencyManagement> <dependencies> <!-- spring-cloud --> <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.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.10</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!--mybatis-plus--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}</version> </dependency> <!-- Small but complete tool library --> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>${hutool.version}</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!--Nacos rely--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
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 .
<dependencies> <!--nacos Service registration discovery dependency--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--gateway gateway rely--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--public package--> <dependency> <groupId>com.ityz</groupId> <artifactId>love-common</artifactId> <version>1.0</version> </dependency> </dependency> <!--redis rely--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!-- <version>2.3.7.RELEASE</version>--> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.7.2</version> </dependency> </dependencies>
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; @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class,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.
server: port: 10010 spring: application: name: gateway cloud: nacos: server-addr: localhost:8848 # nacos address gateway: routes: - 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 predicates: - Path=/file/** # default-filters: #The default filter will take effect on all routing requests # - AddRequestHeader=Truth,ityz is freaking awesome! #set request header globalcors: add-to-simple-url-handler-mapping: true #Solve the problem that the options request is intercepted cors-configurations: '[/**]': allowedOrigins: #Which sites are allowed to make cross-origin requests - "http://localhost:8090" - "http://www.leyou.com" 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 redis: # host: localhost host: 8.130.40.99 port: 6379 # password: Zxy020729 jedis: pool: 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.