Share
Embed
決済サービスのSpring Bootのバージョンを2系に上げた話
JSUG勉強会 2020その2 Spring Boot 1.xから2.xへの移行
Ryosuke Uchitate
February 12, 2020
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
57 slides
-
ࣗݾհ ໊લ ཱ ྑհʢ͏ͪͨͯ Γΐ͏͚͢ʣ ॴଐ ίΠχʔגࣜձࣾ Fashion Charity Project
Triangle Sauce ࠷ۙͷࣄ Reactॻ͍ͨΓɺՃໍళ͞Μʹخ͍͠ػೳ࣮ͨ͠Γ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
CoineyͷγεςϜߏ • admin-api • ࣾઐ༻αʔϏε͕ݺͿAPI • web-api • Ճໍళ͕ར༻͢ΔWebཧαʔ Ϗε͕ݺͿAPI
• mobile-api • Ճໍళ͕ར༻͢ΔϞόΠϧΞϓ Ϧ͕ݺͿAPI • partner-api • ύʔτφʔاۀཧʹؔ͢Δ API • antisocial-force- checker • ࣾνΣοΫʹؔ͢ΔAPI • emoney-api • ిࢠϚωʔʹؔ͢ΔAPI ઃఆϑΝΠϧCloud ConfigͰཧ -
ϨϙδτϦͱόʔδϣϯ Spring Boot Gradle આ໌ coiney-api 1.5.18 3.5.1 ϝΠϯͷAPI ʢadmin-api,
web-api, mobile- apiʣ coiney-api- config-server 1.5.9 2.9 Spring Cloud Config Server coiney-emoney 1.5.16 3.5.1 ిࢠϚωʔܾࡁʹؔ͢ΔAPI antisocial- force-checker 1.4.1 3.3 ࣾνΣοΫʹؔ͢ΔAPI Spring Boot 2.0.xGradle 4+ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
Gradle 4.xͷ࠷৽ʹ͢Δ // build.gradle wrapper { gradleVersion = "4.10.2" distributionType
= Wrapper.DistributionType.ALL } $ ./gradlew wrapper // Gradle 5.0ͰඇޓʹͳΔػೳΛར༻͍ͯ͠Δͱϝοηʔδ͕ग़ྗ͞ΕΔ // —-warning-mode allΦϓγϣϯΛ͏ͱৄࡉΛ֬ೝͰ͖Δ $ ./gradlew build —-warning-mode all -
build.gradleͷॻ͖͑ dependencies { - compile "com.fasterxml.jackson.dataformat:jackson-dataformat-csv" + implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-csv" }
Gradle4.7͔ΒcompileͳͲ͕ඇਪʹ 4.6·Ͱ 4.7Ҏ߱ compile implementation runtime runtimeOnly testCompile testImplementation testRuntime testRuntimeOnly -
implementationʹॻ͖͑Δͱ // fooϞδϡʔϧ dependencies { implementation "com.fasterxml.jackson.dataformat:jackson-dataformat-csv" } // barϞδϡʔϧ
dependencies { implementation project (":foo") } barϞδϡʔϧʹjackson-dataformat-csv͕͠ͳ͍ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
औಘ͢Δִ࣌ؒؒΛઃఆՄೳʹͳͬͨ // લͷϦΫΤετ͔Β60ඵޙҎ߱ͷϦΫΤετͰऔಘ // σϑΥϧτ0ඵɻʢϦΫΤετͷʹऔಘʣ spring.cloud.config.server.git.refresh-rate=60 Git Refresh Rate You
can control how often the config server will fetch updated configuration data from your Git backend by using spring.cloud.config.server.git.refreshRate. The value of this property is specified in seconds. By default the value is 0, meaning the config server will fetch updated configuration from the Git repo every time it is requested. [ެࣜυΩϡϝϯτΑΓൈਮ] Spring Cloud Config Server 2.0.2͔Β -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
@NotBlank, @NotEmpty, @Email͕ඇਪ • 6.0.0.Final͔Βඇਪʹͳͬͨ • javax-validation͕ఏڙ͢ΔΞϊςʔγϣϯΛ͏ - import org.hibernate.validator.constraints.NotBlank;
+ import javax.validation.constraints.NotBlank; -
Spring Boot Starter Json • JacksonͷͨΊͷAuto-configuration͕ఏڙ͞ΕΔ • ࣍ͷϥΠϒϥϦؚ͕·Ε͍ͯΔ • jackson-databind
• jackson-datatype-jdk8 • jackson-datatype-jsr310 • jackson-module-parameter-names -
WebMvcConfigurerAdapter.class͕ඇਪ • Spring 5͔ΒJava8+ʹͳΓɺdefaultϝιου͕͑ΔΑ͏ʹͳͬͨ ͨΊ • WebMvcConfigurer.javaΛ࣮͢Δ public class WebMvcConfig
extends WebMvcConfigurerAdapter {} public class WebMvcConfig implements WebMvcConfigurer {} -
ύοέʔδมߋ - import org.springframework.boot.autoconfigure.web.ErrorAttributes; + import org.springframework.boot.web.servlet.error.ErrorAttributes; - import org.springframework.boot.autoconfigure.web.ErrorController;
+ import org.springframework.boot.web.servlet.error.ErrorController; Spring Boot 2ʢSpring 5ʣ͔ΒServletͱReactiveʹ͔ΕͨͨΊ -
HtmlUtils.htmlEscape()ͷ࣮มߋ // 4.3.x public static String htmlEscape(String input, String encoding)
{ Assert.notNull(encoding, "Encoding is required"); if (input == null) { return null; } // 5.0.x public static String htmlEscape(String input, String encoding) { Assert.notNull(input, "Input is required"); Assert.notNull(encoding, "Encoding is required"); input͕nullͷͱ͖ExceptionΛ͛ΔΑ͏ʹͳͬͨ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
DataSourceΧελϚΠζ࣌ͷҙ • HikariCPΛར༻ͯ͠DataSourceΛΧελϚΠζ͢ΔࡍɺϓϩύςΟ ΛurlͰͳ͘jdbc-urlʹ͢Δ // application.properties app.datasource.jdbc-url=jdbc:mysql://localhost/test app.datasource.username=dbuser app.datasource.password=dbpass app.datasource.maximum-pool-size=30
@Bean @ConfigurationProperties(prefix = "app.datasource") public HikariDataSource dataSource() { return DataSourceBuilder.create().type(HikariDataSource.class).build(); } -
ίϯετϥΫλ͕ඇਪ - new PageRequest(1, 10); + PageRequest.of(1, 10); - new
Sort(orders); + Sort.by(orders); ίϯετϥΫλ͕ඇਪʹͳͬͨɻ2.2.0͔ΒpublicίϯετϥΫλ͡Όͳ͘ͳΔ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
JpaRepository.javaͷ࣮มߋ // ϝιουͷΓʹOptional͕͑ΔΑ͏ʹͳͬͨ - User fidyByName(String name); + Optional<User> findByName(String
name); // ϝιου໊ͷมߋʢҰ෦ʣ - <S extends T> List<S> save(Iterable<S> entities); + <S extends T> List<S> saveAll(Iterable<S> entities); -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
FlywayΛมߋͳ͠Ͱ3 -> 5 2019-09-11 03:43:15.152 ERROR --- Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.api.FlywayException: Validate failed: Migration checksum mismatch for migration 1 -> Applied to database : 1750005324 -> Resolved locally : -558371367 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.jav a:1583) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java: 545) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java: 482) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java: 230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:296) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java: 851) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:371) at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) at com.b1a9idps.sample.view.Application.main(Application.java:19) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ϚΠάϨʔγϣϯࣦഊ -
Flyway 3 -> 5ͷओͳมߋ Important note for users upgrading from
Flyway 3.x: This release no longer supports a schema history table upgrade from Flyway 3.x. You must upgrade to Flyway 4.2.0 first before upgrading to Flyway 5.0.0. 3 -> 4 νΣοΫαϜͷܭࢉํ๏ͷมߋɻνΣοΫαϜܭࢉ࣌ʹɺվߦίʔυΛແࢹ͢ΔΑ͏ʹͳͬͨɻ 4 -> 5 FlywayͷϝλσʔλΛཧ͢Δςʔϒϧ໊͕ schema_version͔Βflyway_schema_historyʹͳͬͨɻ -
όʔδϣϯΞοϓखॱ 1. Flyway 4ܥʹ্͛Δ 2. ΞϓϦέʔγϣϯΛىಈʢϚΠάϨʔγϣϯ࣮ߦʣ 3. Flyway 5ܥʹ্͛ΔʢBoot 2.0.9.RELEASEʹʣ
4. ΞϓϦέʔγϣϯΛىಈʢϚΠάϨʔγϣϯ࣮ߦʣ -
Flyway4ܥʹ্͛ͯىಈ dependencies { compile "org.flywaydb:flyway-core:4.2.0" } $ gradle bootRun =====
2019-09-11 03:43:14.467 INFO --- Upgrading metadata table `sample`.`schema_version` to the Flyway 4.0 format ... 2019-09-11 03:43:15.329 INFO --- Repairing metadata for version 1 (Description: CREATE TABLE SAMPLE DATA, Checksum: -558371367) ... 2019-09-11 03:43:15.349 INFO --- Repairing metadata for version 2 (Description: MODIFY SAMPLE DATA LENGTH, Checksum: -1031537806) ... 2019-09-11 03:43:15.370 INFO --- Repairing metadata for version 3 (Description: SAMPLE DATA ADD INDEX, Checksum: 194652164) ... 2019-09-11 03:43:15.412 INFO --- Metadata table schema_version successfully upgraded to the Flyway 4.0 format. ॳճىಈ࣌ʹطଘͷschema_versionςʔϒϧΛΞοϓάϨʔυͯ͘͠ΕΔ -
ϝλςʔϒϧ໊Λมߋ͠ͳ͔ͬͨ߹ 2019-09-25 23:05:33.882 WARN --- Could not find schema history
table `sample`.`flyway_schema_history`, but found `sample`.`schema_version` instead. You are seeing this message because Flyway changed its default for flyway.table in version 5.0.0 to flyway_schema_history and you are still relying on the old default (schema_version). Set flyway.table=schema_version in your configuration to fix this. This fallback mechanism will be removed in Flyway 6.0.0. 6ܥ͔ΒϑΥʔϧόοΫػೳ͕ͳ͘ͳΔ͔Βςʔϒϧ໊มߋ͠ͳΑͱҙ͞ΕΔ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
ϥΠϒϥϦͷมߋ JUnit JUnit 5͕͑ΔΑ͏ʹͳ͕ͬͨɺSpring Boot Starter TestJUnit 4ɻ Spring Boot
2.2.x͔ΒJUnit 5͕σϑΥϧτʹͳͬͨɻ Mockito 1ܥ͔Β2ܥʹͳͬͨ AssertJ 2ܥ͔Β3ܥʹͳͬͨ -
ύοέʔδมߋ - import org.mockito.runners.MockitoJUnitRunner; + import org.mockito.junit.MockitoJUnitRunner; - import org.mockito.Matchers.*;
+ import org.mockito.ArgumentMatchers.*; -
ෆཁͳελϒͷআ @ExtendWith(MockitoExtension.class) class StubTest { @Mock Hoge hoge; @Test void
test() { when(hoge.getName()).thenReturn("uchitate"); } class Hoge { String getName() { return "";} } } Unnecessary stubbings detected. Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary (click to navigate to relevant line of code): 1. -> at com.coiney.StubTest.test(StubTest.java:18) Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class. org.mockito.exceptions.misusing.UnnecessaryStubbingException: Unnecessary stubbings detected. Clean & maintainable test code requires zero unnecessary code. Following stubbings are unnecessary (click to navigate to relevant line of code): 1. -> at com.coiney.StubTest.test(StubTest.java:18) Please remove unnecessary stubbings or use 'lenient' strictness. More info: javadoc for UnnecessaryStubbingException class. ग़ྗ͞Εͨߦ൪߸ͷίʔυΛআ͢Δ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
EnvironmentΠϯλʔϑΣʔε͔ΒͷόΠϯυ͕Մೳʹ // ΠϯελϯεͷόΠϯυ JsugPropsBinder binder = new JsugPropsBinder(); Binder.get(environment) .bind("jsug.props-instance",
Bindable.ofInstance(binder)) .get(); // ҙͷΫϥεͷόΠϯυ Binder.get(environment) .bind("jsug.props-simple", Bindable.of(JsugPropsBinder.class)) .get(); // List<JsugPropsBinder>ͷόΠϯυ Binder.get(environment) .bind("jsug.props", Bindable.listOf(JsugPropsBinder.class)) .get(); public class JsugPropsBinder { private List<String> speakerNames; private String theme; @DateTimeFormat(iso = DATE) private LocalDate date; } -
ProfileͰόΠϯυ͢ΔΛมߋ͢Δ jsug: props: list: - name: uchitate theme: SpringBoot2 ---
spring: profiles: dev jsug: props: list: - name: uchitate-dev theme: SpringBoot2-dev @ConfigurationProperties("jsug.props") @ConstructorBinding public class JsugProps { private final List<Jsug> list; public static class Jsug { private String name; private String theme; } } -
ProfileͰόΠϯυ͢ΔΛมߋ͢Δ // profile=defaultͷͱ͖ { "list": [ { "name": "uchitate", "theme":
"SpringBoot2" } ] } // profile=devͷͱ͖ { "list": [ { "name": "uchitate-dev", "theme": "SpringBoot2-dev" } ] } Ճ͡Όͳ্ͯ͘ॻ͖͞ΕΔ -
ΞδΣϯμ • લஔ͖ • όʔδϣϯΞοϓ࡞ۀɹ • Gradle • Spring Cloud
Config Server • Spring Web • Spring Data • Spring Data JPA • Flyway • Testing • Configuration Property Binding • ·ͱΊ -
·ͱΊ • ެࣜυΩϡϝϯτΛಡΉ͜ͱ͕ॏཁ • ຯͳ࡞ۀ͕ଟ͍ • ͔ͳΓษڧʹͳΔ • ϓϧϦΫΛখग़͠ʹ͢Δͷ͕Αͦ͞͏ •
GradleͱLombokͷόʔδϣϯ૬ੑͭΒ͍