第一個(gè)微服務(wù)應(yīng)用

(git上的源碼:https://gitee.com/rain7564/spring_microservices_study/tree/master/first-microservice)

使用idea創(chuàng)建一個(gè)基于Spring Cloud的微服務(wù)應(yīng)用
  1. 點(diǎn)擊File -> New -> Project,然后出現(xiàn)如下彈窗:


    image.png
  2. 然后點(diǎn)擊Next,輸入一些必要信息,如下:


    image.png
  3. 點(diǎn)擊Next,如下:


    image.png

注:如果沒得選(1.5.8),可以隨便選一個(gè),然后生成項(xiàng)目后,將版本號改成1.5.8.RELEASE即可。

  1. 點(diǎn)擊Next,最后輸入Project name和Project location即可,如下:


    image.png
  2. 等從遠(yuǎn)程下載項(xiàng)目的.zip文件并解壓后,idea會(huì)自動(dòng)將jar包導(dǎo)入,第一次創(chuàng)建Spring Boot項(xiàng)目,加載時(shí)間可能會(huì)比較久,現(xiàn)在可以先喝杯飲料壓壓驚~~~
  3. 等項(xiàng)目加載完,第一個(gè)微服務(wù)應(yīng)用的雛形就完成了,目錄結(jié)構(gòu)如下:


    image.png

    標(biāo)準(zhǔn)的maven項(xiàng)目,我們重點(diǎn)關(guān)注以下幾個(gè)文件:
    ① Application.java:Spring Boot工程的啟動(dòng)類(已被重命名),打開該文件,如下:

//該注解表明該類是項(xiàng)目(微服務(wù))的啟動(dòng)類
@SpringBootApplication
public class Application {
    //運(yùn)行該方法,會(huì)啟動(dòng)整個(gè)Spring Boot服務(wù)
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

② application.properties: Spring Boot項(xiàng)目的配置文件。該配置文件可以用來配置諸如數(shù)據(jù)源等的配置信息。該文件是properties文件,不過Spring Boot還支持另一種文件格式——application.yml,該教程所有配置文件都會(huì)使用這種文件格式來配置各種配置信息。
以后還會(huì)有一個(gè)配置文件——bootstrap.yml,后文會(huì)詳講。
雖說是配置文件,但當(dāng)配置文件為空,整個(gè)項(xiàng)目也能跑起來。
③ .gitignore: 剛創(chuàng)建的項(xiàng)目并沒有該文件,是后來加上去的,若不需要push到git,可以忽略該文件。
④ pom.xml: 寫過maven工程的道友肯定對這個(gè)文件很熟悉。沒錯(cuò),這個(gè)微服務(wù)實(shí)際上就是一個(gè)maven工程,只是該文件中默認(rèn)加入了Spring Boot的Maven插件(<plugins>標(biāo)簽)、從spring-boot-starter-parent繼承的版本號(<parent>標(biāo)簽)和Spring Boot框架的啟動(dòng)依賴(<dependencies>標(biāo)簽)。

  1. 因?yàn)槲覀円獎(jiǎng)?chuàng)建的是基于Spring Cloud的微服務(wù)應(yīng)用,所以需要對剛剛創(chuàng)建的項(xiàng)目進(jìn)行改造,讓剛剛創(chuàng)建的項(xiàng)目變成一個(gè)父項(xiàng)目,并創(chuàng)建一個(gè)子項(xiàng)目:
  • 刪掉下圖選中的文件/文件:


    image.png
  • 修改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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.study.microservice</groupId>
    <artifactId>first-microservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

    <name>first-microservice</name>
    <description></description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.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>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RELEASE</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>
        </plugins>
    </build>

</project>
  • 創(chuàng)建一個(gè)微服務(wù)--licenses-service:
    • 右鍵項(xiàng)目名,依次選擇New->Module:


      image.png
    • 創(chuàng)建license-service微服務(wù),可參考以下步驟(類似步驟1-4):
image.png
image.png
image.png

點(diǎn)擊Finish,項(xiàng)目加載完成后,整個(gè)工程的目錄樹如下:

image.png

簡單說明圖中部分文件/文件夾:
① license-service:剛剛新建的微服務(wù)。一個(gè)基于Spring Cloud的微服務(wù)應(yīng)用,可以包含多個(gè)這樣的微服務(wù)。
② LicenseServiceApplication:license-service微服務(wù)的啟動(dòng)類。
③ 文件夾static、templates:static存放類似.js、.jpg等靜態(tài)資源,templates存放.html文件等。接下來都不涉及頁面的開發(fā),所以這兩個(gè)文件夾可以刪掉。
④ application.properties:配置文件。
⑤ pom.xml:pom文件。打開文件,可以看到跟剛剛創(chuàng)建的父工程first-microservice的pom文件類似,但是<dependencies>標(biāo)簽中的依賴略有不同,多了"spring-boot-starter-web"、"mysql-connector-java",這兩個(gè)就是剛剛創(chuàng)建時(shí)勾選的"Web"、"MySQL"。也就是說需要什么依賴,可以在創(chuàng)建時(shí)勾選對應(yīng)的jar包即可。同樣你也可以直接編輯pom文件,比如添加依賴"spring-boot-starter-data-jpa"。不過一般都是直接跳過勾選,需要什么直接編輯pom文件。你可能會(huì)問,那pom文件怎么知道引用哪個(gè)版本的依賴,答案可以參考spring boot的maven配置依賴。
從maven結(jié)構(gòu)來看,first-microservice是license-service的父項(xiàng)目,所以可以對license-service項(xiàng)目的pom文件進(jìn)行修改,去掉<build>、<properties>標(biāo)簽,修改<parent>標(biāo)簽。如下:

<?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>cn.study.microservice</groupId>
    <artifactId>license-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>license-service</name>
    <description></description>

    <parent>
        <groupId>cn.study.microservice</groupId>
        <artifactId>first-microservice</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

可以看到<parent>引用了父項(xiàng)目first-microservice,license-service的pom文件繼承了first-microservice的pom文件。

  • 刪除下圖選中的文件/文件夾,并重命名啟動(dòng)類和配置文件:
image.png
  • 創(chuàng)建License.java和LicenseController.java:
package cn.study.microservice.license.domain;

public class License{
    private String id;
    private String organizationId;
    private String productName;
    private String licenseType;

    public String getId() {
        return id;
    }

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

    public String getOrganizationId() {
        return organizationId;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getLicenseType() {
        return licenseType;
    }

    public void setLicenseType(String licenseType) {
        this.licenseType = licenseType;
    }

    public License withId(String id){
        this.setId( id );
        return this;
    }

    public License withOrganizationId(String organizationId){
        this.setOrganizationId(organizationId);
        return this;
    }

    public License withProductName(String productName){
        this.setProductName(productName);
        return this;
    }

    public License withLicenseType(String licenseType){
        this.setLicenseType(licenseType);
        return this;
    }
}
/**
 * 該注解相當(dāng)于@ResponseBody + @Controller合在一起的作用
 * 會(huì)將request/response序列化/反序列化為JSON格式
 */
@RestController
@RequestMapping(value="v1/organizations/{organizationId}/licenses")
public class LicenseServiceController {

    @RequestMapping(value="/{licenseId}",method = RequestMethod.GET)
    public License getLicenses( @PathVariable("organizationId") String organizationId,
                                @PathVariable("licenseId") String licenseId) {
        //@PathVariable能將URL中{organizationId}、{licenseId}映射到方法的變量organizationId、licenseId
        return new License()
                .withId(licenseId)
                .withOrganizationId(organizationId)
                .withProductName("Teleco")
                .withLicenseType("Seat");
    }

    @RequestMapping(value="{licenseId}",method = RequestMethod.PUT)
    public String updateLicenses( @PathVariable("licenseId") String licenseId) {
        return String.format("This is the put");
    }

    @RequestMapping(value="{licenseId}",method = RequestMethod.POST)
    public String saveLicenses( @PathVariable("licenseId") String licenseId) {
        return String.format("This is the post");
    }

    @RequestMapping(value="{licenseId}",method = RequestMethod.DELETE)
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public String deleteLicenses( @PathVariable("licenseId") String licenseId) {
        return String.format("This is the Delete");
    }

}
  • 啟動(dòng),以下為idea的幾種啟動(dòng)方式:
image.png

啟動(dòng)后,可在控制臺看到類似下圖的打印效果:


image.png
  • 測試,驗(yàn)證結(jié)果
    在瀏覽器或使用postman進(jìn)行調(diào)試。

至此,第一個(gè)基于Spring Cloud的微服務(wù)應(yīng)用搭建完成,這只是一個(gè)最簡單的微服務(wù)應(yīng)用,還沒涉及到Spring Cloud的五大神獸:

  • 服務(wù)發(fā)現(xiàn)——Netflix Eureka
  • 客服端負(fù)載均衡——Netflix Ribbon
  • 斷路器——Netflix Hystrix
  • 服務(wù)網(wǎng)關(guān)——Netflix Zuul
  • 分布式配置——Spring Cloud Config

下一節(jié),將介紹第一個(gè)神獸:Netflix Eureka(服務(wù)注冊與發(fā)現(xiàn))。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容