springboot模块

时间:2024-09-15 22:36:44

1.web

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

server.context-path=/
server.port=80
server.session.timeout=60
server.tomcat.max-threads=800
server.tomcat.uri-encoding=UTF-8

@ComponentScan(basePackages="com.atguigu")

2.字符编码
spring.http.encoding.charset=UTF-8
spring.http.encoding.force=true

3.freemarker

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

spring.freemarker.allow-request-override=false
spring.freemarker.allow-session-override=false
spring.freemarker.cache=true
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.enabled=true
spring.freemarker.expose-request-attributes=false
spring.freemarker.expose-session-attributes=false
spring.freemarker.expose-spring-macro-helpers=true
spring.freemarker.prefer-file-system-access=false
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
spring.freemarker.settings.template_update_delay=0
spring.freemarker.settings.default_encoding=UTF-8
spring.freemarker.settings.classic_compatible=true
spring.freemarker.order=1

4.集成Nginx

server.address=127.0.0.1
server.tomcat.remote-ip-header=x-forwarded-for
server.tomcat.protocol-header=x-forwarded-proto
server.tomcat.port-header=X-Forwarded-Port
server.use-forward-headers=true

5.redis

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
spring.session.store-type=redis
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.password=123123
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=60000

@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {

}

设置Redis Session共享后,如果向Session中保存数据,需要让数据对象实现可序列化接口java.io.Serializable

6.mysql,pool,mybatis

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.5</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

spring:
datasource:
name: mydb
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://127.0.0.1:3306/atcrowdfunding
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapperLocations: classpath*:mybatis/mapper-*.xml
typeAliasesPackage: com.atguigu.**.bean

@MapperScan("com.atguigu.**.dao")
@EnableTransactionManagement
@Transactional(readOnly=true)

public interface MemberDao {

@Select("select * from t_member where id = #{id}")
public Member queryById(Integer id);

@Insert("insert into t_member (loginacct) values (#{loginacct})")
public int insertMember(Member member);

}

7.热部署
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional><!-- 这个需要为 true 热部署才有效-->
</dependency>

8.打jar包 :数据库文件位置不对.将src/main/resources下的文件复制到src/main/java目录下,再打jar包即可.
打war包:需要将springboot tomcat模块设成provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

9
9.1注册中心

建项目时勾选Eureka Server
在类前增加注解@EnableEurekaServer
在application.properties文件中,增加配置信息

...
spring.application.name=eureka-server
server.port=1001
eureka.instance.hostname=127.0.0.1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/
...

9.2服务端
建项目时勾选Eureka Discovery
在类前增加注解@EnableDiscoveryClient
在application.properties文件中,增加配置信息

spring.application.name=eureka-member-service
server.port=2001
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1001/eureka/

9.3客户端
建项目时勾选Eureka Discovery和Feign
在类前增加注解@EnableDiscoveryClient, @EnableFeignClients
在application.properties文件中,增加配置信息

spring.application.name=eureka-member-client
server.port=80
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

@FeignClient("eureka-member-service")
public interface MemberClient {

@GetMapping("/test")
String test(); //与服务的方法返回类型一致

}

@FeignClient("eureka-member-service")中的字符串内容应该和会员服务在注册中心注册的名称相同eureka-member-service