spring cloud config搭建说明例子(四)-补充配置文件

服务端 ConfigServer

pom.xml

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
                <dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</artifactId>
		</dependency>
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>

spring-cloud-starter-eureka与spring-cloud-starter-eureka-server的区别
spring-cloud-starter-eureka-server已不推荐,有替代包。
原文:Spring Cloud Starter Eureka Server (deprecated, please use spring-cloud-starter-netflix-eureka-server)
见:http://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server

app类

@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@EnableDiscoveryClient与@EnableEurekaClient的区别
Eureka推荐用后者,其他服务发现用前者。

application.yml

server:
   port: ${PORT:8888}                                #配置工程端口号

spring:
   application:
      name: cloud-config-server                     #设置该服务应用名称
   profiles:
      active: native                                 #设置读取为本地工程文件
   config:
      server:
         native:
            searchLocations: classpath:/config       #配置文件根目录,也就是XXX-dev.properties等的目录

#注册到eureka服务中心进行监控
eureka:
   client:
      serviceUrl:
          defaultZone: http://eureka:eureka@localhost:8761/eureka # 可以逗号分隔,配置多个
      healthcheck:
          enabled: true                                                         #开启健康监控
   instance:
      prefer-ip-address: true
      instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
      leaseRenewalIntervalInSeconds: 30 
      leaseExpirationDurationInSeconds: 90

官网也用发发发发这个端口。

配置文件

config目录下配置文件:
XXX-dev.properties
XXX-test.properties

db.type=com.alibaba.druid.pool.DruidDataSource
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/MYDB?useUnicode=true&characterEncoding=UTF8
db.username=dbuser
db.password=123456

客户端AppClient

pom.xml也需要增加autuator

	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
        <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-eureka</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>

bootstrap.properties配置

spring.cloud.config.name=XXX
spring.cloud.config.profile=dev
#spring.cloud.config.profile=test
spring.cloud.config.uri=http\://localhost\:8888/ # 只能配置一个,不能逗号分隔配置多个config

application.yml配置

server:
   port: 8080                                  #设置当前服务端口
   context-path: /abc                          #设置服务上下文路径

spring:
   application:
      name: app-client                  #service name 设置当前服务名称

eureka:
   client:
      serviceUrl:
          defaultZone: http://eureka:eureka@localhost:8761/eureka   # 可以逗号分隔,配置多个
      healthcheck:
          enabled: true
   instance:
      prefer-ip-address: true                              # 注册到Eureka Server上的是IP
      instanceId: ${spring.application.name}:${spring.application.instance_id:${server.port}}
      leaseRenewalIntervalInSeconds: 15                    # 心跳时间,即服务续约间隔时间(缺省为30s)  
      leaseExpirationDurationInSeconds: 45                  # 发呆时间,即服务续约到期时间(缺省为90s)

application类

@SpringBootApplication
@EnableEurekaClient
public class ClientApplication {

	public static void main(String[] args) throws Exception {
		SpringApplication.run(ClientApplication.class, args);
	}
}

参考资料:
spring cloud config搭建说明例子(三)
spring cloud config搭建说明例子(二)
https://blog.csdn.net/hh652400660/article/details/79474419
http://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server

posted @ 2018-06-03 10:33  Danni3  阅读(300)  评论(0编辑  收藏  举报