基于SpringCloud、SpringBoot构建的微服务框架

基于SpringCloud、SpringBoot构建的微服务框架


重要说明:

    1、config中的模块的配置文件的名字需要同模块中设置的spring.application.name相同!

  2、config中的个模块的配置文件的优先级大于模块自己的配置文件,以reristry模块举例:config中的resource\shared\registry.yml中的配置优先级大于registry模块中的application.yml中的配置优先级!

    3、config中的总的application.yml不会对具体的环境下的所有模块生效,只会对自己也就是config生效!

   4、EurekaServer和EurekaClient配置中的eureka.client.serviceUrl一定要一致,不一致会导致EurekaClient无法注册到EurekaServer!

    5、eureka.client.serviceUrl的地址的格式一定要是:http://ip或者赢或者注册名:端口号/eureka/。最后一定要加/eureka或者/eureka/(如果/eureka后面少/的话,会自动在其后加一个/),不加的话注册中心EurekaServer能启动,但是客户端EurekaClient无法启动,无法注册到EurekaServer!
  6、优先级:config项目\resource\shared\客户端.yml>config项目\resource\shared\application.yml>项目中的application.yml


一、开发SpringCloudConfig服务(配置中心),工程名为:config

  1.1、添加SpringBoot启动类:

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

  1.2、添加config的配置文件

    1.2.1、总配置文件application.yml

      
server:
  port: 8008

spring:
  application:
    name: config

security:
  user:
    password: ${CONFIG_SERVICE_PASSWORD}

---
spring:
  profiles: native
  cloud:
    config:
      server:
        native:
          search-locations: classpath:/shared
---
spring:
  profiles:
    active: native

    1.2.2、配置shared环境的各模块的配置文件

      1.2.2.1、配置shared环境的总配置文件application.yml
           
logging:
  level:
    org.springframework.security: INFO
  path: logs/${spring.application.name}/

hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 10000

## 通过下面的地址配置,找到registry_service服务(注册中心),
## 根据注册中心对应的服务名称找到对应的服务地址及端口
eureka:
  instance:
    prefer-ip-address: true # 实例名称显示IP配置
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/ # 配置eureka服务器的位置

spring:
#  rabbitmq:
#      host: rabbitmq
#      port: 5672
#      username: guest
#      password: guest

# 日志输出默认是多彩形式,
# NEVER:禁用ANSI-colored输出(默认项)
# DETECT:会检查终端是否支持ANSI,是的话就采用彩色输出(推荐项)
# ALWAYS:总是使用ANSI-colored格式输出,若终端不支持的时候,会有很多干扰信息,不推荐使用
output:
  ansi:
    enabled: always
aop:
  auto: true
  proxy-target-class: true
jpa:
  hibernate:
    ddl-auto: update
  show-sql: true
  open-in-view: true

management:
  security:
    enabled: false
      1.2.2.2、配置shared环境下regisry模块的配置(registry.yml)
server:
  port: 8001
      1.2.2.3、配置shared环境下client模块的配置(client-test.yml)
server:
  port: 8002
# 数据库访问配置
# 主数据源,默认的
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://production-db:3326/production?useUnicode=true&characterEncoding=UTF-8
    username: root
    password: db_password
    initialSize: 5    # 下面为连接池的补充设置,应用到上面所有数据源中
    minIdle: 5        # 初始化大小,最小,最大
    maxActive: 20
    maxWait: 60000     # 配置获取连接等待超时的时间
    timeBetweenEvictionRunsMillis: 60000    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    validationQuery: SELECT 1 FROM DUAL  # 配置一个连接在池中最小生存的时间,单位是毫秒
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true  # 打开PSCache,并且指定每个连接上PSCache的大小
    maxPoolPreparedStatementPerConnectionSiz: 20
    filters: stat,wall,log4j    # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    #useGlobalDataSourceStat: true # 合并多个DruidDataSource的监控数据

二、开发EurekaServer服务(注册中心),工程名为:registry

  2.1、添加SpringBoot启动类:

    
@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServerApplication.class, args);
    }
}

  2.2、resource下添加bootstrap.yml(bootstrap.yml相比application.yml的作用就是会先加载)    

server:
  port: 8001

spring:
  application:
    name: registry
  cloud:
    config:
      uri: http://config:8008
      fail-fast: true
      password: ${CONFIG_SERVICE_PASSWORD}
      username: user

eureka:
  instance:
    prefer-ip-address: true
    hostname: registry1
  client:
    serviceUrl:
      defaultZone: http://localhost:8001/eureka/
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0

三、开发EurekaClient服务(客户端),工程名为:clientTest

  3.1、添加SpringBoot启动类:    

@SpringBootApplication
@EnableDiscoveryClient
//@EnableTransactionManagement
//@EnableConfigurationProperties
//@EnableAsync
//@EnableScheduling
//@EnableFeignClients
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }
}

  3.2、配置client的配置(bootstrap.yml):

spring:
  application:
    name: client-test
  cloud:
    config:
      uri: http://config:8008
      fail-fast: true
      password: ${CONFIG_SERVICE_PASSWORD}
      username: user


#第一种方式:eureka实现
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8001


#第二种方式:consul实现
#spring:cloud:consul:host=localhost
#spring:cloud:consul:port=8500
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值