微服务
微服务架构师当前软件开发领域的技术热点。它在各种博客、社交媒体和会议演讲上的出镜率非常之高。大家以前可能或多或少听说过些许。然而微服务似乎又是非常虚幻的————我们找不到微服务的完整定义,以至于很多人认为这是一个概念的炒作。
一般讲到微服务架构,都会提到单体应用。首先来说说单体应用:
一个归档包(比如 war 包)包含的所有功能的应用程序。在项目中我们通常将需求分为三个主要部分:数据库、服务端处理、前端展现。在业务发展初期,由于所有的业务逻辑在一个应用中,开发、测试、部署都还比较容易且方便。这种通常称为单体应用。这也就是现在称之为单体应用架构的方法论
尽管该应用已经进行了模块化,但由于 UI 和若干业务模块最终都被打包在同一个 War 包中,改 war 包包含了整个系统所有的业务功能,这样的应用系统成为单体应用。 相信很多项目都是从单体应用开始的。单体应用比较容易部署、测试,在项目初期,单体应用可以很好地稳定运行。然而,一般项目都会随着需求而不断的变化以及增加,越来越多的人加入到项目的开发团队,代码库也在飞速地膨胀。慢慢地,单体应用变得越来越臃肿,可维护性、灵活性逐渐降低,维护成本越来越高。
微服务架构风格是一种将一个单体应用程序开发为一组小行服务的方法,每个服务运行在自己的进程中,服务间通信采用轻量级通信机制(通常用 HTTP 资源 API)。这些服务围绕业务能力构建摒弃人可通过全自动部署机制独立部署。这些服务共用一个最小型的集中式的管理,服务可用不同的语言开发,使用不同的数据存储技术。——引自 Martin Fowler 的博客
- 每个微服务可独立运行在自己的进程里,做到了进程隔离。
- 一系列独立运行的微服务共同构建起整个系统。
- 每个服务为独立的业务开发,一个微服务只关注某个特定的功能,例如订单管理、用户管理等。
- 微服务之间通过一些轻量级的通信机制进行通信,例如通过 RESTful API 进行调用。
- 可以使用不同的语言与数据库存储技术。
- 全自动的部署机制。
开始进入真正的主角介绍,Spring Cloud
开发框架。
Spring Cloud 是一个用于构建分布式系统的通用模块的工具集
。
- 约定优于配置
- 适用于各种环境
- 隐藏了组件的复杂性
- 开箱即用
- 轻量级的组件
- 丰富的组件,比如:服务发现、断路器、微服务网关等,后面实验将会一一介绍。
- 灵活,Spring Cloud 的组成是解耦的,开发人员可按需灵活挑选组合。
更多关于 Spring Cloud 的信息可以去官网查看:spring.io。
从这节开始我们真正开始实战微服务架构。首先 Spring Cloud 并不是面向零基础的开发人员,假设你现在已经熟悉 Java 基础和 Spring Boot 基础。
之前讲到过,Spring Cloud 是用于构建分布式系统的,一般分布式系统都会涉及到服务提供者和服务消费者。 本实验就开始试着构建一个用户服务提供者和一个电影服务消费者。
以电影售票系统为例。如图,用户向电影微服务发起了一个购票的请求。在进行购票的业务操作前,电影微服务需要调用用户微服务的接口,查询当前用户的余额是多少、是不是符合购票标准等。在这种场景下,用户微服务就是一个服务提供者,电影微服务则是一个服务消费者。
知识点
- 分布式系统的大致组成
- 服务提供者和消费者的关系
- 通过 Maven 引用 Spring Cloud 依赖
- 通过 Spring Cloud 编写微服务
工具及软件
- JDK:建议使用 JDK 1.8
- Spring Boot:实验使用 Spring Boot 2.0.7.RELEASE 版本。
- Spring Cloud:实验使用 Spring Cloud Finchley.SR2 版本。
- Maven:实验使用 Maven 3.6.0 构建项目。
创建微服务项目
C:\Users\l>mvn -version
Apache Maven 3.6.3 (cecedd3430 02696d0abb50b32b541b8a6ba2883f)
Maven home: E:\kaifa\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin\..
Java version: 1.8.0_111, vendor: Oracle Corporation, runtime: D:\kaifa\java\jdk\jre
Default locale: zh_CN, platform encoding: GBK
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"


maven pom 仓库
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <?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.lzj1234</groupId> <artifactId>microservice-provider-user</artifactId> <version> 1.0 -SNAPSHOT</version> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version> 2.4 . 2 </version> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version> 2.4 . 2 </version> </dependency> <!-- 引入H2数据库,一种内嵌的数据库,语法类似MySQL --> <!-- https: //mvnrepository.com/artifact/com.h2database/h2 --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version> 1.4 . 200 </version> <!-- <scope>test</scope>--> </dependency> <!-- https: //mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version> 1.18 . 18 </version> <scope>provided</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 2.4 . 2 </version> <scope>test</scope> </dependency> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin --> <dependency> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-clean-plugin</artifactId> <version> 2.5 </version> </dependency> </dependencies> <!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 --> <dependencyManagement> <dependencies> <!-- https: //mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR10</version> <type>pom</type> <scope>runtime</scope> </dependency> </dependencies> </dependencyManagement> <!-- <build>--> <!-- <plugins>--> <!-- <!– 添加spring-boot的maven插件,不能少,打jar包时得用 –>--> <!-- <plugin>--> <!-- <groupId>org.springframework.boot</groupId>--> <!-- <artifactId>spring-boot-maven-plugin</artifactId>--> <!-- <version> 2.4 . 2 </version>--> <!-- </plugin>--> <!-- </plugins>--> <!-- </build>--> <build> <plugins> <!-- 添加spring-boot的maven插件,不能少,打jar包时得用 --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version> 2.4 . 2 </version> </plugin> <plugin> <!-- https: //mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version> 2.5 . 1 </version> <configuration> <source> 1.8 </source> <target> 1.8 </target> </configuration> </plugin> </plugins> </build> </project> |
maven setttings配置
1 2 3 4 5 6 7 8 9 10 11 12 13 | <mirror> <id>aliyunmaven</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>https: //maven.aliyun.com/repository/public</url> </mirror> <mirror> <id>aliyunmaven2</id> <mirrorOf>*</mirrorOf> <name>阿里云公共仓库</name> <url>http: //repository.apache.org/content/groups/snapshots/?spm=a2c40.maven_devops2020_goldlog_.0.0.43643054AxL62v/</url> </mirror> |
不添加这个mirror镜像:
1 | http: //repository.apache.org/content/groups/snapshots/?spm=a2c40.maven_devops2020_goldlog_.0.0.43643054AxL62v/<br>就会报这个错 |
Could not transfer metadata org.codehaus.mojo/maven-metadata.xml from/to rep
运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | C:\Users\l\javademo\microservice-provider-user>mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] ---------------< com.lzj1234:microservice-provider-user >--------------- [INFO] Building microservice-provider-user 1.0 -SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) > test-compile @ microservice-provider-user >>> [INFO] [INFO] --- maven-resources-plugin: 2.6 :resources ( default -resources) @ microservice-provider-user --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin: 2.5 . 1 :compile ( default -compile) @ microservice-provider-user --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin: 2.6 :testResources ( default -testResources) @ microservice-provider-user --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin: 2.5 . 1 :testCompile ( default -testCompile) @ microservice-provider-user --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) < test-compile @ microservice-provider-user <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) @ microservice-provider-user --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2. 4.2 ) 2021 - 02 - 18 23 : 56 : 10.859 INFO 18084 --- [ main] com.src.App : Starting App using Java 1.8 .0_111 on DESKTOP-SRVQTV7 with PID 18084 (C:\Users\l\j avademo\microservice-provider-user\target\classes started by l in C:\Users\l\javademo\microservice-provider-user) 2021 - 02 - 18 23 : 56 : 10.863 INFO 18084 --- [ main] com.src.App : No active profile set, falling back to default profiles: default 2021 - 02 - 18 23 : 56 : 11.446 INFO 18084 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2021 - 02 - 18 23 : 56 : 11.496 INFO 18084 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 42 ms. Found 1 JPA repository interfa ces. 2021 - 02 - 18 23 : 56 : 12.891 INFO 18084 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8000 (http) 2021 - 02 - 18 23 : 56 : 12.902 INFO 18084 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021 - 02 - 18 23 : 56 : 12.902 INFO 18084 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/ 9.0 . 41 ] 2021 - 02 - 18 23 : 56 : 13.001 INFO 18084 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021 - 02 - 18 23 : 56 : 13.001 INFO 18084 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2091 ms 2021 - 02 - 18 23 : 56 : 13.161 INFO 18084 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Starting... 2021 - 02 - 18 23 : 56 : 13.266 INFO 18084 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Start completed. 2021 - 02 - 18 23 : 56 : 13.312 INFO 18084 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default ] 2021 - 02 - 18 23 : 56 : 13.360 INFO 18084 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4 . 27 .Final 2021 - 02 - 18 23 : 56 : 13.483 INFO 18084 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations { 5.1 . 2 .Final} 2021 - 02 - 18 23 : 56 : 13.600 INFO 18084 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect Hibernate: drop table if exists user CASCADE Hibernate: drop sequence if exists hibernate_sequence Hibernate: create sequence hibernate_sequence start with 1 increment by 1 Hibernate: create table user (id bigint not null , age integer, balance decimal( 19 , 2 ), name varchar( 255 ), username varchar( 255 ), primary key (id)) 2021 - 02 - 18 23 : 56 : 14.216 INFO 18084 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jt a.platform.internal.NoJtaPlatform] 2021 - 02 - 18 23 : 56 : 14.225 INFO 18084 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2021 - 02 - 18 23 : 56 : 14.508 WARN 18084 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2021 - 02 - 18 23 : 56 : 14.614 INFO 18084 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021 - 02 - 18 23 : 56 : 14.793 INFO 18084 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8000 (http) with context path '' 2021 - 02 - 18 23 : 56 : 14.801 INFO 18084 --- [ main] com.src.App : Started App in 4.4 seconds (JVM running for 4.811 ) Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 2021 - 02 - 18 23 : 56 : 14.853 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [BIGINT] - [ 1 ] Hibernate: call next value for hibernate_sequence Hibernate: insert into user (age, balance, name, username, id) values (?, ?, ?, ?, ?) 2021 - 02 - 18 23 : 56 : 14.885 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [INTEGER] - [ 20 ] 2021 - 02 - 18 23 : 56 : 14.888 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 2 ] as [NUMERIC] - [ 100 ] 2021 - 02 - 18 23 : 56 : 14.889 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 3 ] as [VARCHAR] - [瀵姳绗乚 2021 - 02 - 18 23 : 56 : 14.890 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 4 ] as [VARCHAR] - [account1] 2021 - 02 - 18 23 : 56 : 14.891 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 5 ] as [BIGINT] - [ 1 ] Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 2021 - 02 - 18 23 : 56 : 14.899 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [BIGINT] - [ 2 ] Hibernate: call next value for hibernate_sequence Hibernate: insert into user (age, balance, name, username, id) values (?, ?, ?, ?, ?) 2021 - 02 - 18 23 : 56 : 14.901 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [INTEGER] - [ 28 ] 2021 - 02 - 18 23 : 56 : 14.902 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 2 ] as [NUMERIC] - [ 180 ] 2021 - 02 - 18 23 : 56 : 14.902 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 3 ] as [VARCHAR] - [閺夊骸娲揮 2021 - 02 - 18 23 : 56 : 14.903 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 4 ] as [VARCHAR] - [account2] 2021 - 02 - 18 23 : 56 : 14.905 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 5 ] as [BIGINT] - [ 2 ] Hibernate: select user0_.id as id1_0_0_, user0_.age as age2_0_0_, user0_.balance as balance3_0_0_, user0_.name as name4_0_0_, user0_.username as username5_0_0_ from user user0_ where user0_.id=? 2021 - 02 - 18 23 : 56 : 14.907 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [BIGINT] - [ 3 ] Hibernate: call next value for hibernate_sequence Hibernate: insert into user (age, balance, name, username, id) values (?, ?, ?, ?, ?) 2021 - 02 - 18 23 : 56 : 14.909 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 1 ] as [INTEGER] - [ 32 ] 2021 - 02 - 18 23 : 56 : 14.909 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 2 ] as [NUMERIC] - [ 280 ] 2021 - 02 - 18 23 : 56 : 14.910 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 3 ] as [VARCHAR] - [閻滃绨瞉 2021 - 02 - 18 23 : 56 : 14.910 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 4 ] as [VARCHAR] - [account3] 2021 - 02 - 18 23 : 56 : 14.910 TRACE 18084 --- [ main] o.h.type.descriptor.sql.BasicBinder : binding parameter [ 5 ] as [BIGINT] - [ 3 ] 2021 - 02 - 18 23 : 56 : 44.092 INFO 18084 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2021 - 02 - 18 23 : 56 : 44.093 INFO 18084 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2021 - 02 - 18 23 : 56 : 44.094 INFO 18084 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut- do wn' Hibernate: drop table if exists user CASCADE Hibernate: drop sequence if exists hibernate_sequence 2021 - 02 - 18 23 : 56 : 44.099 INFO 18084 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Shutdown initiated... 2021 - 02 - 18 23 : 56 : 44.101 INFO 18084 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Shutdown completed. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 35.701 s [INFO] Finished at: 2021 - 02 -18T23: 56 : 44 + 08 : 00 [INFO] ------------------------------------------------------------------------ 终止批处理操作吗(Y/N)? y |
测试
1 2 | C:\Users\l\javademo\microservice-provider-user>curl http: //localhost:8000/users/1 { "id" : 1 , "username" : "account1" , "name" : "瀵姳绗?," age ":20," balance": 100.00 } |
消费者
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>com.shiyanlou</groupId>
<artifactId>microservice-consumer-movie</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- <properties>-->
<!-- <maven.compiler.source>8</maven.compiler.source>-->
<!-- <maven.compiler.target>8</maven.compiler.target>-->
<!-- </properties>-->
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.4.2</version>
</dependency>
<!-- 引入H2数据库,一种内嵌的数据库,语法类似MySQL -->
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!-- <scope>test</scope>-->
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.18</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.4.2</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-clean-plugin -->
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<!-- 引入spring cloud的依赖,不能少,主要用来管理Spring Cloud生态各组件的版本 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-compiler-plugin -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
user.java
1 | package com.shiyanlou;<br><br> import lombok.AllArgsConstructor;<br> import lombok.Data;<br> import lombok.NoArgsConstructor;<br><br> import java.math.BigDecimal;<br><br> @Data <br> @AllArgsConstructor <br> @NoArgsConstructor <br> public class User {<br> private Long id;<br> private String username;<br> private String name;<br> private Integer age;<br> private BigDecimal balance;<br>}} |
app.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.shiyanlou; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication public class App { public static void main(String[] args) { SpringApplication.run(App. class , args); } @Bean public RestTemplate restTemplate(){ return new RestTemplate(); } } |
MovieController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | package com.shiyanlou; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RequestMapping ( "/movies" ) @RestController public class MovieController { @Autowired RestTemplate restTemplate; @GetMapping ( "/users/{id}" ) public User findById( @PathVariable Long id){ User user= this .restTemplate.getForObject( "http://localhost:8000/users/{id}" ,User. class ,id); return user; } } |
配置
1 2 3 4 5 6 7 8 9 10 11 12 | server: port: 8010 spring: application: name: microservice-consumer-movie logging: level: root: INFO # 配置日志级别,让hibernate打印出执行的SQL参数 org.hibernate: INFO org.hibernate.type.descriptor.sql.BasicBinder: TRACE org.hibernate.type.descriptor.sql.BasicExtractor: TRACE |
运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | C:\Users\l\javademo\microservice-consumer-movie>mvn spring-boot:run [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were encountered while building the effective model for com.shiyanlou:microservice-consumer-movie:jar: 1.0 -SNAPSHOT [WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework.boot:spring-boot-starter-web:jar -> duplicate declaration of version 2.4 . 2 @ line 50 , column 21 [WARNING] [WARNING] It is highly recommended to fix these problems because they threaten the stability of your build. [WARNING] [WARNING] For this reason, future Maven versions might no longer support building such malformed projects. [WARNING] [INFO] [INFO] -------------< com.shiyanlou:microservice-consumer-movie >-------------- [INFO] Building microservice-consumer-movie 1.0 -SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) > test-compile @ microservice-consumer-movie >>> [INFO] [INFO] --- maven-resources-plugin: 2.6 :resources ( default -resources) @ microservice-consumer-movie --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin: 3.1 :compile ( default -compile) @ microservice-consumer-movie --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin: 2.6 :testResources ( default -testResources) @ microservice-consumer-movie --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin: 3.1 :testCompile ( default -testCompile) @ microservice-consumer-movie --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) < test-compile @ microservice-consumer-movie <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) @ microservice-consumer-movie --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2. 4.2 ) 2021 - 02 - 19 11 : 29 : 55.620 INFO 5708 --- [ main] com.shiyanlou.App : Starting App using Java 1.8 .0_111 on DESKTOP-SRVQTV7 with PID 5708 (C:\Users\l\jav ademo\microservice-consumer-movie\target\classes started by l in C:\Users\l\javademo\microservice-consumer-movie) 2021 - 02 - 19 11 : 29 : 55.623 INFO 5708 --- [ main] com.shiyanlou.App : No active profile set, falling back to default profiles: default 2021 - 02 - 19 11 : 29 : 56.211 INFO 5708 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2021 - 02 - 19 11 : 29 : 56.227 INFO 5708 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 JPA repository interface s. 2021 - 02 - 19 11 : 29 : 57.594 INFO 5708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8010 (http) 2021 - 02 - 19 11 : 29 : 57.605 INFO 5708 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021 - 02 - 19 11 : 29 : 57.606 INFO 5708 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/ 9.0 . 41 ] 2021 - 02 - 19 11 : 29 : 57.686 INFO 5708 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021 - 02 - 19 11 : 29 : 57.686 INFO 5708 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2013 ms 2021 - 02 - 19 11 : 29 : 57.825 INFO 5708 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Starting... 2021 - 02 - 19 11 : 29 : 57.922 INFO 5708 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Start completed. 2021 - 02 - 19 11 : 29 : 57.964 INFO 5708 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default ] 2021 - 02 - 19 11 : 29 : 58.011 INFO 5708 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4 . 27 .Final 2021 - 02 - 19 11 : 29 : 58.134 INFO 5708 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations { 5.1 . 2 .Final} 2021 - 02 - 19 11 : 29 : 58.251 INFO 5708 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 2021 - 02 - 19 11 : 29 : 58.472 INFO 5708 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta .platform.internal.NoJtaPlatform] 2021 - 02 - 19 11 : 29 : 58.484 INFO 5708 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2021 - 02 - 19 11 : 29 : 58.712 WARN 5708 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2021 - 02 - 19 11 : 29 : 58.812 INFO 5708 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021 - 02 - 19 11 : 29 : 59.000 INFO 5708 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path '' 2021 - 02 - 19 11 : 29 : 59.010 INFO 5708 --- [ main] com.shiyanlou.App : Started App in 3.838 seconds (JVM running for 4.264 ) 2021 - 02 - 19 11 : 30 : 21.980 INFO 5708 --- [nio- 8010 -exec- 1 ] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2021 - 02 - 19 11 : 30 : 21.981 INFO 5708 --- [nio- 8010 -exec- 1 ] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2021 - 02 - 19 11 : 30 : 21.983 INFO 5708 --- [nio- 8010 -exec- 1 ] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms 2021 - 02 - 19 11 : 31 : 51.962 INFO 5708 --- [extShutdownHook] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor' 2021 - 02 - 19 11 : 31 : 51.962 INFO 5708 --- [extShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default' 2021 - 02 - 19 11 : 31 : 51.963 INFO 5708 --- [extShutdownHook] .SchemaDropperImpl$DelayedDropActionImpl : HHH000477: Starting delayed evictData of schema as part of SessionFactory shut-dow n' 2021 - 02 - 19 11 : 31 : 52.307 INFO 5708 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Shutdown initiated... 2021 - 02 - 19 11 : 31 : 52.313 INFO 5708 --- [extShutdownHook] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Shutdown completed. [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02 : 01 min [INFO] Finished at: 2021 - 02 -19T11: 31 : 52 + 08 : 00 [INFO] ------------------------------------------------------------------------ 终止批处理操作吗(Y/N)? ^Cy C:\Users\l\javademo\microservice-consumer-movie>mvn spring-boot:run [INFO] Scanning for projects... [INFO] [INFO] -------------< com.shiyanlou:microservice-consumer-movie >-------------- [INFO] Building microservice-consumer-movie 1.0 -SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] >>> spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) > test-compile @ microservice-consumer-movie >>> [INFO] [INFO] --- maven-resources-plugin: 2.6 :resources ( default -resources) @ microservice-consumer-movie --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 1 resource [INFO] [INFO] --- maven-compiler-plugin: 2.5 . 1 :compile ( default -compile) @ microservice-consumer-movie --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- maven-resources-plugin: 2.6 :testResources ( default -testResources) @ microservice-consumer-movie --- [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin: 2.5 . 1 :testCompile ( default -testCompile) @ microservice-consumer-movie --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] <<< spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) < test-compile @ microservice-consumer-movie <<< [INFO] [INFO] [INFO] --- spring-boot-maven-plugin: 2.4 . 2 :run ( default -cli) @ microservice-consumer-movie --- [INFO] Attaching agents: [] . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | ' _| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2. 4.2 ) 2021 - 02 - 19 11 : 38 : 14.507 INFO 13400 --- [ main] com.shiyanlou.App : Starting App using Java 1.8 .0_111 on DESKTOP-SRVQTV7 with PID 13400 (C:\Users\l\j avademo\microservice-consumer-movie\target\classes started by l in C:\Users\l\javademo\microservice-consumer-movie) 2021 - 02 - 19 11 : 38 : 14.510 INFO 13400 --- [ main] com.shiyanlou.App : No active profile set, falling back to default profiles: default 2021 - 02 - 19 11 : 38 : 15.114 INFO 13400 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode. 2021 - 02 - 19 11 : 38 : 15.128 INFO 13400 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 7 ms. Found 0 JPA repository interfac es. 2021 - 02 - 19 11 : 38 : 16.513 INFO 13400 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8010 (http) 2021 - 02 - 19 11 : 38 : 16.526 INFO 13400 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021 - 02 - 19 11 : 38 : 16.527 INFO 13400 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/ 9.0 . 41 ] 2021 - 02 - 19 11 : 38 : 16.621 INFO 13400 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021 - 02 - 19 11 : 38 : 16.622 INFO 13400 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2063 ms 2021 - 02 - 19 11 : 38 : 16.768 INFO 13400 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Starting... 2021 - 02 - 19 11 : 38 : 16.877 INFO 13400 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool- 1 - Start completed. 2021 - 02 - 19 11 : 38 : 16.918 INFO 13400 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default ] 2021 - 02 - 19 11 : 38 : 16.965 INFO 13400 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4 . 27 .Final 2021 - 02 - 19 11 : 38 : 17.085 INFO 13400 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations { 5.1 . 2 .Final} 2021 - 02 - 19 11 : 38 : 17.203 INFO 13400 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.H2Dialect 2021 - 02 - 19 11 : 38 : 17.422 INFO 13400 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jt a.platform.internal.NoJtaPlatform] 2021 - 02 - 19 11 : 38 : 17.433 INFO 13400 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default' 2021 - 02 - 19 11 : 38 : 17.505 WARN 13400 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default . Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning 2021 - 02 - 19 11 : 38 : 17.604 INFO 13400 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021 - 02 - 19 11 : 38 : 17.788 INFO 13400 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8010 (http) with context path '' 2021 - 02 - 19 11 : 38 : 17.796 INFO 13400 --- [ main] com.shiyanlou.App : Started App in 3.746 seconds (JVM running for 4.252 ) 2021 - 02 - 19 11 : 38 : 22.197 INFO 13400 --- [nio- 8010 -exec- 1 ] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet' 2021 - 02 - 19 11 : 38 : 22.198 INFO 13400 --- [nio- 8010 -exec- 1 ] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet' 2021 - 02 - 19 11 : 38 : 22.200 INFO 13400 --- [nio- 8010 -exec- 1 ] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms |
1 2 3 4 5 6 | \Users\l\javademo\microservice-consumer-movie>curl http: //localhost:8010/movies/users/1 { "id" : 1 , "username" : "account1" , "name" : "瀵姳绗?," age ":20," balance": 100.00 } C:\Users\l\javademo\microservice-consumer-movie>curl http: //localhost:8010/movies/users/1 { "id" : 1 , "username" : "account1" , "name" : "瀵姳绗?," age ":20," balance": 100.00 } C:\Users\l\javademo\microservice-consumer-movie>curl http: //localhost:8010/movies/users/2 { "id" : 2 , "username" : "account2" , "name" : "閺夊骸娲?," age ":28," balance": 180.00 } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 按钮权限的设计及实现