欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringCloud基本Rest微服務(wù)工程搭建過程

 更新時間:2021年01月25日 08:53:40   作者:MPolaris  
這篇文章主要介紹了SpringCloud基本Rest微服務(wù)工程搭建,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1. 父工程構(gòu)建

1.1 Maven項目搭建 環(huán)境 版本JDK1.8Maven3.6+Maven模板maven-archetype-size刪除父工程src文件

環(huán)境 版本
JDK 1.8
Maven 3.6+
Maven模板 maven-archetype-size
刪除父工程src文件

1.2 父工程pom文件

回顧:

① Maven中dependencyManagement和dependencies的區(qū)別

​ Maven使用dependencyManagement元素來提供一種管理依賴版本號的方式。通常會在一個組織或者項 目的最頂層的父POM中看到。使用pom.xml中 的dependencyManagement元素能讓所有中引用一個依賴而不用顯示的列出版本號。Maven會沿著父子層次向上找,直到找到一個擁有dependencyManagement元素的項目,然后它就會使用這個元素中指定的版本號。另外如果某個子項目需要使用另外的版本只需要聲明version即可。注意:dependencyManagement里只是聲明依賴,并不實現(xiàn)引入,因此子項目要顯示的聲明需要的依賴。

② Maven中如何跳過單元測試

​ 在Idea的Maven側(cè)欄點擊閃電圖標(biāo),使Maven跳過test生命周期即可。

<?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.polaris</groupId>
 <artifactId>com.polaris.springcloud</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>pom</packaging>
 
 <modules>
 
 </modules>

 <!-- 統(tǒng)一管理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>

2. 微服務(wù)提供者支付Module模塊

cloud-provider-payment8001

2.1 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>com.polaris.springcloud</artifactId>
 <groupId>com.polaris</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>
 <!--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>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
  <scope>test</scope>
 </dependency>
 </dependencies>
</project>

2.2 yml配置文件

server:
 port: 8001

spring:
 application:
 name: cloud-payment-service
 datasource:
 type: com.alibaba.druid.pool.DruidDataSource  # 當(dāng)前數(shù)據(jù)源操作類型
 driver-class-name: org.gjt.mm.mysql.Driver  # mysql驅(qū)動包
 url: jdbc:mysql://mpolaris.top:3306/cloud-test?useUnicode=true&characterEncoding=utf-8&useSSL=false
 username: root
 password: 123456

mybatis:
 mapperLocations: classpath:mapper/*.xml
 type-aliases-package: com.polaris.springcloud.entities # 所有Entity別名類所在包

2.3 主啟動類

/**
 * @Author polaris
 * @Date 2021/1/21 1:00
 */
@SpringBootApplication
public class PaymentMain {
 public static void main(String[] args) {
 SpringApplication.run(PaymentMain.class,args);
 }
}

2.4 業(yè)務(wù)類(簡單演示)

建表

entities

主實體 與 Json封裝體

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
 private Long id;
 private String serial;

}
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {
 private Integer code;
 private String message;
 private T data;

 public CommonResult(Integer code, String message){
 this(code,message,null);
 }
}

dao

接口PaymentDao與mybatis映射文件PaymentMapper

@Mapper
public interface PaymentDao {

 int save(Payment payment);

 Payment getPaymentById(@Param("id") Long id);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

<mapper namespace="com.polaris.springcloud.dao.PaymentDao">

 <insert id="save" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
  insert into payment(serial) values(#{serial});
 </insert>


 <resultMap id="BaseResultMap" type="com.polaris.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>

service

接口與實現(xiàn)類

@Service
public class PaymentServiceImpl implements PaymentService {
 @Resource
 private PaymentDao paymentDao;

 @Override
 public int save(Payment payment) {
  return paymentDao.save(payment);
 }

 @Override
 public Payment getPaymentById(Long id) {
  return paymentDao.getPaymentById(id);
 }
}

controller

@RestController
@Slf4j
@RequestMapping("/payment")
public class PaymentController {
 @Resource
 private PaymentService paymentService;

 @PostMapping("/save")
 public CommonResult save(Payment payment) {
  int result = paymentService.save(payment);
  log.info("===> result: " + result);
  if(result > 0) {
   return new CommonResult(200,"保存到數(shù)據(jù)庫成功",result);
  }
  return new CommonResult(400,"保存到數(shù)據(jù)庫失敗",null);
 }

 @GetMapping("/get/{id}")
 public CommonResult<Payment> save(@PathVariable("id") Long id) {
  Payment paymentById = paymentService.getPaymentById(id);
  log.info("===> payment: " + paymentById);
  if(paymentById != null) {
   return new CommonResult(200,"查詢成功",paymentById);
  }
  return new CommonResult(400,"查詢失敗",null);
 }
}

3. 開啟Devtools熱部署

3.1 聚合父類總工程pom.xml添加配置

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

3.2 當(dāng)前工程添加devtools依賴

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-devtools</artifactId>
 <scope>runtime</scope>
 <optional>true</optional>
</dependency>

3.3 Idea開啟自動編譯選項

3.4 開啟熱注冊

快捷鍵 ctrl + shift + alt + /,打開Registry

勾選

重啟Idea,即可測試代碼時不用手動重啟

注意:該功能只能在開發(fā)階段使用,上線前一定要關(guān)閉

4. 微服務(wù)消費者訂單Module模塊

cloud-consumer-order80

4.1 pom.xml

<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.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>

4.2 yml配置文件

server:
  port: 80

4.3 主啟動類

4.4 RestTemplate引入

是什么

RestTemplate提供了多種便捷 訪問遠(yuǎn)程Http服務(wù) 的方法,是一種簡單便捷的訪問restful服務(wù)模板類,是Spring提供的用于訪問Rest服務(wù)的 客戶端模板工具類

如何使用

官網(wǎng)使用:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html

使用restTemplate訪問restful接口非常的簡單粗暴。

  • url REST請求地址
  • requestMap 請求參數(shù)
  • ResponseBean.classs HTTP響應(yīng)轉(zhuǎn)換被轉(zhuǎn)換成的對象類型

config配置類

@Configuration
public class ApplicationContextConfig {
 @Bean
 public RestTemplate getRestTemplate() {
  return new RestTemplate();
 }
}

4.5 業(yè)務(wù)類

entities

與提供者支付模塊一致

controller

@RestController
@Slf4j
@RequestMapping("/consumer")
public class OrderController {
 @Resource
 private RestTemplate restTemplate;

 private static final String PAYMENT_URL = "http://localhost:8001";

 @GetMapping("/payment/save")
 public CommonResult<Payment> save(Payment payment) {
  return restTemplate.postForObject(PAYMENT_URL + "/payment/save",
    payment,CommonResult.class);
 }

 @GetMapping("/payment/get/{id}")
 public CommonResult<Payment> get(@PathVariable("id") Long id) {
  return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id,
    CommonResult.class);
 }
}

4.6 啟動兩個模塊測試

啟動兩個模塊沒有出現(xiàn)Run Dashbord的問題

在工程目錄下找.idea文件夾下的workspace.xml添加如下代碼即可

<component name="RunDashboard">
 <option name="configurationTypes">
  <set>
  <option value="SpringBootApplicationConfigurationType" />
  </set>
 </option>
 <option name="ruleStates">
  <list>
  <RuleState>
   <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
  </RuleState>
  <RuleState>
   <option name="name" value="StatusDashboardGroupingRule" />
  </RuleState>
  </list>
 </option>
</component>

插入成功但是沒有內(nèi)容的問題

解決:支付模塊這里一定要加@RequestBody注解

@PostMapping("/save")
public CommonResult save(@RequestBody Payment payment) {
	int result = paymentService.save(payment);
	log.info("===> result: " + result);
	if(result > 0) {
	return new CommonResult(200,"保存到數(shù)據(jù)庫成功",result);
	}
	return new CommonResult(400,"保存到數(shù)據(jù)庫失敗",null);
}

5. 工程重構(gòu) - 公共工程模塊

5.1 發(fā)現(xiàn)問題 - 系統(tǒng)中有重復(fù)代碼 entities

5.2 新建公共工程 cloud-api-common

重復(fù)代碼,公共接口,第三方接口,工具類等都可以放在這里

pom.xml

Hutool是一個小而全的Java工具類庫,是項目中“util”包友好的替代

<dependencies>
 <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>
 <!-- hutool工具類 -->
 <dependency>
  <groupId>cn.hutool</groupId>
  <artifactId>hutool-all</artifactId>
  <version>5.1.0</version>
 </dependency>
</dependencies>

5.3 抽取多個模塊的共同代碼

將cloud-api-common安裝到maven倉庫

修改訂單80和支付8001代碼

  • 刪除各自原先的enntities
  • 各自添加pom內(nèi)容
<!-- 公共模塊 -->
<dependency>
 <groupId>com.polaris</groupId>
 <artifactId>cloud-api-common</artifactId>
 <version>${project.version}</version>
</dependency>

到此這篇關(guān)于SpringCloud基本Rest微服務(wù)工程搭建的文章就介紹到這了,更多相關(guān)SpringCloud微服務(wù)工程搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring?boot項目自定義參數(shù)校驗規(guī)則示例詳解

    spring?boot項目自定義參數(shù)校驗規(guī)則示例詳解

    這篇文章主要介紹了spring boot項目如何自定義參數(shù)校驗規(guī)則,自定義校驗規(guī)則和自帶的規(guī)則實現(xiàn)方式一樣,先自定義一個注解,然后指定校驗類,在校驗類里實現(xiàn)具體的校驗規(guī)則,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07
  • SpringBoot日志配置簡單介紹

    SpringBoot日志配置簡單介紹

    這篇文章主要介紹了SpringBoot日志配置,需要的朋友可以參考下
    2017-09-09
  • 解決bufferedReader.readLine()讀到最后發(fā)生阻塞的問題

    解決bufferedReader.readLine()讀到最后發(fā)生阻塞的問題

    這篇文章主要介紹了解決bufferedReader.readLine()讀到最后發(fā)生阻塞的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Spring Boot接口限流的常用算法及特點

    Spring Boot接口限流的常用算法及特點

    這篇文章主要給大家介紹了關(guān)于Spring Boot接口限流的常用算法及特點的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • SpringBoot中@ConfigurationProperties 配置綁定

    SpringBoot中@ConfigurationProperties 配置綁定

    本文主要介紹了SpringBoot中@ConfigurationProperties 配置綁定,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • Java 二叉樹遍歷的常用方法

    Java 二叉樹遍歷的常用方法

    二叉樹的遍歷可以說是解決二叉樹問題的基礎(chǔ)。我們常用的遍歷方式無外乎就四種 前序遍歷、中序遍歷、后續(xù)遍歷、層次遍歷 這四種。
    2021-05-05
  • Java 添加、讀取和刪除 Excel 批注的操作代碼

    Java 添加、讀取和刪除 Excel 批注的操作代碼

    這篇文章主要介紹了Java 添加、讀取和刪除 Excel 批注的操作方法,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-04-04
  • Springboot+Redis實現(xiàn)API接口限流的示例代碼

    Springboot+Redis實現(xiàn)API接口限流的示例代碼

    本文主要介紹了Springboot+Redis實現(xiàn)API接口限流的示例代碼,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • PowerJob的MapProcessor工作流程源碼解讀

    PowerJob的MapProcessor工作流程源碼解讀

    這篇文章主要為大家介紹了PowerJob的MapProcessor工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2024-01-01
  • Spring Boot詳解各類請求和響應(yīng)的處理方法

    Spring Boot詳解各類請求和響應(yīng)的處理方法

    平時只是在用SpringBoot框架,但并沒有詳細(xì)研究過請求和響應(yīng)執(zhí)行的一個具體過程,所以本文主要來梳理一下SpringBoot請求和響應(yīng)的處理過程
    2022-07-07

最新評論