物聯網架構成長之路(17)-SpringCloud目前遇到的註意事項
阿新 • • 發佈:2018-02-10
boot spa nbsp gpo 註解 getparent scrip mvc decompile
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 @Override5 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目前遇到的註意事項