最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringCloud基本Rest微服務工程搭建過程

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

1. 父工程構建

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è)欄點擊閃電圖標,使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. 微服務提供者支付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  # 當前數(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è)務類(簡單演示)

建表

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 當前工程添加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ā)階段使用,上線前一定要關閉

4. 微服務消費者訂單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提供了多種便捷 訪問遠程Http服務 的方法,是一種簡單便捷的訪問restful服務模板類,是Spring提供的用于訪問Rest服務的 客戶端模板工具類

如何使用

官網(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響應轉(zhuǎn)換被轉(zhuǎn)換成的對象類型

config配置類

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

4.5 業(yè)務類

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. 工程重構 - 公共工程模塊

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

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

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

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>

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

相關文章

  • MyBatis limit分頁設置的實現(xiàn)

    MyBatis limit分頁設置的實現(xiàn)

    這篇文章主要介紹了MyBatis limit分頁設置的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-04-04
  • spring boot與redis 實現(xiàn)session共享教程

    spring boot與redis 實現(xiàn)session共享教程

    這篇文章主要介紹了spring boot與redis 實現(xiàn)session共享教程,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-04-04
  • 盤點Java中延時任務的多種實現(xiàn)方式

    盤點Java中延時任務的多種實現(xiàn)方式

    當需要一個定時發(fā)布系統(tǒng)通告的功能,如何實現(xiàn)??當支付超時,訂單自動取消,如何實現(xiàn)?其實這些問題本質(zhì)都是延時任務的實現(xiàn),本文為大家盤點了多種常見的延時任務實現(xiàn)方法,希望對大家有所幫助
    2022-12-12
  • Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎

    Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎

    這篇文章主要介紹了Mybatis接口Mapper內(nèi)的方法為啥不能重載嗎,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • Spring Boot中的JdbcTemplate是什么及用法小結

    Spring Boot中的JdbcTemplate是什么及用法小結

    Spring Boot中的JdbcTemplate是一個強大的數(shù)據(jù)庫訪問工具,它簡化了數(shù)據(jù)庫操作的過程,在本文中,我們了解了JdbcTemplate的基本概念,并演示了如何在Spring Boot應用程序中使用它,感興趣的朋友跟隨小編一起看看吧
    2023-10-10
  • SpringCloud負載均衡實現(xiàn)定向路由詳情

    SpringCloud負載均衡實現(xiàn)定向路由詳情

    這篇文章主要介紹了SpringCloud負載均衡實現(xiàn)定向路由詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-08-08
  • spring中AOP 注解開發(fā)示例詳解

    spring中AOP 注解開發(fā)示例詳解

    這篇文章主要介紹了spring中AOP注解開發(fā)的相關資料,文中介紹的很詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-02-02
  • 通過實例解析Java不可變對象原理

    通過實例解析Java不可變對象原理

    這篇文章主要介紹了通過實例解析Java不可變對象原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-10-10
  • SpringBoot Shiro 權限注解不起作用的解決方法

    SpringBoot Shiro 權限注解不起作用的解決方法

    本文主要介紹了SpringBoot Shiro 權限注解不起作用的解決方法,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • idea同步y(tǒng)api插件的詳細步驟

    idea同步y(tǒng)api插件的詳細步驟

    yapi是一個很好的接口文檔維護工具,其swagger功能,可將接口信息同步到y(tǒng)api平臺上,這篇文章主要介紹了idea同步y(tǒng)api插件,需要的朋友可以參考下
    2024-04-04

最新評論

会同县| 含山县| 乌鲁木齐市| 光山县| 阿鲁科尔沁旗| 乐亭县| 乳山市| 南康市| 广安市| 贵南县| 会泽县| 松江区| 刚察县| 湘乡市| 莆田市| 灵丘县| 遵化市| 从化市| 和林格尔县| 临邑县| 肇州县| 龙山县| 晋江市| 聂拉木县| 托克逊县| 威宁| 青铜峡市| 眉山市| 株洲县| 曲靖市| 布尔津县| 宁安市| 红河县| 八宿县| 兴山县| 印江| 古交市| 乐安县| 河南省| 崇义县| 怀化市|