SpringCloud(第 038 篇)idea環境熱部署微服務開發

SpringCloud(第 038 篇)idea環境熱部署微服務開發

-java

1、大體介紹

一、咱們在開發過程當中,會常常須要啓動、中止微服務,並且有時候會比較頻繁,須要不停的操做啓停動做;
二、而咱們本章節主要講解下如何在開發環境中進行熱部署,這樣的話能夠在必定程度上專心敲代碼,兩耳不聞窗外事,一心只想敲代碼;

2、實現步驟

2.1 添加 maven 引用包

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

    <artifactId>springms-simple-provider-user-devtools</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <!-- 訪問數據庫模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- web模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- h2數據庫模塊 -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- 支持Apache Solr搜索平臺,包括spring-data-solr -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-solr</artifactId>
        </dependency>

        <!-- 熱部署模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.2 添加應用配置文件(springms-simple-provider-user-devtoolssrcmainresourcesapplication.yml)

server:
  port: 8305
spring:
  application:
    name: springms-simple-provider-user-devtools  #所有小寫
  jpa:
    generate-ddl: false
    show-sql: true
    hibernate:
      ddl-auto: none
  datasource:
    platform: h2
    schema: classpath:schema.sql
    data: classpath:data.sql
  thymeleaf:
    cache: false
logging:
  level:
    root: INFO
    org.hibernate: INFO
    org.hibernate.type.descriptor.sql.BasicBinder: TRACE
    org.hibernate.type.descriptor.sql.BasicExtractor: TRACE
    com.springms: DEBUG

2.3 添加 H2 數據庫腳本(springms-simple-provider-user-devtoolssrcmainresourcesschema.sql)

drop table user if exists;

CREATE TABLE USER(
    id BIGINT GENERATED by default as identity,
    username VARCHAR(40),
    name VARCHAR(20),
    age int(3),
    balance DECIMAL(10, 2),
    PRIMARY KEY(id)
);

2.4 插入 H2 數據庫一些初始化數據(springms-simple-provider-user-devtoolssrcmainresourcesdata.sql)

INSERT into user (id, username, name, age, balance) values (1, 'user1', '張三', 20, 100.00);
INSERT into user (id, username, name, age, balance) values (2, 'user2', '李四', 22, 100.00);
INSERT into user (id, username, name, age, balance) values (3, 'user3', '王五', 24, 100.00);
INSERT into user (id, username, name, age, balance) values (4, 'user4', '趙六', 26, 100.00);
INSERT into user (id, username, name, age, balance) values (5, 'user5', '李逵', 27, 100.00);
INSERT into user (id, username, name, age, balance) values (6, 'user6', '張遠', 10, 100.00);
INSERT into user (id, username, name, age, balance) values (7, 'user7', '迪拜', 60, 100.00);
INSERT into user (id, username, name, age, balance) values (8, 'user8', '哈士奇', 40, 100.00);
INSERT into user (id, username, name, age, balance) values (9, 'user9', '關羽', 30, 100.00);

2.5 添加訪問底層數據模型的DAO接口(springms-simple-provider-user-devtoolssrcmainjavacomspringmscloudrepositoryUserRepository.java)

package com.springms.cloud.repository;

import com.springms.cloud.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {

}

2.6 添加實體用戶類User(springms-simple-provider-user-devtoolssrcmainjavacomspringmscloudentityUser.java)

package com.springms.cloud.entity;

import java.math.BigDecimal;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  @Column
  private String username;

  @Column
  private String name;

  @Column
  private Short age;

  @Column
  private BigDecimal balance;

  public Long getId() {
    return this.id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  public String getUsername() {
    return this.username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getName() {
    return this.name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Short getAge() {
    return this.age;
  }

  public void setAge(Short age) {
    this.age = age;
  }

  public BigDecimal getBalance() {
    return this.balance;
  }

  public void setBalance(BigDecimal balance) {
    this.balance = balance;
  }
}

2.7 添加用戶Web訪問層Controller(springms-simple-provider-user-devtools/src/main/java/com/springms/cloud/controller/SimpleProviderUserDevtoolsController.java)

package com.springms.cloud.controller;

import com.springms.cloud.entity.User;
import com.springms.cloud.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用戶微服務Controller(支持 idea 熱部署)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@RestController
public class SimpleProviderUserDevtoolsController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/simple/{id}")
    public User findById(@PathVariable Long id) {
        return this.userRepository.findOne(id);
    }

    @GetMapping("simple")
    public String simple() {
        return "simple-2017-09";
    }

    @GetMapping("simple2")
    public String simple2() {
        return "simple2-2017";
    }
}

2.8 添加簡單用戶微服務啓動類(springms-simple-provider-user-devtoolssrcmainjavacomspringmscloudMsSimpleProviderUserApplication.java)

package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * 簡單用戶微服務類(支持 idea 熱部署)。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 17/10/18
 *
 */
@SpringBootApplication
public class MsSimpleProviderUserDevtoolsApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsSimpleProviderUserDevtoolsApplication.class, args);
        System.out.println("【【【【【【 簡單用戶微服務(熱部署) 】】】】】】已啓動.");
    }
}

3、測試

/****************************************************************************************
 1、簡單用戶微服務接口測試:

 一、添加 spring-boot-devtools 熱部署依賴;
 二、pom.xml 添加 <fork>true</fork> 屬性;
 三、設置 idea 屬性:「File」 -> 「Settings」 -> 「Build,Execution,Deplyment」 -> 「Compiler」,選中打勾 「Build project automatically」 ;
 四、設置 idea 屬性:組合鍵:「Shift+Ctrl+Alt+/」 ,選擇 「Registry」 ,選中打勾 「compiler.automake.allow.when.app.running」 ;
 五、設置緩存配置 spring.thymeleaf.cache=false;
 六、設置 chrome 屬性:F12或者「Ctrl+Shift+I」,打開開發者工具,「Network」 選項卡下 選中打勾 「Disable Cache(while DevTools is open)」 ;

 七、啓動 springms-simple-provider-user-devtools 模塊服務,啓動1個端口;
 八、在瀏覽器輸入地址 http://localhost:8305/simple 能夠看到信息成功的被打印出來;

 九、改動 Controller 某個方法的返回值,或者新增方法,而後會看到控制檯自動重啓該應用,進行熱部署;
 十、再在瀏覽器請求剛剛改動的連接,發現連接返回的內容確實動態改變過來了,這就是熱部署;
 ****************************************************************************************/

4、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.gitgit

SpringCloudTutorial交流QQ羣: 235322432web

SpringCloudTutorial交流微信羣: 微信溝通羣二維碼圖片連接spring

歡迎關注,您的確定是對我最大的支持!!!sql

相關文章
相關標籤/搜索