SpringCloud微服务 Apollo分布式配置中心客户端获取配置(一)

灰太狼 2022-03-01 04:18 559阅读 0赞

前言

本小结之前我们学习了如何定制开发与部署属于我们自己的分布式配置中心,以后的章节我们将陆续学习一下如何客户端获取配置中心中的配置信息。

案例

  • 在apollo-portal的apollo-env-properties中做如下设置:

    1. local.meta=http://localhost:8080
    2. dev.meta=http://localhost:8080
    3. fat.meta=http://localhost:8080
    4. uat.meta=http://localhost:8080
    5. lpt.meta=${lpt_meta}
    6. pro.meta=${pro_meta}

    如上配置说明在本地开启的配置中心只支持dev、fat、uat。至于如何自定义env,请自行参考网上学习文档,本人不推荐使用DEV/FAT/UAT/PRO之外的环境定义。

  • 本地部署好Apollo配置中心,如果好不知道怎么本地部署的请参考前文。这里不做赘述。

    在这里插入图片描述

  • 配置好DEV配置属性

    1. timeout = 1500
    2. server.port = 8087
    3. spring.application.name = microservice-deal-cloud
    4. eureka.client.serviceUrl.defaultZone = http://localhost:8080/eureka/
    5. eureka.instance.hostname = Swift
    6. eureka.instance.prefer-ip-address = true
    7. eureka.instance.metadata-map.zone = Asia
    8. eureka.instance.metadata-map.customizedMetadata = eurekaCustomizedMetadata
    9. eureka.instance.metadata-map.instanceId = ${spring.application.name}:${server.port}
    10. feign.hystrix.enabled = true
  • 新建项目microservice-deal-apollo-client

    • 项目结构
      在这里插入图片描述
    • Core Code

      • application.properties

        1. #与Apollo配置中心的APPID一致
        2. app.id=Dustyone163
        3. #设置Client的meta即配置中心地址
        4. apollo.meta=http://127.0.0.1:8080
      • MicroserviceDealApolloClientApplication

        1. @SpringBootApplication
        2. @EnableDiscoveryClient
        3. @EnableApolloConfig
        4. public class MicroserviceDealApolloClientApplication {
        5. public static void main(String[] args) throws Exception {
        6. SpringApplication.run(MicroserviceDealApolloClientApplication.class, args);
        7. }
        8. }

        启动类上添加:@EnableApolloConfig

      • pom.xml

        1. <?xml version="1.0" encoding="UTF-8"?>
        2. <project xmlns="http://maven.apache.org/POM/4.0.0"
        3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        5. <modelVersion>4.0.0</modelVersion>
        6. <artifactId>microservice-deal-apollo-client</artifactId>
        7. <packaging>jar</packaging>
        8. <name>microservice-deal-apollo-client</name>
        9. <description>Demo project for Spring Boot</description>
        10. <parent>
        11. <groupId>com.example</groupId>
        12. <artifactId>microservice-deal-parent</artifactId>
        13. <version>0.0.1-SNAPSHOT</version>
        14. </parent>
        15. <dependencies>
        16. <dependency>
        17. <groupId>com.ctrip.framework.apollo</groupId>
        18. <artifactId>apollo-client</artifactId>
        19. <version>1.3.0</version>
        20. </dependency>
        21. <dependency>
        22. <groupId>com.ctrip.framework.apollo</groupId>
        23. <artifactId>apollo-core</artifactId>
        24. <version>1.3.0</version>
        25. </dependency>
        26. </dependencies>
        27. </project>
      • DealController

        1. @RestController
        2. public class DealController {
        3. @Value("${server.port}")
        4. private String port; // 注入普通字符串
        5. @GetMapping("/deal/{id}")
        6. public Deal findById(@PathVariable Integer id) throws InterruptedException {
        7. //Thread.sleep(3000);
        8. Deal deal = new Deal(id, "Dustyone", "Heyt", 22, 18,port);
        9. return deal;
        10. }
        11. @GetMapping("/dealMock/{id}")
        12. public Deal findByIdMock(@PathVariable Integer id) {
        13. Deal deal = new Deal(id, "DustyoneMock", "Mock", 92, 18,port);
        14. return deal;
        15. }
        16. @GetMapping("/deal/withoutParams")
        17. public Deal findByIdWithoutParams() {
        18. Deal deal = new Deal(0, "DustyoneMock", "MockWithouParams", 92, 18,port);
        19. return deal;
        20. }
        21. /*@GetMapping("/{id}")
        22. public Deal findById(@PathVariable Integer id) {
        23. Deal deal = new Deal(id, "Dustyone", "Heyt", 22, 18,port);
        24. return deal;
        25. }*/
        26. }

        @Value(“${server.port}“)中的server.port与Apollo中设置的server.port一致,最好在通过使用@Value获取server.port信息时给server.port一个默认值。以增强代码健壮性。

  • 运行启动类:MicroserviceDealApolloClientApplication

    • 本地运行http://localhost:8080/ Apollo Client注册到Eureka中。
      在这里插入图片描述
    • 本地运行http://localhost:8087/deal/1
      在这里插入图片描述

小结

  • 你可以在apollo-portal的apollo-env-properties指定你的meta_url,然后在Client端指定你使用的meta_url。
  • 本案例演示的是除Apollo之外的所有配置信息都从Apollo中获取,从而最大程度利用Apollo的功能。

发表评论

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

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

相关阅读