idea创建springcloud项目-中篇(cloud工程配置application.yml)

上一篇: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/看看效果。

好,漂亮。。。。。。

 

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值