Spring Cloud Config 配置中心(六)

冷不防 2021-12-15 23:25 380阅读 0赞

目录直通车

一、面临的问题

二、解决问题

三、配置及使用Config

1、配置Config的Server端

2、配置Eureka的Server端

3、微服务提供者

4、启动与测试


一、面临的问题

分布式系统面临的配置问题!

微服务把单体应用中的业务拆分成一个个子服务,经过之前的学习,大家都知道,每新建一个模块都伴随一个yml的配置文件,倘若100个模块岂不是会存在100个yml?

于是需要一套集中式的、动态配置管理设施是必不可少的。ConfigServer就出现了。

二、解决问题

在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是config client。

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ3OTk5_size_16_color_FFFFFF_t_70

1、集中管理配置文件

2、不同环境不同配置,动态更新配置,分环境部署dev/test/prod/beta/release

3、运行期间动态调整配置,服务会向配置中心统一拉取自己的配置信息

4、更改配置后,服务不用重启即可更新配置

5、将配置信息以Restful api的形式暴露

三、配置及使用Config

先说一下最终实现的效果吧~

(1)将所有的配置文件放到Github中,实现Config的集中式管理。

(2)将微服务提供者注册成Eureka的客户端与Config的客户端并注册进Eureka的服务端。

(3)最终展现出上面图片中的流程。

1、配置Config的Server端

新建如下maven模块,结构如下:

20190703204551584.png

POM

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. <version>2.0.0.M9</version>
  6. </dependency>
  7. </dependencies>

启动类

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

application.yml

  1. server:
  2. port: 3344
  3. spring:
  4. application:
  5. name: microservice-config
  6. cloud:
  7. config:
  8. server:
  9. git:
  10. uri: https://github.com/DJun5/microservice-demo-config

2、配置Eureka的Server端

新建如下模块:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ3OTk5_size_16_color_FFFFFF_t_70 1

POM

  1. <dependencies>
  2. <!-- Config Client -->
  3. <dependency>
  4. <groupId>org.springframework.cloud</groupId>
  5. <artifactId>spring-cloud-starter-config</artifactId>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter-jetty</artifactId>
  10. </dependency>
  11. <!-- web -->
  12. <dependency>
  13. <groupId>org.springframework.boot</groupId>
  14. <artifactId>spring-boot-starter-web</artifactId>
  15. </dependency>
  16. <!-- Actuator,感应服务端变化-->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-actuator</artifactId>
  20. </dependency>
  21. <!-- Eureka Server-->
  22. <dependency>
  23. <groupId>org.springframework.cloud</groupId>
  24. <artifactId>spring-cloud-netflix-eureka-server</artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.cloud</groupId>
  28. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  29. </dependency>
  30. <!-- 修改后立即生效,热部署 -->
  31. <dependency>
  32. <groupId>org.springframework</groupId>
  33. <artifactId>springloaded</artifactId>
  34. <version>${springloaded.version}</version>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-devtools</artifactId>
  39. </dependency>
  40. </dependencies>

启动类

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

eureka-7001.yml

此文件内容放在上面配置的Github仓库中,三根减号“—-”表示区域分隔线。

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. server:
  7. port: 7001
  8. spring:
  9. profiles: dev
  10. application:
  11. name: demo-config-eureka
  12. eureka:
  13. instance:
  14. hostname: eureka7001.com
  15. client:
  16. # 自己不注册进服务列表
  17. register-with-eureka: false
  18. # 不通过eureka获取注册信息
  19. fetch-registry: false
  20. service-url:
  21. # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  22. defaultZone: http://eureka7001.com:7001/eureka/
  23. ---
  24. server:
  25. port: 7001
  26. spring:
  27. profiles: test
  28. application:
  29. name: demo-config-eureka
  30. eureka:
  31. instance:
  32. hostname: eureka7001.com
  33. client:
  34. # 自己不注册进服务列表
  35. register-with-eureka: false
  36. # 不通过eureka获取注册信息
  37. fetch-registry: false
  38. service-url:
  39. # defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
  40. defaultZone: http://eureka7001.com:7001/eureka/

bootstrap.yml

(1)bootstrap(系统级)的优先级高于application(用户级)。

(2)下面的配置作用为:访问此路径 http://localhost:3344/eureka-7001-dev.yml 使得 Config Server 端去 Github 找 eureka-7001.yml 的 dev 部分。

  1. spring:
  2. cloud:
  3. config:
  4. name: eureka-7001
  5. profile: dev
  6. label: master
  7. uri: http://localhost:3344

application.yml

这个文件可以不配置了,只是习惯的问题,写了以下内容

  1. spring:
  2. application:
  3. name: eureka-7001

3、微服务提供者

这个模块需要注册为Config的Client和Eureka的Client,新建如下目录:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ3OTk5_size_16_color_FFFFFF_t_70 2

Controller

  1. @RestController
  2. @RequestMapping("/dept")
  3. public class DeptController {
  4. @Resource
  5. private DeptService service;
  6. @PostMapping("/add")
  7. public JSONResult add( @RequestBody Dept model){
  8. return JSONResult.ok(service.add(model));
  9. }
  10. @GetMapping("/findById/{id}")
  11. // 切面的方式
  12. @HystrixCommand(fallbackMethod = "processHystrix_Get")
  13. public JSONResult get(@PathVariable("id") Long id){
  14. List<Dept> dept ;
  15. dept = service.get(id);
  16. if (dept.size() == 0){
  17. // throw new RuntimeException("该ID:"+id+",没有对应的信息");
  18. return JSONResult.errorMsg("该ID:"+id+",没有对应的信息");
  19. }
  20. return JSONResult.ok(service.get(id));
  21. }
  22. @GetMapping("/list")
  23. public JSONResult getAll(){
  24. return JSONResult.ok(service.list());
  25. }
  26. @PostMapping("/delete/{id}")
  27. public JSONResult delete(@PathVariable("id") Long id){
  28. return JSONResult.ok(service.delete(id));
  29. }
  30. /**
  31. * 响应注解 @HystrixCommand
  32. */
  33. public JSONResult processHystrix_Get(Long id){
  34. return JSONResult.errorMsg("该ID:"+id+",没有对应的信息--->@HystrixCommand");
  35. }
  36. }

Mapper

  1. @Mapper
  2. public interface DeptMapper {
  3. boolean addDept(Dept modelName);
  4. List<Dept> findById(Long id);
  5. List<Dept> findAll();
  6. boolean delete(Long id);
  7. }

Service

  1. public interface DeptService {
  2. boolean add(Dept modelName);
  3. List<Dept> get(Long id);
  4. List<Dept> list();
  5. boolean delete(Long id);
  6. }

ServiceImpl

  1. @Service
  2. public class DeptServiceImpl implements DeptService {
  3. @Resource
  4. private DeptMapper mapper;
  5. @Override
  6. public boolean add(Dept modelName) {
  7. return mapper.addDept(modelName);
  8. }
  9. @Override
  10. public List<Dept> get(Long id) {
  11. return mapper.findById(id);
  12. }
  13. @Override
  14. public List<Dept> list() {
  15. return mapper.findAll();
  16. }
  17. @Override
  18. public boolean delete(Long id) {
  19. return mapper.delete(id);
  20. }
  21. }

启动类

  1. @SpringBootApplication
  2. // 服务启动之后自动注册进入Eureka
  3. @EnableEurekaClient
  4. // 服务发现
  5. @EnableDiscoveryClient
  6. // Hystrix断路器
  7. @EnableCircuitBreaker
  8. public class ConfigProvider8001_App {
  9. public static void main(String[] args) {
  10. SpringApplication.run(ConfigProvider8001_App.class,args);
  11. }
  12. // 解决SpringBoot2.0版本对hystrix监控流失效的问题
  13. @Bean
  14. public ServletRegistrationBean getServlet() {
  15. HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
  16. ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
  17. registrationBean.setLoadOnStartup(1);
  18. registrationBean.addUrlMappings("/hystrix.stream");
  19. registrationBean.setName("HystrixMetricsStreamServlet");
  20. return registrationBean;
  21. }
  22. }

provider-dept-8001.yml 放Github仓库

  1. spring:
  2. profiles:
  3. active:
  4. - dev
  5. ---
  6. server:
  7. port: 8001
  8. spring:
  9. profiles: dev
  10. application:
  11. name: dept-hystrix-provider-8001
  12. datasource:
  13. driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
  14. url: jdbc:mysql://localhost:3306/cloud01 # 数据库名称
  15. username: root
  16. password: root
  17. type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
  18. dbcp2:
  19. min-idle: 5 # 数据库连接池的最小维持连接数
  20. initial-size: 5 # 初始化连接数
  21. max-total: 5 # 最大连接数
  22. max-wait-millis: 200 # 等待连接获取的最大超时时间
  23. tomcat:
  24. max-wait: 30000 # 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒
  25. mybatis:
  26. config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
  27. type-aliases-package: com.djun.demo.model # 所有Entity别名类所在包
  28. mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件
  29. eureka:
  30. client:
  31. service-url:
  32. defaultZone: http://eureka7001.com:7001/eureka/
  33. instance:
  34. instance-id: dept-8001.com
  35. prefer-ip-address: true
  36. # info是当前微服务的信息
  37. info:
  38. app.name: dept-8001
  39. company.name: com.djun.demo
  40. build.artifactId: $project.artifactId$
  41. build.version: $project.version$
  42. ---
  43. server:
  44. port: 8001
  45. spring:
  46. profiles: test
  47. application:
  48. name: dept-hystrix-provider-8001
  49. datasource:
  50. driver-class-name: com.mysql.jdbc.Driver # mysql驱动包
  51. url: jdbc:mysql://localhost:3306/cloud01 # 数据库名称
  52. username: root
  53. password: root
  54. type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
  55. dbcp2:
  56. min-idle: 5 # 数据库连接池的最小维持连接数
  57. initial-size: 5 # 初始化连接数
  58. max-total: 5 # 最大连接数
  59. max-wait-millis: 200 # 等待连接获取的最大超时时间
  60. tomcat:
  61. max-wait: 30000 # 配置间隔多久才进行一次检测需要关闭的空闲连接,单位是毫秒
  62. mybatis:
  63. config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
  64. type-aliases-package: com.djun.demo.model # 所有Entity别名类所在包
  65. mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件
  66. eureka:
  67. client:
  68. service-url:
  69. defaultZone: http://eureka7001.com:7001/eureka/
  70. instance:
  71. instance-id: dept-8001.com
  72. prefer-ip-address: true
  73. # info是当前微服务的信息
  74. info:
  75. app.name: dept-8001
  76. company.name: com.djun.demo
  77. build.artifactId: $project.artifactId$
  78. build.version: $project.version$

bootstrap.yml

  1. spring:
  2. cloud:
  3. config:
  4. name: provider-dept-8001
  5. profile: dev
  6. label: master
  7. uri: http://localhost:3344

application.yml

  1. spring:
  2. application:
  3. name: provider-8001

4、启动与测试

启动顺序:Config Server(3344)-> Eureka Server(7001) -> Provider(8001)

查看Config:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQxNjQ3OTk5_size_16_color_FFFFFF_t_70 3

查看Eureka:

2019070321100468.png

一切正常后访问provider的接口:

20190703211203516.png

全部免费源码传送,下载里面的压缩包:https://github.com/DJun5/microservice-demo

发表评论

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

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

相关阅读