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

Posted 无脑仔的小明

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了物联网架构成长之路(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格式,需要自定义一个配置类

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

 

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

 1 @Component
 2 public class OnStartListener implements ApplicationListener<ContextRefreshedEvent> {
 3 
 4     @Override
 5     public void onApplicationEvent(ContextRefreshedEvent event) {
 6         System.out.println(event.getApplicationContext().getParent());
 7         System.out.println("on start: " + System.currentTimeMillis());
 8     }
 9 
10 }

 

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

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

 

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

 

8.一份简单的application.properties

 1 spring.application.name=service-developer-center
 2 
 3 #server
 4 server.port=9200
 5 server.session.timeout=360000
 6 server.session.cookie.name=IOT
 7 
 8 #eureka
 9 eureka.client.serviceUrl.defaultZone=http://172.16.23.203:9000/eureka/
10 eureka.instance.prefer-ip-address=true
11 eureka.instance.instance-id=${spring.cloud.client.ipAddress}:${server.port}
12 
13 #logging
14 logging.path=log
15 logging.level.com.favorites=INFO
16 logging.level.org.springframework.web=INFO
17 
18 #datasource
19 spring.datasource.url=jdbc:postgresql://172.16.23.202:5432/service_developer_center
20 spring.datasource.username=postgres
21 spring.datasource.password=
22 spring.datasource.driverClassName=org.postgresql.Driver
23 
24 #mybatis
25 mybatis.type-aliases-package=com.wunaozai.xxx.center.model
26 mybatis.mapper-locations=classpath:com/wunaozai/xxx/center/mapper/*.xml
27 
28 #pagehelper
29 pagehelper.helperDialect=postgresql
30 pagehelper.reasonable=true
31 pagehelper.supportMethodsArguments=true
32 pagehelper.returnPageInfo=check
33 
34 #redis
35 spring.redis.database=0
36 spring.redis.host=172.16.20.229
37 spring.redis.port=6379
38 spring.redis.password=
39 spring.redis.timeout=0
40 spring.redis.pool.max-active=8
41 spring.redis.pool.max-wait=-1
42 spring.redis.pool.max-idle=8
43 spring.redis.pool.min-idle=0
44 spring.cache.type=Redis
45 
46 #thymeleaf
47 spring.thymeleaf.mode=html5
48 spring.thymeleaf.encoding=UTF-8
49 spring.thymeleaf.content-type=text/html
50 spring.thymeleaf.cache=false
51 
52 #upload max file size
53 spring.http.multipart.maxFileSize=25Mb
54 spring.http.multipart.maxRequestSize=50Mb
55 
56 #email config
57 spring.mail.host=192.168.8.208
58 spring.mail.username=[email protected]
59 spring.mail.password=
60 srping.mail.default-encoding=UTF-8

 

9.一份简单的 pom.xml

  1 <?xml version="1.0" encoding="UTF-8"?>
  2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4     <modelVersion>4.0.0</modelVersion>
  5 
  6     <groupId>com.wunaozai.center</groupId>
  7     <artifactId>service-developer-center</artifactId>
  8     <version>0.0.1</version>
  9     <packaging>jar</packaging>
 10 
 11     <name>service-developer-center</name>
 12     <description>XXX开发者中心</description>
 13 
 14     <parent>
 15         <groupId>org.springframework.boot</groupId>
 16         <artifactId>spring-boot-starter-parent</artifactId>
 17         <version>1.5.9.RELEASE</version>
 18         <relativePath/> <!-- lookup parent from repository -->
 19     </parent>
 20 
 21     <properties>
 22         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 23         <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 24         <java.version>1.8</java.version>
 25         <spring-cloud.version>Edgware.SR1</spring-cloud.version>
 26     </properties>
 27 
 28     <dependencies>
 29         <dependency>
 30             <groupId>org.springframework.cloud</groupId>
 31             <artifactId>spring-cloud-starter-eureka-server</artifactId>
 32         </dependency>
 33         <dependency>
 34             <groupId>org.springframework.boot</groupId>
 35             <artifactId>spring-boot-starter-web</artifactId>
 36         </dependency>
 37         <dependency>
 38             <groupId>org.postgresql</groupId>
 39             <artifactId>postgresql</artifactId>
 40         </dependency>
 41         <dependency>
 42             <groupId>org.mybatis.spring.boot</groupId>
 43             <artifactId>mybatis-spring-boot-starter</artifactId>
 44             <version>1.3.1</version>
 45         </dependency>
 46         <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
 47         <dependency>
 48             <groupId>com.github.pagehelper</groupId>
 49             <artifactId>pagehelper-spring-boot-starter</artifactId>
 50             <version>1.2.3</version>
 51         </dependency>
 52         <dependency>
 53             <groupId>org.springframework.boot</groupId>
 54             <artifactId>spring-boot-starter-data-redis</artifactId>
 55         </dependency>
 56         <!-- https://mvnrepository.com/artifact/org.springframework.session/spring-session-data-redis -->
 57         <dependency>
 58             <groupId>org.springframework.session</groupId>
 59             <artifactId>spring-session-data-redis</artifactId>
 60         </dependency>
 61         <dependency>
 62             <groupId>org.springframework.boot</groupId>
 63             <artifactId>spring-boot-starter-thymeleaf</artifactId>
 64         </dependency>
 65         <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
 66         <dependency>
 67             <groupId>com.aliyun.oss</groupId>
 68             <artifactId>aliyun-sdk-oss</artifactId>
 69             <version>3.0.0</version>
 70         </dependency>
 71         <!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
 72         <dependency>
 73             <groupId>commons-io</groupId>
 74             <artifactId>commons-io</artifactId>
 75             <version>2.6</version>
 76         </dependency>
 77         <!-- https://mvnrepository.com/artifact/commons-fileupload/commons-fileupload -->
 78         <dependency>
 79             <groupId>commons-fileupload</groupId>
 80             <artifactId>commons-fileupload</artifactId>
 81             <version>1.3.3</version>
 82         </dependency>
 83         <dependency>
 84             <groupId>org.springframework.boot</groupId>
 85             <artifactId>spring-boot-starter-mail</artifactId>
 86         </dependency> 
 87 
 88         <dependency>
 89             <groupId>org.springframework.boot</groupId>
 90             <artifactId>spring-boot-devtools</artifactId>
 91             <scope>runtime</scope>
 92         </dependency>
 93         <dependency>
 94             <groupId>org.springframework.boot</groupId>
 95             <artifactId>spring-boot-starter-test</artifactId>
 96             <scope>test</scope>
 97         </dependency>
 98     </dependencies>
 99 
100     <dependencyManagement>
101         <dependencies>
102             <dependency>
103                 <groupId>org.springframework.cloud</groupId>
104                 <artifactId>spring-cloud-dependencies</artifactId>
105                 <version>${spring-cloud.version}</version>
106                 <type>pom</type>
107                 <scope>import</scope>
108             </dependency>
109         </dependencies>
110     </dependencyManagement>
111 
112     <build>
113         <plugins>
114             <plugin>
115                 <groupId>org.springframework.boot</groupId>
116                 <artifactId>spring-boot-maven-plugin</artifactId>
117             </plugin>
118         </plugins>
119     </build>
120 
121 
122 </project>

 

 

以上是关于物联网架构成长之路(17)-SpringCloud目前遇到的注意事项的主要内容,如果未能解决你的问题,请参考以下文章

物联网架构成长之路(12)-物联网架构小结1

物联网架构成长之路-目录

物联网架构成长之路(13)-SpringBoot入门

物联网架构成长之路(35)-利用Netty解析物联网自定义协议

物联网架构成长之路(58)-用户与应用服务器之WebSocket通信

物联网架构成长之路(11)-Redis缓存主从复制