关于SegmentFault:SpringCloud框架开发教程

36次阅读

共计 5585 个字符,预计需要花费 14 分钟才能阅读完成。

本文章是依据 B 站 UP 主尚硅谷周阳老师教学视频进行练习,会继续更新
视频链接:https://www.bilibili.com/vide…

SpringCloud= 散布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶

SpringBoot 是一种服务开发技术

服务注册与发现:EUREKA

服务负载平衡与调用:NETFLIX OSS RIBBON

服务负载与调用:NETTFLIX

服务熔断降级:HYSTRIX

服务网关:Zuul

服务分布式配置:SpringCloud Config

服务开发:SpingBoot

1、新建 Maven 我的项目,删除 Src 文件,此我的项目作为聚合工程的父工程应用
POM 文件

<?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/m…d”>
<modelVersion>4.0.0</modelVersion>
<groupId>com.springcloud</groupId>
<artifactId>cloud2021</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>Maven</name>
<!– FIXME change it to the project’s website –>
<url>http://maven.apache.org/</url>
<inceptionYear>2001</inceptionYear>
<distributionManagement> <site> <id>website</id>
<url>scp://webhost.company.com/www/website</url>
</site> </distributionManagement>
<!– 对立治理 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>
<!– 子模块继承之后,提供作用:锁定版本 + 子 module 不必 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>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>
2、数据库建表

  1. CREATE DATABASE /!32312 IF NOT EXISTS/cloud /!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci /;
  1. USE cloud;
  2. /Table structure for table payment /
  3. DROP TABLE IF EXISTS payment;
  4. CREATE TABLE payment (
  5. id bigint(20) NOT NULL AUTO_INCREMENT COMMENT ‘ID’,
  6. serial varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
  7. PRIMARY KEY (id)
  8. ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  9. /Data for the table payment /
  10. insert  into payment(id,serial) values (1,’ 尚硅谷 ’),(2,’alibaba’),(3,’ 京东 ’),(4,’ 头条 ’);

3、在父工程内新建子模块
POM 如下:

<?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/m…d”>
<parent> <artifactId>cloud2021</artifactId>
<groupId>com.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent> <modelVersion>4.0.0</modelVersion>
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <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> <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> <dependency> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies></project>

4、在子模块中增加 application.yml 文件并进行配置

server:
port: 8001
spring:
application:

name: cloud-provider-service

datasource:

type: com.alibaba.druid.pool.DruidDataSource

driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/cloud2021?useUnicode=true&characterEncoding-utr-8&useSSL=false

username: root
password: root

mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.springcloud.entities

正文完
 0