spring出品,必属精品,现在springcloud如火如荼,了解它似乎已经成为必要的了。
这里记录一次搭建springcloud项目步骤,其实springcloud每个模块就是一个服务,每个服务就是一个springboot项目,这里使用maven子父模块管理项目
一:先建父项目
什么都不选,直接Next
填写GroupId和ArtifactId,GroupId是项目组织唯一的标识符,一般为反向域名,ArtifactId为项目的唯一的标识符,然后Next
填写项目名称,选择储存项目路径后点击Finish
接下来配置一下maven安装目录和settting.xml文件位置,File---->Setting
ok即可
再配置pom文件
<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>net.lersin.www</groupId>
<artifactId>knowledge-main-project</artifactId>
<!-- 父项目必须为pom -->
<packaging>pom</packaging>
<!-- 版本号 -->
<version>1.0-SNAPSHOT</version>
<!-- 项目名称 -->
<name>knowledge_main_project</name>
<!-- 项目描述-->
<description>main project for Spring cloud</description>
<!--相关版本属性 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR3</spring-cloud.version>
<springboot.version>2.1.11.RELEASE</springboot.version>
<swagger2.version>2.9.2</swagger2.version>
<swagger-ui.version>2.9.2</swagger-ui.version>
<druid.version>1.1.10</druid.version>
<mybatis.version>1.3.1</mybatis.version>
<pagehelper.version>1.2.5</pagehelper.version>
<redis.version>1.5.6.RELEASE</redis.version>
<lombok.verson>1.16.20</lombok.verson>
<fastjson.version>1.2.41</fastjson.version>
</properties>
<!-- 配置dependencyManagement,子项目导入jar包则无需指定版本信息,方便jar包版本统一管理-->
<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>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${springboot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--pageHelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${redis.version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.verson}</version>
</dependency>
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
导jar包时需注意,springcloud和springboot有对应版本号,否则会无法启动,这里不详细介绍,具体查看我另一篇博客,以下地址
https://blog.csdn.net/Ming13416908424/article/details/106635007
由于是父项目,把项目里的src目录删掉,因为用不到,最终效果如下
二:搭建Eureka服务端
新建module
还是什么都不选,next,一直next,finish
建立启动类和配置yml文件,项目结构图如下
其中,WebSecurityConfig类是关闭关闭csrf
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
/**
* Copyright (C), 2018-2020
* FileName: WebSecurityConfig
* Author: yu.gui.ming
* Date: 2020-03-03 10:05
* Description: 关闭csrf
*/
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
super.configure(http);
}
}
application.yml文件配置
spring:
profiles:
active: pde
application-pde.yml文件配置
pring:
application:
name: eureka-service
server:
port: 5858
eureka:
server:
enable-self-preservation: false
renewalPercentThreshold: 0.85
instance:
#主机
hostname: 192.168.0.104
client:
# 是否注册到eureka服务,默认为true,当前已为eureka server,且单点eureka,故配置为false
fetch-registry: false
# 是否在本地缓存注册表信息,默认为true,当前为单点eureka server,
不需要从其他eureka除获取注册表信息,更谈不上缓存,故配置为false
register-with-eureka: false
serviceUrl:
defaultZone: http://${USER}:${PASSWORD}@${eureka.instance.hostname}:${server.port}/eureka/
#erueka 密码校验
USER: 123456
PASSWORD: 123456
spring:
security:
user:
name: ${USER}
password: ${PASSWORD}
然后运行一下看效果,说明启动成功
亦可登录http://localhost:5858/login 输入用户名和密码查看
到此eureka注册中心搭建成功三:搭建测试服务到注册中心
重复搭建eureka注册中心步骤,新建module,还是什么都不选,直接next,填写服务名后点击next,finish即可
搭建后的项目目结构如下图,新建的服务official_service其实与eureka服务结构一样
其中pom.xml文件基本依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>knowledge-main-project</artifactId>
<groupId>net.lersin.www</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>official_service</artifactId>
<!--父模块pom文件中使用dependencyManagement标签,所以这里不需要引入版本号-->
<dependencies>
<!--springboot 基本-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- spring cloud eureka客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- spring cloud feign组件 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
</dependency>
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--fastJson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
</dependencies>
</project>
application.yml文件配置
spring:
profiles:
active: pde
application-pde.yml文件配置
server:
port: 5878
servlet:
#上下文标识
context-path: /off
eureka:
instance:
prefer-ip-address: true
leaseRenewalIntervalInSeconds: 30
leaseExpirationDurationInSeconds: 90
client:
enabled: true
serviceUrl:
#注册到eureka,@前的是用户名和密码
defaultZone: http://lexin:lexin2020@192.168.0.104:5858/eureka/
spring:
datasource:
url: jdbc:mysql://192.168.0.104:3306/backstage_db?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8&noAccessToProcedureBodies=true
driver-class-name: com.mysql.cj.jdbc.Driver
username: qiuming
password: 123456
druid:
initial-size: 1
min-idle: 5
maxActive: 50
maxWait: 60000
timeBetweenEvictionRunsMillis: 6000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filter:
wall:
config:
multi-statement-allow: true # 打开druid允许批量修改
http:
encoding:
charset: utf-8
force: true
enabled: true
application:
name: official-service
ribbon:
eureka:
enabled: true
configuration:
#设置mybatis的resultType为map时value为null字段不丢失
call-setters-on-nulls: true
启动类OfficialServiceApplication代码
package net.lersin.official;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* Copyright (C), 2018-2020
* FileName: OfficialServiceApplication
* Author: yu.gui.ming
* Date: 2020-03-23 10:58
* Description: 前台启动类
*/
@SpringBootApplication(scanBasePackages = {"net.lersin.official"},
exclude={DruidDataSourceAutoConfigure.class}) //扫码包和跳过数据库检查
@MapperScan(value = "com.lexin.official.**.**.**.dao")//扫描mapper包位置
@EnableDiscoveryClient //eureka客户端
@EnableFeignClients // feign调用客户端
public class OfficialServiceApplication {
public static void main(String[] args) {
SpringApplication.run(OfficialServiceApplication.class, args);
System.out.println("official service start");
}
}
最后跑起来没报错,成功
再登录http://localhost:5858/ 查看,服务已成功注册到eureka
到此简单的springcloud demo已经搭建成功
另外,redis,mq等中间件也可当做单独服务注册到eureka以供服务调用,相当方便,先到这里,后续再更新集成第三方中间件及其他相关配置
哪里写得不好欢迎大家留言指出!