上一篇:idea创建springcloud项目-上篇(idea创建工程)
生成目录结构如下:
启动读取时,新加application.yml先于application.properties。yml写法简捷,不懂百度一下。
另外pom.xml要添加一些启动配置,启动时读取*.xml,*.properties,*.yml所有配置文件。
第一步:修改pom.xml
添加配置:在pom.xml里的<build>加</build>节点里添加
<!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
第二步:读取配置文件加好了,就要在application.yml添加具体项目数据配置,比如数据库,eureka,端口等。
server:
port: 8761
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
eureka:
environment: dev
instance:
preferIpAddress: true
lease-expiration-duration-in-seconds: 60
lease-renewal-interval-in-seconds: 5
server:
enable-self-preservation: true
client:
register-with-eureka: false
fetch-registry: false
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka
1.配置启动端口,必须的
server:
port: 8761
2.配置连接数据库,因为加入了mybatis,会去连库,找不到库,连接不上会报错不能启动。必加。我用的是mysql。
如果连别的数据库,百度一下,url,driver-class-name配置。
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
3.配置eureka监控服务,正好可以测试一下是否启动成功。
eureka:
environment: dev
instance:
preferIpAddress: true
lease-expiration-duration-in-seconds: 60
lease-renewal-interval-in-seconds: 5
server:
enable-self-preservation: true
client:
register-with-eureka: false
fetch-registry: false
registry-fetch-interval-seconds: 5
serviceUrl:
defaultZone: http://127.0.0.1:8761/eureka
4.找到CloudApplication.java,main方法启动类。添加eureka注解。@EnableEurekaServer
5.在上图第10行,点击启动项目。
看到这图说明启动好了。访问链接http://127.0.0.1:8761/看看效果。
好,漂亮。。。。。。