1.首先需要创建注册中心工程 ,后期的后端服务器和前端服务器都要注册到本注册中心 ,形成一个完成的工程体系
添加Eureka Server依赖
在主类上添加@EnableEurekaServer注解 , 开启服务
在配置文件中添加如下配置 name为注册中心的名字,一般设置为工程名字
spring.application.name=atcrowdfunding-reg-center 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/port为端口号 , 不要重复即可 hostname为服务器所在机器的地址 eureka和registry的状态表示注册中心是否需要键集群 ,如果需要注册到其他注册中心则设置为true defaultZone为本服务器发布的注册网址 ,客户端和服务端根据此网址注册
2.创建公共资源中心 ,不需要添加任何依赖
主要用于存放bean包 ,util包,listener类 ,exception类等各种公共资源
主函数类不用删 , 打包工程的时候需要使用 ,所以不建议删除
为了解决项目的路径问题 ,需配置一个监听器 ,将项目名放入application域 , 需在类上加@WebListener声明监听器 相应的可以添加@WebServlet //声明Servlet对象 @WebFilter //声明过滤器对象 使用的时候只需在相应的服务器主类上添加扫描注解@ServletComponentScan
3.创建客户端项目 (所有的客户端项目创建步骤都是一样的)
添加Eureka Discovery, Fegin , Freemarker , Web 依赖 ,创建成功后依赖于common
除注册中心和common外都需依赖Eureka Discovery , Fegin 用于接口转发请求到服务器 , Freemarker页面解析
主类上增加@EnableFeignClients ,@EnableDiscoveryClient开启对应的服务, 加@ServletComponentScan启用监听器扫描
静态资源放在static文件夹下 , 页面放在templates文件下
配置pom.xml文件
配置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>
配置application.properties文件
客户端服务器配置
spring.application.name=atcrowdfunding-boot-portal eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/ server.context-path=/ server.port=8080 server.session.timeout=60 server.tomcat.max-threads=800 server.tomcat.uri-encoding=UTF-8freemark配置
spring.freemarker.allow-request-override=false spring.freemarker.allow-session-override=false spring.freemarker.cache=false 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=1redis的session共享配置
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 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客户端包结构
controller:处理前端页面请求 , 处理数据调用service接口 service:被controller调用 , 做请求转发 ,调用服务端的controller service中的方法和服务端controller中的方法声明需一样
4.创建服务端项目 (所有的服务端创建步骤都是一样的)
添加Eureka Discovery ,创建成功后依赖于common ,依赖于Mybatis
在主类上增加@EnableTransactionManagement ,@MapperScan("com.atguigu.**.dao") ,@EnableDiscoveryClient注解开启相应服务
配置pom.xml文件
<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>
加入数据库连接驱动 ,如果配置还是无法连接可以手动将驱动版本设置为 5.1.45 加入Druid jar包 加入spring与mybatis的整合jar包
配置application.yml文件
--- spring: datasource: name: mydb type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://127.0.0.1:3306/atcrowdfunding username: root password: abcd driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath*:/mybatis/*Mapper.xml type-aliases-package: com.lwj.**.bean指定数据库连接池 要连接的数据库地址 数据库连接用户名和密码 数据库连接驱动 mybatis的Mapper.xml所在路径 bean对象所在路径
配置application.properties文件
spring.application.name=atcrowdfunding-boot-member-service server.port=2001 eureka.client.serviceUrl.defaultZone=http://127.0.0.1:1001/eureka/指定服务器的名称 ,客户端接口转发时使用 服务器端口号 注册中心的网址
服务端包结构
controller: 因为服务端数据全部需返回给客户端的service接口 ,需加@RestController注解 service:接口层 service.impl: 接口的实现层 ,被controller调用 , 做一些逻辑处理 , 调用dao层对数据库进行操作 , dao: 直接操作数据库 , 简单语句用@Select注解直接写sql语句 , 复杂的写在Mapper.xml文件中
其他
热部署配置依赖 , 只能热部署已写好方法内的代码 , 新建方法之后还需重启服务器(此功能鸡肋)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!-- 这个需要为 true 热部署才有效 --> </dependency>