手把手教你搭建spring cloud项目
IDE开发工具:IntelliJ IDEA 14.0.2
版本管理:Maven
技术栈:spring cloud
环境:JDK 1.8
(1)文件->新建项目->maven,如图:
文件->新建项目
(2)填写模块名称和项目路径
按照以下步骤创建 Maven 项目。
这时候的项目不是spring boot项目!!
(1)pom.xml 引入所需的jar包
注:根据各项目实际情况,建筑业主为maven私人仓库
介绍SpringBoot所需的jar包
引入spring cloud需要的jar包
引入ereka服务注册发现客户端需要的jar包
介绍mybatis spring cloud 依赖的jar包
介绍kafka需要的jar包
引入redis需要的jar包
配置中心Spring config client的引入依赖jar包等,根据各个项目的需要。
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.**inks.e**s</groupId>
<artifactId>msg</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>msg</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<tomcat.version>8.5.30</tomcat.version>
</properties>
<dependencies>
<!--spring-cloud-config introduce -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--spring-cloud Monitoring function is introduced, which can be used to refresh configuration files dynamically -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- introduce feign rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
</dependency>
<!-- kafka rely on -->
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
</dependency>
<!-- redis rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Spring Boot -->
<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>
</dependency>
<!--mybatis to configure-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.epaylinks.efps</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.epaylinks.efps</groupId>
<artifactId>logtracer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpasyncclient</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form-spring</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>io.github.openfeign.form</groupId>
<artifactId>feign-form</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/generatorConfig-mch.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.1.0.7.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
(2)创建spring cloud项目的主入口类** Application
package com.**s.**s;
import com.**ks.e**.common.util.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.kafka.annotation.EnableKafka;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.client.RestTemplate;
@RefreshScope
@EnableEurekaClient
@EnableFeignClients
@EnableKafka
@EnableScheduling
@EnableAspectJAutoProxy(proxyTargetClass=true , exposeProxy=true)
@SpringBootApplication
public class MsgApplication {
// Note that since we inject RestTemplate into the controller, we need to instantiate an instance of this class at startup
@Autowired
private RestTemplateBuilder builder;
// Use RestTemplateBuilder to instantiate the RestTemplate object. spring has injected the RestTemplateBuilder instance by default
@Bean
public RestTemplate restTemplate() {
return builder.build();
}
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MsgApplication.class);
SpringContextUtils.setApplicationContext(springApplication.run(args));
}
}
@SpringBootApplication的注解说明这个项目是一个SpringBoot项目,这个类是项目的主入口类。
使用以下主要方法启动应用程序
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MsgApplication.class);
SpringContextUtils.setApplicationContext(springApplication.run(args));
}
至此,spring boot项目已经完成了大半。接下来是配置数据库连接和registry config、ereka等配置文件
(1)读取Spring Config Center配置项目的配置文件
楼主使用Spring Config Center作为配置中心,配置读取项目所需的各种连接和配置信息(Spring Config Center这里不再详细介绍)
(2)GitLab远程托管配置信息
Spring Config Center配置项目的连接和读取权限后,在gitlab上配置项目的各种需要的信息
在此处查看另一篇文章
spring cloud项目使用gitLab作为配置中心
(3)加载项目中各个环境对应的配置文件信息(dev、test、prod、uat)
以dev开发环境为例
按图标顺序加载
所有spring cloud项目都是从bootstrap.yml文件开始加载项目所需的各种连接和配置信息。这是spring cloud core自带的决定。可以研究源码,这里不详述。
spring:
application:
name: msg
cloud:
config:
uri: http://172.20 *. 4 *. 80:9000 / ා configure the url of the spring cloud config server
profile: dev # Specify profile
label: master # Specify the branch of the gitlab repository
主要是连接spring cloud config server获取远程gitlab上的配置信息。
eureka:
client:
registerWithEureka: true
service-url:
defaultZone: http://172.20.4.80:8000/eureka/
swagger:
enable: true
主要用于连接eureka服务注册和发现
至此,春关工程已基本完成。下一步就是为各种数据库建表,配置Mybatis,更新代码。
你适合学Java吗?4大专业测评方法
代码逻辑 吸收能力 技术学习能力 综合素质
先测评确定适合在学习