spring-cloud-square開發實戰(三種型別全覆蓋)

程式設計師欣宸發表於2021-11-01

歡迎訪問我的GitHub

這裡分類和彙總了欣宸的全部原創(含配套原始碼):https://github.com/zq2599/blog_demos

本篇概覽

  • 前文《五分鐘搞懂spring-cloud-square》詳細介紹了什麼是spring-cloud-square,以及三種實現型別的詳細概念,愛動手的您已迫不及待想編碼體驗spring-cloud-square了,本篇我們們就來暢快實戰,體驗這個spring官方帶給我們的smart client
  • 如標題所述,接下里我們們會將spring-cloud-square提供的三種client都編碼體驗,總的來說本篇由以下內容構成:
  1. 新建maven工程,名為<font color="blue">spring-cloud-square-tutorials</font>,這是本篇所有應用的父工程,庫版本在此工程中統一管理;
  2. 建立子工程<font color="blue">eureka</font>,作為註冊中心
  3. 建立子工程<font color="blue">client</font>,放一些公用的資料結構
  4. 建立子工程<font color="blue">provider</font>,身份是服務提供者,接下來的三個用到spring-cloud-square的子工程,都呼叫provider的服務
  5. 建立子工程<font color="blue">consumer-okhttp</font>,基於spring-cloud-square的<font color="red">okhttp</font>能力做遠端呼叫
  6. 建立子工程<font color="blue">consumer-retrofit-okhttp</font>,基於spring-cloud-square的<font color="red">retrofit + okhttp</font>能力做遠端呼叫
  7. 建立子工程<font color="blue">consumer-retrofit-webflux</font>,基於spring-cloud-square的<font color="red">retrofit + webflux</font>能力做遠端呼叫
  • 上述幾個服務的關係如下圖:

在這裡插入圖片描述

如何驗證

  • 程式碼寫完之後,如何驗證功能是否符合預期呢?本篇採用單元測試的方式,consumer-okhttp、consumer-retrofit-okhttp、consumer-retrofit-webflux這三個子工程都有自己的單元測試程式碼,執行通過就意味著程式碼功能符合預期了

原始碼下載

名稱連結備註
專案主頁https://github.com/zq2599/blo...該專案在GitHub上的主頁
git倉庫地址(https)https://github.com/zq2599/blo...該專案原始碼的倉庫地址,https協議
git倉庫地址(ssh)git@github.com:zq2599/blog_demos.git該專案原始碼的倉庫地址,ssh協議
  • 這個git專案中有多個資料夾,本篇的原始碼在<font color="blue">spring-cloud-square-tutorials</font>資料夾下,如下圖紅框所示:
    在這裡插入圖片描述

版本資訊

  • 本篇實戰涉及到的主要版本情況如下:
  • JDK:1.8.0_291
  • IDEA:2021.1.3 (Ultimate Edition)
  • maven:3.8.1
  • 作業系統:win10 64位
  • springboot:2.4.4
  • spring-cloud:2020.0.2
  • spring-cloud-square:0.4.0-SNAPSHOT

父工程spring-cloud-square-tutorials

  • 父工程名為<font color="blue">spring-cloud-square-tutorials</font>,其pom.xml如下,除了依賴庫的版本在此統一管理,還要注意的是兩個倉庫的引入(https://repo.spring.io/snapshothttps://repo.spring.io/milestone),引入它們是因為spring-cloud-square還在孵化階段,沒有釋出到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>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.bolingcavalry</groupId>
    <artifactId>spring-cloud-square-tutorials</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.2</spring-cloud.version>
        <square.dependency.version>0.4.0-SNAPSHOT</square.dependency.version>
    </properties>

    <packaging>pom</packaging>
    <description>Demo project for Spring Cloud Square Retrofit Web</description>

    <modules>
        <module>provider</module>
        <module>eureka</module>
        <module>consumer-okhttp</module>
        <module>client</module>
        <module>consumer-retrofit-okhttp</module>
        <module>consumer-retrofit-webflux</module>
    </modules>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>com.squareup.okhttp3</groupId>
                <artifactId>okhttp</artifactId>
                <version>3.14.9</version>
                <scope>compile</scope>
            </dependency>

            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-classic</artifactId>
                <version>1.1.7</version>
            </dependency>

            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.16.16</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-square-okhttp</artifactId>
                <version>${square.dependency.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-square-retrofit</artifactId>
                <version>${square.dependency.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-boot-starter-webflux</artifactId>
                <version>${square.dependency.version}</version>
            </dependency>

            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-square-retrofit-webclient</artifactId>
                <version>${square.dependency.version}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <!--skip deploy (this is just a test module) -->
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>
</project>

註冊中心eureka

  • eureka應用並沒有什麼特別之處,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>spring-cloud-square-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka</artifactId>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>com.bolingcavalry.eureka.EurekaApplication</start-class>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
<!--                <version>Finchley.BUILD-SNAPSHOT</version>-->
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>

            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- defined in spring-cloud-starter-parent pom (as documentation hint),
                    but needs to be repeated here -->
                <configuration>
                    <requiresUnpack>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-core</artifactId>
                        </dependency>
                        <dependency>
                            <groupId>com.netflix.eureka</groupId>
                            <artifactId>eureka-client</artifactId>
                        </dependency>
                    </requiresUnpack>
                </configuration>
            </plugin>

            <plugin>
                <!--skip deploy (this is just a test module) -->
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  • 中規中矩的配置檔案application.yml,埠是8761,後面的應用也要保持一致:
server:
  port: 8761

spring:
  application:
    name: eureka

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
  server:
    waitTimeInMsWhenSyncEmpty: 0
  • 啟動類EurekaApplication.java,記得用註解EnableEurekaServer開啟eureka服務:
package com.bolingcavalry.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(EurekaApplication.class, args);
    }
}
  • eureka應用已經完成,接下來是服務提供者了

服務提供者provider

  • -新建名為<font color="blue">provider</font>的應用,pom.xml如下,可見是個普通的web工程,會將自己註冊到eureka上去:
<?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>spring-cloud-square-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>client</artifactId>
            <version>${project.version}</version>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

    <build>
        <plugins>
            <!-- 如果父工程不是springboot,就要用以下方式使用外掛,才能生成正常的jar -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bolingcavalry.provider.ProviderApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 配置檔案application.yml:
spring:
  application:
    name: provider

server:
  port: 18080

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 啟動類ProviderApplication .java:
package com.bolingcavalry.provider;

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

@SpringBootApplication
public class ProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class, args);
    }
}
  • web服務類,可見對外提供了兩個介面<font color="blue">hello-str</font>和<font color="blue">hello-obj</font>,前者返回字串,或者返回物件:
package com.bolingcavalry.provider.controller;

import com.bolingcavalry.client.HelloResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
public class Hello {

    public static final String HELLO_PREFIX = "Hello World";

    @Autowired
    DiscoveryClient client;

    /**
     * 隨機取一個provider例項,返回其描述資訊,
     * 如果只有一個provider例項時,返回的就是當前服務資訊
     * @return
     */
    private String providerDescription() {
        List<ServiceInstance> instances = client.getInstances("provider");
        ServiceInstance selectedInstance = instances
                .get(new Random().nextInt(instances.size()));

        return String.format("serviceId [%s], host [%s], port [%d]",
                selectedInstance.getServiceId(),
                selectedInstance.getHost(),
                selectedInstance.getPort());
    }

    private String dateStr(){
        return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
    }

    @GetMapping("/hello-str")
    public String helloStr() {
        List<ServiceInstance> instances = client.getInstances("provider");
        ServiceInstance selectedInstance = instances
                .get(new Random().nextInt(instances.size()));
        return HELLO_PREFIX
                + " : "
                + providerDescription()
                + ", "
                + dateStr();
    }
    
    @GetMapping("/hello-obj")
    public HelloResponse helloObj(@RequestParam("name") String name) {
        return new HelloResponse(name, dateStr(), providerDescription());
    }
}
  • 這個provider應用算是個最樸實無華的web服務了

啟動服務

  • 現在可以將eureka和provider服務先後啟動,這樣後面的應用編碼完成後可以直接測試

consumer-okhttp,基於spring-cloud-square的okhttp能力

  • 接下來要建立的應用<font color="blue">consumer-okhttp</font>,使用的是spring-cloud-square三種能力的第一種:okhttp
  • pom.xml內容如下,重點是spring-cloud-square-okhttp和spring-cloud-starter-loadbalancer這兩個庫的引入:
<?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>spring-cloud-square-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-okhttp</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>client</artifactId>
            <version>${project.version}</version>
        </dependency>

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

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

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-square-okhttp</artifactId>
            <version>0.4.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- 如果父工程不是springboot,就要用以下方式使用外掛,才能生成正常的jar -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.bolingcavalry.ConsumerApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  • 配置檔案application.yml,還是常見的那幾個配置:應用名、埠、eureka:
spring:
  application:
    name: consumer-okhttp

server:
  port: 18081

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 啟動類:
package com.bolingcavalry.consumer;

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

@SpringBootApplication
public class OkhttpApplication {
    public static void main(String[] args) {
        SpringApplication.run(OkhttpApplication.class, args);
    }
}
  • 接下來是重要的配置類OkHttpClientConfig.java,用於例項化OkHttpClient.Builder物件並註冊到spring環境:
package com.bolingcavalry.consumer;

import okhttp3.OkHttpClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
class OkHttpClientConfig{
    @Bean
    @LoadBalanced
    public OkHttpClient.Builder okHttpClientBuilder() {
        return new OkHttpClient.Builder();
    }
}
  • 然後就可以使用這個Builder來建立OkHttpClient例項了,如下所示,可見入參request的url欄位裡使用了服務名<font color="blue">provider</font>,相當於OkHttpClient內如也能通過服務名取得具體的服務地址,至於是如何獲取的,會在後面的文章詳細分析,整段程式碼除了url使用服務名,並沒有什麼值得關注的地方了,普通的OkHttpClient使用而已:
package com.bolingcavalry.consumer.controller;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController
public class RemoteHello {
    @Autowired
    private OkHttpClient.Builder builder;

    @GetMapping("/remote-str")
    public String hello() throws IOException {
        // 直接使用服務名
        Request request = new Request.Builder().url("http://provider/hello-str").build();

        // 遠端訪問
        Response response = builder.build().newCall(request).execute();

        return "get remote response : " + response.body().string();
    }
}
  • 接下來看看單元測試程式碼,使用MockMvcRequestBuilders構造http請求,檢查返回碼和返回內容:
package com.bolingcavalry.consumer.controller;

import com.bolingcavalry.client.Constants;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class RemoteHelloTest {

    @Autowired
    private MockMvc mvc;

    @Test
    void hello() throws Exception {
        String responseString = mvc.perform(MockMvcRequestBuilders.get("/remote-str").accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().string(containsString(Constants.HELLO_PREFIX)))
                .andDo(print())
                .andReturn()
                .getResponse()
                .getContentAsString();

        log.info("response in junit test :\n" + responseString);
    }
}
  • 如果eureka和provider都執行起來了,那麼此時可以直接執行單元測試類,順利通過測試,如下圖:

在這裡插入圖片描述

consumer-retrofit-okhttp,基於spring-cloud-square的okhttp能力

  • 接下來的兩個應用都使用了當下熱門的retrofit,再搭配Spring Cloud LoadBalance實現服務註冊發現,當然了retrofit自身無法完成網路請求處理,要依賴其他庫,先看okhttp庫的
  • 新建應用<font color="blue">consumer-retrofit-okhttp</font>,其pom.xml如下,要注意的必須依賴spring-cloud-square-retrofit和spring-cloud-square-okhttp,另外,為了:
<?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>spring-cloud-square-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>consumer-retrofit-okhttp</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-square-retrofit</artifactId>
            <version>0.4.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-square-okhttp</artifactId>
            <version>0.4.0-SNAPSHOT</version>
        </dependency>

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!--skip deploy (this is just a test module) -->
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 配置檔案:
spring:
  application:
    name: consumer-retrofit-okhttp
server:
  port: 18082
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 啟動類:
package com.bolingcavalry.consumer;

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

@SpringBootApplication
public class RetrofitOkhttpApplication {
    public static void main(String[] args) {
        SpringApplication.run(RetrofitOkhttpApplication.class, args);
    }
}
  • 配置類,和前一個應用的沒啥區別,想想也是,底層可不都是okhttp麼:
package com.bolingcavalry.consumer;

import okhttp3.OkHttpClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.square.retrofit.EnableRetrofitClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRetrofitClients
class OkHttpClientConfig{
    @Bean
    @LoadBalanced
    public OkHttpClient.Builder okHttpClientBuilder() {
        return new OkHttpClient.Builder();
    }
}
  • 接下來,有趣的部分出現了,先定義HelloService.java,裡面的註解RetrofitClient指定了對應的服務名<font color="blue">provider</font>,在hello方法生,用GET註解指定了provider提供的web介面,而且hello方法的返回值Call<HelloResponse>,和provider服務中hello-obj的返回值HelloResponse也是對應的,還有就是hello的入參對應著provider服務中hello-obj的入參,很熟悉吧,確實,和feign太像了:
package com.bolingcavalry.consumer.service;

import com.bolingcavalry.client.HelloResponse;
import org.springframework.cloud.square.retrofit.core.RetrofitClient;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

@RetrofitClient("provider")
public interface HelloService {

    @GET("/hello-obj")
    Call<HelloResponse> hello(@Query("name") String name);
}
  • 接下來是呼叫provider服務中hello-obj介面的程式碼RemoteHello.java,如下所示,神奇的一幕出現了,剛才我們們只寫了HelloService介面,並沒有寫它的實現,但是通過Autowired註解卻能 從spring環境拿到例項直接使用,在hello方法中,並沒有見到遠端呼叫的程式碼,而是執行helloService.hello,就能發起遠端呼叫,拿到provider返回的結果:
package com.bolingcavalry.consumer.controller;

import com.bolingcavalry.client.HelloResponse;
import com.bolingcavalry.consumer.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;

@RestController
public class RemoteHello {
    @Autowired(required = false)
    HelloService helloService;

    @GetMapping("/remote-obj")
    public HelloResponse hello(@RequestParam("name") String name) throws IOException {
        return helloService.hello(name).execute().body();
    }
}
  • 看到這裡,聰明的您一定會覺得欣宸就是個<font color="blue">沒見過世面的鄉巴佬</font>:定義HelloService 介面,無需開發實現類,這玩意在mybatis不就有了嘛,居然敢說"神奇",我覺得您說得對,欣宸確實沒見識,大驚小怪的...
  • 單元測試類如下,由於返回的是json物件,因此可以用andExpect方法再配合MockMvcResultMatchers,對json進行檢查:
package com.bolingcavalry.consumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class RemoteHelloTest {

    private MockMvc mvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @BeforeEach
    public void setUp() {
        // 在單元測試的時候,MockHttpServletResponse例項的characterEncoding預設是ISO-8859-1,
        // 得到的字串列印出來也是亂碼,
        // 下面的設定可以解決此問題
        if (null==mvc) {
            mvc = MockMvcBuilders
                    .webAppContextSetup(webApplicationContext)
                    .addFilter((request, response, chain) -> {
                        response.setCharacterEncoding("UTF-8"); // this is crucial
                        chain.doFilter(request, response);
                    }, "/*")
                    .build();
        }
    }

    @Test
    void hello() throws Exception {
        // 請求引數是使用者名稱,實時生成一個
        String name = System.currentTimeMillis() + "程式設計師A";

        // 請求
        String responseString = mvc.perform(
                MockMvcRequestBuilders
                        .get("/remote-obj")
                        .param("name", name)
                        .accept(MediaType.APPLICATION_JSON)
        )
                .andExpect(status().isOk())                           // 驗證狀態
                .andExpect(jsonPath("$.name", is(name)))    // 驗證json中返回的欄位是否含有name
                .andDo(print())
                .andReturn()
                .getResponse()
                .getContentAsString();

        log.info("response in junit test :\n" + responseString);
    }
}
  • 執行單元測試,如下圖,順利通過:

在這裡插入圖片描述

consumer-retrofit-webflux,基於spring-cloud-square的retrofit + webflux

  • 最後登場的是consumer-retrofit-webflux,pom.xml如下,依賴庫是spring-cloud-square-retrofit + spring-boot-starter-webflux的組合:
<?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>spring-cloud-square-tutorials</artifactId>
        <groupId>com.bolingcavalry</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer-retrofit-webflux</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.bolingcavalry</groupId>
            <artifactId>client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-square-retrofit</artifactId>
            <version>0.4.0-SNAPSHOT</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-square-retrofit-webclient</artifactId>
        </dependency>

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

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!--skip deploy (this is just a test module) -->
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
  • 配置檔案application.yml:
spring:
  application:
    name: consumer-retrofit-webflux

server:
  port: 18083

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  • 啟動類RetrofitWebfluxApplication.java

    package com.bolingcavalry.consumer;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class RetrofitWebfluxApplication {
      public static void main(String[] args) {
          SpringApplication.run(RetrofitWebfluxApplication.class, args);
      }
    }
  • 配置類AppConfiguration.java,使用的註解是EnableRetrofitClients,例項化的Buider物件是WebClient.Builder,和前面的不一樣,要格外注意:
package com.bolingcavalry.consumer;

import okhttp3.OkHttpClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.square.retrofit.webclient.EnableRetrofitClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
@EnableRetrofitClients
class AppConfiguration {
    @Bean
    @LoadBalanced
    public WebClient.Builder builder() {
        return WebClient.builder();
    }
}
  • 接下來是介面定義,注意hello方法的返回值是<font color="blue">Mono<HelloResponse></font>,這是weflux風格的返回值,代表非同步的0個或一個元素:
package com.bolingcavalry.consumer.service;

import com.bolingcavalry.client.HelloResponse;
import org.springframework.cloud.square.retrofit.core.RetrofitClient;
import reactor.core.publisher.Mono;
import retrofit2.http.GET;
import retrofit2.http.Query;

@RetrofitClient("provider")
public interface HelloService {

    @GET("/hello-obj")
    Mono<HelloResponse> hello(@Query("name") String name);
}
  • 最後是單元測試類,和前面的沒啥區別:
package com.bolingcavalry.consumer.controller;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
@AutoConfigureMockMvc
@Slf4j
class RemoteHelloTest {

    private MockMvc mvc;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @BeforeEach
    public void setUp() {
        // 在單元測試的時候,MockHttpServletResponse例項的characterEncoding預設是ISO-8859-1,
        // 得到的字串列印出來也是亂碼,
        // 下面的設定可以解決此問題
        if (null==mvc) {
            mvc = MockMvcBuilders
                    .webAppContextSetup(webApplicationContext)
                    .addFilter((request, response, chain) -> {
                        response.setCharacterEncoding("UTF-8"); // this is crucial
                        chain.doFilter(request, response);
                    }, "/*")
                    .build();
        }
    }

    @Test
    void hello() throws Exception {
        // 請求引數是使用者名稱,實時生成一個
        String name = System.currentTimeMillis() + "程式設計師B";

        // 請求
        String responseString = mvc.perform(
                MockMvcRequestBuilders
                        .get("/remote-obj")
                        .param("name", name)
                        .accept(MediaType.APPLICATION_JSON)
        )
                .andExpect(status().isOk())                           // 驗證狀態
                .andExpect(jsonPath("$.name", is(name)))    // 驗證json中返回的欄位是否含有name
                .andDo(print())
                .andReturn()
                .getResponse()
                .getContentAsString();

        log.info("response in junit test :\n" + responseString);
    }
}
  • 執行單元測試,如下圖,順利通過,並且紅框中所示的中文也沒有亂碼:

在這裡插入圖片描述

  • 至此,spring-cloud-square的三種型別,我們們全部編碼體驗了一遍,聰明的您當然不會只滿足於使用它們,接下來文章,我們們就去深入spring-cloud-square原始碼,研究其實現的細節,欣宸原創,必不會辜負您的期待!

你不孤單,欣宸原創一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 資料庫+中介軟體系列
  6. DevOps系列

歡迎關注公眾號:程式設計師欣宸

微信搜尋「程式設計師欣宸」,我是欣宸,期待與您一同暢遊Java世界...
https://github.com/zq2599/blog_demos

相關文章