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

客官°小女子只卖身不卖艺 2024-02-18 19:16 100阅读 0赞

基于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启动类:

  1. @SpringBootApplication
  2. @EnableConfigServer
  3. public class ConfigApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ConfigApplication.class, args);
  6. }
  7. }

1.2、添加config的配置文件

1.2.1、总配置文件application.yml

  1. server:
  2. port: 8008
  3. spring:
  4. application:
  5. name: config
  6. security:
  7. user:
  8. password: ${CONFIG_SERVICE_PASSWORD}
  9. ---
  10. spring:
  11. profiles: native
  12. cloud:
  13. config:
  14. server:
  15. native:
  16. search-locations: classpath:/shared
  17. ---
  18. spring:
  19. profiles:
  20. active: native

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

1.2.2.1、配置shared环境的总配置文件application.yml
  1. logging:
  2. level:
  3. org.springframework.security: INFO
  4. path: logs/${spring.application.name}/
  5. hystrix:
  6. command:
  7. default:
  8. execution:
  9. isolation:
  10. thread:
  11. timeoutInMilliseconds: 10000
  12. ## 通过下面的地址配置,找到registry_service服务(注册中心),
  13. ## 根据注册中心对应的服务名称找到对应的服务地址及端口
  14. eureka:
  15. instance:
  16. prefer-ip-address: true # 实例名称显示IP配置
  17. client:
  18. serviceUrl:
  19. defaultZone: http://localhost:8001/eureka/ # 配置eureka服务器的位置
  20. spring:
  21. # rabbitmq:
  22. # host: rabbitmq
  23. # port: 5672
  24. # username: guest
  25. # password: guest
  26. # 日志输出默认是多彩形式,
  27. # NEVER:禁用ANSI-colored输出(默认项)
  28. # DETECT:会检查终端是否支持ANSI,是的话就采用彩色输出(推荐项)
  29. # ALWAYS:总是使用ANSI-colored格式输出,若终端不支持的时候,会有很多干扰信息,不推荐使用
  30. output:
  31. ansi:
  32. enabled: always
  33. aop:
  34. auto: true
  35. proxy-target-class: true
  36. jpa:
  37. hibernate:
  38. ddl-auto: update
  39. show-sql: true
  40. open-in-view: true
  41. management:
  42. security:
  43. enabled: false
1.2.2.2、配置shared环境下regisry模块的配置(registry.yml)
  1. server:
  2. port: 8001
1.2.2.3、配置shared环境下client模块的配置(client-test.yml)
  1. server:
  2. port: 8002
  3. # 数据库访问配置
  4. # 主数据源,默认的
  5. spring:
  6. datasource:
  7. type: com.alibaba.druid.pool.DruidDataSource
  8. driver-class-name: com.mysql.jdbc.Driver
  9. url: jdbc:mysql://production-db:3326/production?useUnicode=true&characterEncoding=UTF-8
  10. username: root
  11. password: db_password
  12. initialSize: 5 # 下面为连接池的补充设置,应用到上面所有数据源中
  13. minIdle: 5 # 初始化大小,最小,最大
  14. maxActive: 20
  15. maxWait: 60000 # 配置获取连接等待超时的时间
  16. timeBetweenEvictionRunsMillis: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
  17. validationQuery: SELECT 1 FROM DUAL # 配置一个连接在池中最小生存的时间,单位是毫秒
  18. testWhileIdle: true
  19. testOnBorrow: false
  20. testOnReturn: false
  21. poolPreparedStatements: true # 打开PSCache,并且指定每个连接上PSCache的大小
  22. maxPoolPreparedStatementPerConnectionSiz: 20
  23. filters: stat,wall,log4j # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
  24. connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
  25. #useGlobalDataSourceStat: true # 合并多个DruidDataSource的监控数据

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

2.1、添加SpringBoot启动类:

  1. @EnableEurekaServer
  2. @SpringBootApplication
  3. public class ServerApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(ServerApplication.class, args);
  6. }
  7. }

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

  1. server:
  2. port: 8001
  3. spring:
  4. application:
  5. name: registry
  6. cloud:
  7. config:
  8. uri: http://config:8008
  9. fail-fast: true
  10. password: ${CONFIG_SERVICE_PASSWORD}
  11. username: user
  12. eureka:
  13. instance:
  14. prefer-ip-address: true
  15. hostname: registry1
  16. client:
  17. serviceUrl:
  18. defaultZone: http://localhost:8001/eureka/
  19. registerWithEureka: false
  20. fetchRegistry: false
  21. server:
  22. waitTimeInMsWhenSyncEmpty: 0

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

3.1、添加SpringBoot启动类:

  1. @SpringBootApplication
  2. @EnableDiscoveryClient
  3. //@EnableTransactionManagement
  4. //@EnableConfigurationProperties
  5. //@EnableAsync
  6. //@EnableScheduling
  7. //@EnableFeignClients
  8. public class ClientApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(ClientApplication.class, args);
  11. }
  12. }

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

  1. spring:
  2. application:
  3. name: client-test
  4. cloud:
  5. config:
  6. uri: http://config:8008
  7. fail-fast: true
  8. password: ${CONFIG_SERVICE_PASSWORD}
  9. username: user
  10. #第一种方式:eureka实现
  11. eureka:
  12. client:
  13. serviceUrl:
  14. defaultZone: http://localhost:8001
  15. #第二种方式:consul实现
  16. #spring:cloud:consul:host=localhost
  17. #spring:cloud:consul:port=8500

发表评论

表情:
评论列表 (有 0 条评论,100人围观)

还没有评论,来说两句吧...

相关阅读