preface

When building the microservice development environment with Idea, we mostly use nested modules to organize projects. If the configuration is not appropriate, all kinds of puzzling problems may occur. Make a note here to avoid repeated trips in the future.

Question 1

  • SpringBoot started failed: failed to introspect a Class [] org. Springframework. Boot. Autoconfigure. XXX
  • Analysis: SpringCloud depends on SpringBoot, and different SpringCloud versions depend on specific SpringBoot versions. Because we use nested modules to organize, it is easy to cause version conflicts between parent and child versions.

For example, if the parent POM file introduces 2.2.5 SpringBoot

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> < version > 2.2.5. RELEASE < / version > < relativePath / > < / parent >Copy the code

We know that the parent project contains multiple dependencies, and this can happen if one of the dependencies is referenced separately in a child module and the version is different from the parent version.

  • Solution: through the exception information above we can see is org springframework. Boot. Autoconfigure related problems. At this point you can remove the dependency, using the autoconfigure dependency in the parent POM by default

Question 2

  • Failed to execute goal org.springframework.boot:spring-boot-maven-plugin
  • Once the spring-boot-Maven-plugin is introduced, the main method of the project will be scanned when the package is packaged. This means that to introduce this configuration, you must create a spring-boot startup class under the project SRC /main/ Java /.
  • Solution: If there is no code under the parent Module, remove the plugin configuration from the POM
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId>  </plugin> </plugins> </build>Copy the code

Question 3

  • Failed to execute goal org. Springframework. The boot: spring – the boot – maven – plugin: 2.2.5. RELEASE
  • Dependencies in dependencyManager is a declaration that dependencies are not actually downloaded locally. If a child module wants to reference a dependency, it needs to add it in its own dependencies without adding version information, and the version inherits from the parent module. Add the version information only when the version of the submodule must be different from that of the parent module
    <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.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>
        </dependencies>
    </dependencyManagement>
Copy the code

By default, the spring-boot-starter- XXX version inherits from the spring-boot-starter-parent version. If you do not want to inherit from the parent version, you can set the scope to import. This way you can use your own version, and submodules will inherit your custom version.

Note: < scope> import < /scope> can only be used in dependencyManagement

  • Solution: add the required dependencies in the child model’s POM. If the custom version is not necessary, you can not write the version, so that the version in the parent POM is inherited.

Question 4

  • In UTautowiredThe marked object returns null
  • Analysis of theautowiredYou need to rely on the Spring Context to run, if only normal test classes do not load the Spring Context
  • Solution: Add the following annotations to the test class
  1. RunWith(springrunner.class) : Let the Spring container start the class
  2. @SpringBoottest: Register this test class as a bean
  • Example:
@RunWith(SpringRunner.class) @SpringBootTest public class TestPaymentDao { @Autowired private PaymentDao paymentDao; @Test public void testAdd(){ int result=paymentDao.add(new Payment(1L,"123")); Assert.assertTrue(result>0); }}Copy the code

Question 5

  • org.yaml.snakeyaml.scanner.ScannerException: while scanning a simple key
  • Analysis: If yML is used as the configuration file, the colon must be followed by a space
  • Solution: Add a space after the colon
  • Example:
spring: application: name: cloud-payment-service datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/blog? characterEncoding=utf-8&serverTimezone=UTC&allowPublicKeyRetrieval=true&useSSL=false username: root password: 123456Copy the code

When using YML, ensure that the indentation of the configurations at the same level is consistent; otherwise, exceptions may occur

Question 6

  • SqlSession XXX was not registered for synchronization because synchronization is not active
  • Analysis: If you can rule out configuration reasons, try to add@Transactionalannotations
  • Solution: Add transaction annotations to methods
  • Example:
  @Update("update payment set sequence_num=#{SequenceNum} where id=#{Id}")
  @Transactional
  int update(Payment payment);
Copy the code

Question 7

  • no converter found for return value of type
  • Analysis: When using RestTemplate to convert JSON to a specified object, you need to add getters for the properties of the specified object. Because I was using Lombok and forgot to add it@getter, so the above error is reported
  • Solution: Add@Getteror@Data
  • Example:
@Data @NoArgsConstructor @AllArgsConstructor public class ApiResult<T> { private int status; private String message; private T data; public ApiResult(int status,String message){ this(status,message,null); }}Copy the code

Question 8

  • When restapis call each other, the RestTemplate is used to pass parameters. In this case, the called API may receive empty parameters
  • For example, the add method of the order service calls the add method of the payment service and needs to pass a Payment object to the Add method of the payment service. In this case, the payment service may receive an empty payment

Example of adding @requestBody to the payment service add method argument:

  @PostMapping(value = "add")
  public ApiResult add(@RequestBody Payment payment){
    if(payment==null || payment.getId()==0)
      return new ApiResult(405,"invalid parameters");
    if(paymentService.add(payment)>0)
      return new ApiResult(200,"succeeded");
    return new ApiResult<>(500,"failed");
  }
Copy the code

Question 9

  • Connect to localhost:8761 time out
  • Analysis: When using Eureka to build microservices, you need to passdefaultZoneTo specify the address of the registry. The above exception occurs if this is not configured. Looking at my configuration, my application.yml contains defaultZone, but it’s top-leveleurekaWritten on theeurake, this also indirectly leads to the failure to findeureka.client.service-url.defaultZone
  • Address:euraketoeureka. What a stupid mistake