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

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

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

文章目录

一、springboot项目的搭建

二、整合所需全部依赖

三、整合具体步骤

四、总结


一、springboot项目的搭建

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

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


点击新建springboot项目https://www.cnblogs.com/swzx-1213/p/12345330.html

二、整合所需全部依赖

		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
			org.projectlombok
			lombok
			1.18.10
		
		
		
			com.baomidou
			mybatis-plus-boot-starter
			3.4.2
		
		
			com.baomidou
			mybatis-plus-generator
			3.4.1
		
		
			org.apache.velocity
			velocity
			1.7
		
		
		
			com.alibaba
			druid-spring-boot-starter
			1.1.17
		
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
		
			mysql
			mysql-connector-java
			5.1.17
			runtime
		
		
		
			io.springfox
			springfox-swagger2
			2.9.2
		
		
		
			io.springfox
			springfox-swagger-ui
			2.9.2
		
		
		
			org.apache.kafka
			kafka-clients
			2.6.0
		
		
			org.springframework.kafka
			spring-kafka
		
		
		
			org.springframework.boot
			spring-boot-starter-data-redis
			2.0.4.RELEASE
		
		
		
			org.springframework.boot
			spring-boot-starter-thymeleaf
		

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


			
				src/main/java
				
					**/*.xml
				
				
					**/*.yaml
				
			
			
				src/main/resources
				
					**/*.xml
				
			
			
				${basedir}/src/main/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)在linux服务器中添加 topic (其中地址为zookeeper所在服务器地址)

bin/kafka-topics.sh --create --zookeeper 192.168.0.2:2181/kafka100 --topic0 topic-test1 --replication-factor 1 --partitions 2

bin/kafka-topics.sh --create --zookeeper 192.168.0.2:2181/kafka100 --topic1 topic-test1 --replication-factor 1 --partitions 2

        5)  yaml配置文件, 配置kafka

#kafaka
  kafka:
    bootstrap-servers: 192.168.51.222:9092
    listener:
      missing-topics-fatal: false
    producer:
      retries: 0
      acks: 1
      batch-size: 16384
      properties:
        linger:
          ms: 0
      buffer-memory: 33554432
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    consumer:
      properties:
        group:
          id: defaultConsumerGroup

        session:
          timeout:
            ms: 120000

        request:
          timeout:
            ms: 180000

      enable-auto-commit: true
      auto:
        commit:
          interval:
            ms: 1000

      auto-offset-reset: latest

      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer

        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 kafkaTemplate;

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

                消费消息类       

package com.kdhy.again.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.x
 * @Author: liming
 * @Date: 2021/9/8 14:04
 */
@Api(tags = "kafka消费者")
@Component
@RestController
public class KafkaConsumer {
    /**
     * 消费监听
     *
     * @param record
     */
    @ApiOperation("kafka消费者-消费消息")
    @KafkaListener(topics = {"topic1"})
    public void onMessage1(ConsumerRecord record) {
        // 消费的哪个topic、partition的消息,打印出消息内容
        System.out.println("简单消费:" + record.topic() + "-" + record.partition() + "-" + record.value());
    }
}

        5)启动项目,进行测试(就按getmapping中的地址进行测试)

3、整合nginx

        1)配置nginx。conf文件(在nginx安装路径下,conf/nginx.conf)

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

        2)启动nginx(启动方式我的博客中有, 在这里就不说了)

        3)部署jar包, 启动jar包(这里附上jar包启动命令)

nohup java -jar 项目名称.jar --server.port=8888 -Xmx128m -Xms128m > v.log &

        4)  在浏览器中访问nginx服务器(地址为服务器地址,也就是nginx服务器部署的地址)(端口号为nginx.conf中配置listen的端口号)

http://192.168.51.222:8099/

4、整合redis

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

        2)配置yaml文件

 #redis
  redis:
    database: 0
    host: 192.168.51.222
    port: 6379
    password: 123456
    jedis:
      pool:
        max-active: 100
        max-idle: 10
        max-wait: 100000
    timeout: 5000

        3)添加RedisController 类进行测试

package com.kdhy.again.controller;

import com.kdhy.again.entity.Emp;
import com.kdhy.again.util.RedisUtils;
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 20:42
 */
@RestController
@Api(tags = "redis测试curd")
public class RedisController {

    @Autowired
    private RedisUtils redisUtils;
    /**
     * 用户登录
     * @param id
     * @return
     */
    @ApiOperation("用户登录--redis")
    @GetMapping("/redisLogin/{id}")
    public String redisLogin(@RequestBody @PathVariable String id){
        String s = redisUtils.get(id);
        if("李明".equals(s)){
            return "redis登录成功--"+s;
        }else {
            return "redis登录失败";
        }
    }
    
    /**
     * 用户注册
     * @return
     */
    @ApiOperation("用户注册--redis")
    @GetMapping("/redisInsert/{empId}/{name}")
    public String redisInsert(@PathVariable String empId, @PathVariable String name){
        boolean set = redisUtils.set(empId, name);
        if(set){
            return "redis注册成功--李明";
        }else {
            return "redis注册失败";
        }
    }


    /**
     * 用户修改密码

     * @return
     */
    @ApiOperation("用户修改密码--redis")
        @GetMapping("/redisUpdate/{empId}/{name}")
    public String redisUpdate(@PathVariable String empId, @PathVariable String name){
        boolean andSet = redisUtils.getAndSet(empId, name);
        if(andSet){
            return "redis修改成功--李明改为-李民";
        }else {
            return "redis修改失败";
        }
    }

    /**
     * 用户注销
     * @param empId
     * @return
     */
    @ApiOperation("用户注销--redis")
    @GetMapping("/redisDelete/{empId}")
    public String redisDelete(@RequestBody @PathVariable String empId){
        boolean delete = redisUtils.delete(empId);
        if(delete){
            return "redis注销成功--李明";
        }else {
            return "redis注销失败";
        }
    }

}

        2)启动项目, 直接使用url测试

5、整合mysql

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

        2)配置yaml文件

  #配置数据库连接源
  datasource:
    url: jdbc:mysql://192.168.51.222:3306/zb
    username: root
    password: 123456
    driverClassName: com.mysql.jdbc.Driver

        3)代码去我博客的资源里面找(因为mysql测试代码有点多)

        4) 启动项目进行测试

四、总结

最后可以打一个jar包, 进行一个综合测试。有需求可以留言,博主看见会和你交流的。

你可能感兴趣的:(springboot,nginx,kafka,spring,boot,nginx,kafka,redis,linux)