spring-cloud-square开发实战(三种类型全覆盖)
Posted 程序员欣宸
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring-cloud-square开发实战(三种类型全覆盖)相关的知识,希望对你有一定的参考价值。
欢迎访问我的GitHub
这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos
本篇概览
- 前文《五分钟搞懂spring-cloud-square》详细介绍了什么是spring-cloud-square,以及三种实现类型的详细概念,爱动手的您已迫不及待想编码体验spring-cloud-square了,本篇咱们就来畅快实战,体验这个spring官方带给我们的smart client
- 如标题所述,接下里咱们会将spring-cloud-square提供的三种client都编码体验,总的来说本篇由以下内容构成:
- 新建maven工程,名为spring-cloud-square-tutorials,这是本篇所有应用的父工程,库版本在此工程中统一管理;
- 创建子工程eureka,作为注册中心
- 创建子工程client,放一些公用的数据结构
- 创建子工程provider,身份是服务提供者,接下来的三个用到spring-cloud-square的子工程,都调用provider的服务
- 创建子工程consumer-okhttp,基于spring-cloud-square的okhttp能力做远程调用
- 创建子工程consumer-retrofit-okhttp,基于spring-cloud-square的retrofit + okhttp能力做远程调用
- 创建子工程consumer-retrofit-webflux,基于spring-cloud-square的retrofit + webflux能力做远程调用
- 上述几个服务的关系如下图:
如何验证
- 代码写完之后,如何验证功能是否符合预期呢?本篇采用单元测试的方式,consumer-okhttp、consumer-retrofit-okhttp、consumer-retrofit-webflux这三个子工程都有自己的单元测试代码,执行通过就意味着代码功能符合预期了
源码下载
- 本篇实战中的完整源码可在GitHub下载到,地址和链接信息如下表所示(https://github.com/zq2599/blog_demos):
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
- 这个git项目中有多个文件夹,本篇的源码在spring-cloud-square-tutorials文件夹下,如下图红框所示:
版本信息
- 本篇实战涉及到的主要版本情况如下:
- 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
- 父工程名为spring-cloud-square-tutorials,其pom.xml如下,除了依赖库的版本在此统一管理,还要注意的是两个仓库的引入(https://repo.spring.io/snapshot和https://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
- -新建名为provider的应用,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>
以上是关于spring-cloud-square开发实战(三种类型全覆盖)的主要内容,如果未能解决你的问题,请参考以下文章
spring-cloud-square源码速读(retrofit + okhttp篇)
spring-cloud-square源码速读(retrofit + okhttp篇)
spring-cloud-square源码速读(spring-cloud-square-okhttp篇)