快速搭建springcloud环境
Posted 夏虫语氷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了快速搭建springcloud环境相关的知识,希望对你有一定的参考价值。
一、SpringBoot和SpringCloud版本选型
spring-boot: 2.2.2.RELEASE
spring-cloud: Hoxton.SR1
spring-cloud-alibaba: 2.1.0.RELEASE
jdk: 1.8
mysql: 5.1.47
druid: 1.1.16
junit:4.12
log4j:1.2.17
lombok:1.16.18
mybatis:1.3.0
二、父工程创建
三、配置环境
编码统一为UTF-8
注解生效激活
编译版本为1.8
过滤文件格式为 *.idea, *.iml 的文件
配置maven仓库及配置文件
不区分大小写
项目版本
启动自动导包
跳过单元测试
安装lombok插件
四、父工程pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atgugu.springcloud</groupId>
<artifactId>cloud2021</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>cloud-provider-payment8001</module>
</modules>
<packaging>pom</packaging><!-- 这里添加,注意不是jar或war -->
<!-- 统一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<lombok.version>1.16.18</lombok.version>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
</project>
4.1 dependencyManagement
Maven使用dependencyManagement元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM中看到dependencyManagement元素。使用pom.xml中的dependencyManagement元素能让所有在子项目中引用个依赖而不用显式的列出版本量。
Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本号。
这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改;另外如果某个子项目需要另外的一个版本,只需要声明version就可。
dependencyManagement里只是声明依赖,并不实现引入,因此子项目需要显示的声明需要用的依赖。
如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本,才会从父项目中继承该项,并且version和scope都读取自父pom。
如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
4.2 父工程执行mvn : install
父工程创建完成执行mvn : install将父工程发布到仓库方便子工程继承。
五、创建支付模块Module
如图,在支付模块下建立相应文件
六、支付模块相关文件
6.1 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>cloud2021</artifactId>
<groupId>com.atgugu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<!--
1. Zipkin可以结合压力测试工具一起使用,分析系统在大压力下的可用性和性能
2. spring boot对zipkin的自动配置可以使得所有RequestMapping匹配到的endpoints得到监控,
以及强化了RestTemplate,对其加了一层拦截器,使得由它发起的http请求也同样被监控。
-->
<!--包含了sleuth+zipkin-->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>-->
<!--eureka-client-->
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>-->
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<!--
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
-->
<!--web的场景,自动帮我们引入了web模块开发需要的相关jar包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--actuator是监控系统健康情况的工具-->
<!--在生产环境中,需要实时或定期监控服务的可用性。spring-boot 的actuator(监控)功能提供了很多监控所需的接口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--springboot程序测试依赖,如果是自动创建项目默认添加-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
6.2 application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
org.gjt.mm.mysql.Driver # mysql驱动包 :
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
mybatis:
mapperLocations: classpath:mapper/*.xml
com.atgugu.springcloud.entities # 所有Entity别名类所在包 :
6.3 启动类PaymentMain001
package com.atgugu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
SpringBootApplication启动类不能直接放在java文件夹下
否则报此错误:
WARNING ** : Your ApplicationContext is unlikely
to start due to a @ComponentScan of the default package.
*/
public class PaymentMain001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain001.class, args);
}
}
6.4 PaymentController
package com.atgugu.springcloud.controller;
import com.atgugu.springcloud.entities.CommonResult;
import com.atgugu.springcloud.entities.Payment;
import com.atgugu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
public class PaymentController {
private String serverPort;
PaymentService paymentService;
public CommonResult create(Payment payment) {
log.info("*****调用方法:/payment/create,请求参数为:payment:{}", payment.toString());
int result = paymentService.create(payment);
log.info("*****插入结果:"+result);
if(result > 0) {
return new CommonResult(200,"插入数据库成功,serverPort: "+serverPort,result);
}else{
return new CommonResult(444,"插入数据库失败",null);
}
}
public CommonResult<Payment> getPaymentById( Long id) {
log.info("*****调用方法:/payment/get,请求参数为: id:{}", id);
Payment payment = paymentService.getPaymentById(id);
if (payment != null) {
return new CommonResult(200, "查询成功,serverPort: " + serverPort, payment);
} else {
return new CommonResult(444, "没有对应记录,查询ID: " + id, null);
}
}
}
6.5 PaymentService
package com.atgugu.springcloud.service;
import com.atgugu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
public interface PaymentService {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
6.6 PaymentServiceImpl
package com.atgugu.springcloud.service.impl;
import com.atgugu.springcloud.mapper.PaymentMapper;
import com.atgugu.springcloud.entities.Payment;
import com.atgugu.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
public class PaymentServiceImpl implements PaymentService {
private PaymentMapper paymentMapper;
public int create(Payment payment) {
return paymentMapper.create(payment);
}
public Payment getPaymentById(Long id) {
return paymentMapper.getPaymentById(id);
}
}
6.7 PaymentMapper
package com.atgugu.springcloud.mapper;
import com.atgugu.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
*/
@Mapper
//@Repository不用Spring的
public interface PaymentMapper {
public int create(Payment payment);
public Payment getPaymentById(@Param("id") Long id);
}
6.8 PaymentMapper.xml
<mapper namespace="com.atgugu.springcloud.mapper.PaymentMapper">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values(#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.atgugu.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
</select>
</mapper>
6.9 Payment
package com.atgugu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
public class Payment implements Serializable {
private long id;
private String serial;
}
6.10 CommonResult
package com.atgugu.springcloud.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
//使用这个注解,就不用再去手写Getter,Setter,equals,canEqual,hasCode,toString等方法了,注解后在编译时会自动加进去。
//使用后添加一个构造函数,该构造函数含有所有已声明字段属性参数
//使用后创建一个无参构造函数
public class CommonResult<T> {
private Integer code;
private String message;
private T data;
//如果没有安装lombok插件,此步会报错
public CommonResult(Integer code, String message){
this(code, message, null);
}
}
七、启动服务测试
7.1 Postman - http://localhost:8001/payment/get/1
7.2 Postman - http://localhost:8001/payment/create?serial=test1
八、出现异常
8.1 异常1
org.springframework.context.ApplicationContextException: Unable to start web server;
Unable to start ServletWebServerApplicationContext due to
missing ServletWebServerFactory bean
在Spring Boot项目中,出现这个错误有两种情况:
一,在main方法所在的类忘记添加@SpringBootApplication
二,缺少依赖,添加即可
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
8.2 异常2
** WARNING ** : Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
// 该警告解释为 : ApplicationContext 不能从一个组件的默认包启动
// 一般发出这个警告的原因是你把启动类直接放在的src目录下面。
// 你需要在src目录下面再建一个包,比如controlcenter,然后把启动类放到controlcenter下面。
注意入口类(SpringBootApplication)在项目结构中所在位置:
SpringBootApplication启动类不能直接放在java文件夹下
谢谢!
以上是关于快速搭建springcloud环境的主要内容,如果未能解决你的问题,请参考以下文章
02. SpringCloud实战项目-快速搭建Linux环境-运维必备
SpringCloud环境搭建服务提供者 我们需要拿到实体类,所以要配置api module, 在这时报错
Sleuth+Zipkin 实现 SpringCloud 链路追踪