springboot整合~swagger~kafka~nginx~redis~mysql(在linux服务器环境下部署运行测试)

~适合小白入门,springboot简单整合微服务技术,部署在linux环境下进行测试~

~注意: 运行环境为linux服务器,提前给linux服务器环境中安装jdk、kafka、zookeeper、nginx、redis、mysql等必要的环境~

文章目录


 

一、springboot项目的搭建

1、这是由于springboot过于简单,直接附上链接大家自己照着新建一个springboot项目

2、注意: 搭建项目是不要自动选择依赖, 我下面已经附上全部所需依赖, 使用的我依赖, 按着我的步骤,测试成功率会很高,不然很有可能出现一些未知问题

点击新建springboot项目

、整合所需全部依赖

		<!-- web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!-- lombok -->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.18.10</version>
		</dependency>
		<!--mybatis-plus-->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.2</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity</artifactId>
			<version>1.7</version>
		</dependency>
		<!--数据库连接池-->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>1.1.17</version>
		</dependency>
		<!--添加JDBC依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<!--数据库连接驱动 连接配置修改时间-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.17</version>
			<scope>runtime</scope>
		</dependency>
		<!--Swagger2依赖-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<!--Swagger UI-->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>
		<!-- kafka -->
		<dependency>
			<groupId>org.apache.kafka</groupId>
			<artifactId>kafka-clients</artifactId>
			<version>2.6.0</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.kafka</groupId>
			<artifactId>spring-kafka</artifactId>
		</dependency>
		<!-- redis -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
			<version>2.0.4.RELEASE</version>
		</dependency>
		<!--引入thymeleaf的依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

下面的xml文件是用来防止项目在加载mapper.xml文件加载不到target中的

<resources>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
				<excludes>
					<exclude>**/*.yaml</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
			<resource>
				<directory>${basedir}/src/main/resources</directory>
			</resource>
</resources>

三、整合具体步骤

1、整合swagger

        1)添加swagger依赖(上面依赖中已经包含)

        2)加入如下代码

package com.kdhy.again.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @Author: liming
 * @Date: 2021/9/9 15:38
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
    /**
     * 测试API
     * @return
     */
    @Bean
    public Docket myDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("集成测试")
                //调用的api描述方法
                .apiInfo(myApiInfo())
                .select()
                //扫描的API包路径
                //这块package路径 , 你得填写你自己的
                .apis(RequestHandlerSelectors.basePackage("com.kdhy.again.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo myApiInfo() {
        return new ApiInfoBuilder()
                .title("综合项目测试文档(kdhy)")
                .description("主要是整合公司所所使用的技术,都包括(kafka/nginx/redis/mysql/swagger/springboot)等")
                .license("文档说明")
                .licenseUrl("http://www.wowuyule.com")
                .termsOfServiceUrl("www.w3c.com")
                .version("1.0.0")
                .build();
    }
}

        3)添加controller类,用来测试swagger

package com.kdhy.again.controller;


import com.kdhy.again.entity.Emp;
import com.kdhy.again.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: liming
 * @Date: 2021/9/9 16:11
 */
@RestController
@Api(tags = "员工管理") // swagger注解 展示接口功能
public class EmpController {

    @Autowired
    private EmpService empService;
    
    /**
     * 用户登录
     * @param empId
     * @param password
     * @return
     */
    @ApiOperation("用户登录")  // swagger注解, 展示接口中方法的功能
    @GetMapping("/login/{empId}/{password}")
    public String login(@RequestBody @PathVariable Integer empId, @PathVariable Integer password){
        Emp login = empService.login(empId, password);
        if(login != null){
            return "登录成功";
        }else {
            return "登录失败";
        }
    }
}

          4) 启动application类 , 访问 http://localhost:8080/swagger-ui.html  即可看到效果

2、整合kafka

        1)添加kafka依赖(上面依赖中已经包含)

        2)启动kafka(启动kafka之前首先启动zookeeper)

        3)

        4)添加生产消息类和消费消息类

        

package com.kdhy.again.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: liming
 * @Date: 2021/9/8 13:57
 */
@Api(tags ="kafka测试")
@RestController
public class KafkaProducer {

    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     *
     * @param normalMessage
     */
    @ApiOperation("生产者-生产消息")
    @GetMapping("/lm/{message}")
    public void sendMessage1(@PathVariable("message") String normalMessage) {
        System.out.println("请求进来了");
        kafkaTemplate.send("topic1", normalMessage);
    }
}

     

           5)启动项目,进行测试

3、整合nginx

        1)

        2)

        3)

4、整合redis

        1)添加redis依赖(上面依赖中已经包含)

        2)

        3)

5、整合mysql

        1)添加mysql依赖(上面依赖中已经包含)

        2)

        3)

上一篇:如何在ASP.NET Core中返回自定义HTTP状态/消息而不返回对象,IActionResult等?


下一篇:.net core 添加swagger