码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-06-03 12:34:50      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:cep   间隔   ruid   coding   word   cte   instance   lease   需要   

服务端 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的区别?

app类

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

@EnableDiscoveryClient与@EnableEurekaClient的区别

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搭建说明例子(二)

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

标签:cep   间隔   ruid   coding   word   cte   instance   lease   需要   

原文地址:https://www.cnblogs.com/ouyida3/p/9128213.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!