物联网架构成长之路(17)-SpringCloud目前遇到的注意事项

时间:2023-03-09 09:47:31
物联网架构成长之路(17)-SpringCloud目前遇到的注意事项

1. STS插件最好是要安装的.

2. 对应的Decompiler插件也是要安装的.

3. 如果遇到maven工程因为找不到包问题的, 在确认pom.xml 文件没有问题的情况下, 右键项目-Maven-Update Project 然后点击OK,更新一下工程,
  还不行的 点击 Force Update of Snapshots/Releases
  还不行的 删除.m2 Maven下载目录下的本地仓库所有文件,重新更新下载一遍
  还不行的 在报错的Markers页签 右键删除报错信息
  然后重新运行Spring Cloud

4.Spring-cloud 项目加入eureka后, restcontroller 默认返回的是xml格式,需要自定义一个配置类

 @Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter { //解决spring-cloud 加载eureka后, restcontroller 默认返回xml格式
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.ignoreAcceptHeader(true).defaultContentType(MediaType.APPLICATION_JSON);
super.configureContentNegotiation(configurer);
}
}

5. 如果需要在项目一启动,就执行某些代码,可以增加如下的监听器代码

 @Component
public class OnStartListener implements ApplicationListener<ContextRefreshedEvent> { @Override
public void onApplicationEvent(ContextRefreshedEvent event) {
System.out.println(event.getApplicationContext().getParent());
System.out.println("on start: " + System.currentTimeMillis());
} }

6.启动事务时,要在启动类加入一些注解

 /**
* 启动类
* @author wunaozai
* MapperScan : 扫描的是mapper.xml中namespace指向的包位置
* EnableTransactionManagement : 加入事务
* 加入@Transaction 事务处理后, spring 会启用动态代理方式,
* 所以在EnableTransactionManagement 要增加proxyTargetClass
*/
@EnableEurekaClient
@SpringBootApplication
@EnableTransactionManagement(proxyTargetClass=true)
@MapperScan("com.wunaozai.xxx.center.mapper")
public class ServiceDeveloperCenterApplication { public static void main(String[] args) {
SpringApplication.run(ServiceDeveloperCenterApplication.class, args);
}
}

7.项目中各个文件夹(包名)都最好按照默认的约定的来,不要自己乱改.

8.一份简单的application.properties

 spring.application.name=service-developer-center

 #server
server.port=9200
server.session.timeout=360000
server.session.cookie.name=IOT #eureka
eureka.client.serviceUrl.defaultZone=http://172.16.23.203:9000/eureka/
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port} #logging
logging.path=log
logging.level.com.favorites=INFO
logging.level.org.springframework.web=INFO #datasource
spring.datasource.url=jdbc:postgresql://172.16.23.202:5432/service_developer_center
spring.datasource.username=postgres
spring.datasource.password=
spring.datasource.driverClassName=org.postgresql.Driver #mybatis
mybatis.type-aliases-package=com.wunaozai.xxx.center.model
mybatis.mapper-locations=classpath:com/wunaozai/xxx/center/mapper/*.xml #pagehelper
pagehelper.helperDialect=postgresql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.returnPageInfo=check #redis
spring.redis.database=0
spring.redis.host=172.16.20.229
spring.redis.port=6379
spring.redis.password=
spring.redis.timeout=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.cache.type=Redis #thymeleaf
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false #upload max file size
spring.http.multipart.maxFileSize=25Mb
spring.http.multipart.maxRequestSize=50Mb #email config
spring.mail.host=192.168.8.208
spring.mail.username=xxx@wunaozai.com
spring.mail.password=
srping.mail.default-encoding=UTF-8

9.一份简单的 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.wunaozai.center</groupId>
<artifactId>service-developer-center</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging> <name>service-developer-center</name>
<description>XXX开发者中心</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Edgware.SR1</spring-cloud.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <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>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>