• 其他服务注册与发现


    一、Zookeeper 服务注册与发现

    1.1 Zookeeper 安装

    直接去官网下载:
    Zookeeper-3.7.0

    然后解压:

    //解压
    tar -zxvf zookeeper-3.7.0-bin.tar.gz 
    //移动到应用目录
    sudo mv apache-zookeeper-3.7.0-bin /usr/local/ 
    // 更改默认配置文件名称
    cd /usr/local/apache-zookeeper-3.7.0-bin/conf
    mv zoo_sample.cfg zoo.cfg 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    启动 Zookeeper Server:

    cd /usr/local/apache-zookeeper-3.7.0-bin/bin
    ./zkServer.sh start
    
    • 1
    • 2

    连接客户端:

    ./zkCli.sh
    
    • 1

    停止 Server:

    ./zkServer stop
    
    • 1

    1.2 注册中心 Zookeeper

    Zookeeper 是一个分布式协调工具,可以实现注册中心功能。在安装好 Zookeeper 后,执行 ./zkServer start,将注册中心启动。

    1.3 搭建服务提供者 cloud-provider-payment8004

    1.3.1 新建 cloud-provider-payment8004 子模块

    1.3.2 修改 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <parent>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud2020</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
        <artifactId>cloud-provider-payment8004</artifactId>
        <dependencies>
            <!-- SpringBoot 整合 Web 组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity -->
                <groupId>com.atguigu.springcloud</groupId>
                <artifactId>cloud-api-commons</artifactId>
                <version>${project.version}</version>
            </dependency>  <!-- SpringBoot 整合 zookeeper 客户端 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    1.3.3 配置 application.yml

    # 8004 表示注册到 Zookeeper 服务器的支付服务提供者端口号
    server:
      port: 8004
    
    # 服务别名——Zookeeper注册到注册中心的名称
    spring:
      application:
        name: cloud-provider-payment
      cloud:
        zookeeper:
          connect-string: localhost:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.3.4 配置主启动类

    @SpringBootApplication
    @EnableDiscoveryClient
    public class
    PaymentMain8004 {
    
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain8004.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    @EnableDiscoveryClient:使用 Consul 或者 Zookeeper作为注册中心时,可使用该注解向注册中心注册服务

    1.3.5 controller

    @RestController
    public class PaymentController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @RequestMapping("/payment/zk")
        public String payment(){
            return "springcloud with zookeeper: " + serverPort + "\t" + UUID.randomUUID().toString();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    1.3.6 测试

    如果安装的 Zookeeper 版本过低,在启动时可能会如下错误:
    在这里插入图片描述
    这是由于 Zookeeper 的 jar 包版本冲突导致的,如下:
    在这里插入图片描述
    解决方案:
    修改 pom.xml 文件,将自带的 zookeeper3.5.3 排除,重新引入符合自己安装的 Zookeeper 的版本 jar

     <!-- SpringBoot 整合 zookeeper 客户端  -->
    <dependency> 
         <groupId>org.springframework.cloud</groupId> 
         <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId> 
         <!-- 先排除自带的 zookeeper3.5.3-->
         <exclusions> 
             <exclusion> 
                 <groupId>org.apache.zookeeper</groupId> 
                 <artifactId>zookeeper</artifactId> 
             </exclusion> 
         </exclusions> 
     </dependency> 
     <!-- 添加 zookeeper3.4.9 版本 -->
     <dependency> 
         <groupId> org.apache.zookeeper </groupId> 
         <artifactId>zookeeper</artifactId> 
         <version>3.4.9</version> 
     </dependency> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    我自己安装的 Zookeeper 版本为 3.7.0,没有遇到这个问题,建议还是使用高版本。

    启动后,在浏览器中访问 http://localhost:8004/payment/zk
    在这里插入图片描述
    登录 Zookeeper 客户端,执行以下命令:
    在这里插入图片描述
    最终可以获得一串 Json 字符串:

    {
      "name": "cloud-provider-payment",
      "id": "bba4b3ac-4175-4b49-b47c-c1c8e989be3c",
      "address": "IP地址",
      "port": 8004,
      "sslPort": null,
      "payload": {
        "@class": "org.springframework.cloud.zookeeper.discovery.ZookeeperInstance",
        "id": "application-1",
        "name": "cloud-provider-payment",
        "metadata": {
          
        }
      },
      "registrationTimeUTC": 1656235221350,
      "serviceType": "DYNAMIC",
      "uriSpec": {
        "parts": [
          {
            "value": "scheme",
            "variable": true
          },
          {
            "value": "://",
            "variable": false
          },
          {
            "value": "address",
            "variable": true
          },
          {
            "value": ":",
            "variable": false
          },
          {
            "value": "port",
            "variable": true
          }
        ]
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    思考: Zookeeper 中的节点是临时节点还是持久节点?
    临时节点。将 8004 服务关闭后,可以看到 Zookeeper 会将创建的节点删除。
    在这里插入图片描述

    1.4 服务消费者 cloud-consumerzk-order80

    1.4.1 创建 cloud-consumerzk-order80 子模块

    1.4.2 修改 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
    
      <parent>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud2020</artifactId>
        <version>1.0-SNAPSHOT</version>
      </parent>
    
      <artifactId>cloud-consumerzk-order80</artifactId>
      <dependencies>
        <!-- SpringBoot 整合 Web 组件 -->
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
          <!-- 引入自己定义的 api 通用包,可以使用 Payment 支付 Entity -->
          <groupId>com.atguigu.springcloud</groupId>
          <artifactId>cloud-api-commons</artifactId>
          <version>${project.version}</version>
        </dependency>  <!-- SpringBoot 整合 zookeeper 客户端 -->
        <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
          <scope>runtime</scope>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <optional>true</optional>
        </dependency>
        <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-test</artifactId>
          <scope>test</scope>
        </dependency>
      </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    1.4.3 配置 application.yml

    server:
      port: 80
    
    spring:
      application:
        name: cloud-consumer-order
      cloud:
        # 注册到 Zookeeper 的地址
        zookeeper:
          connect-string: localhost:2181
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.4.4 配置主启动类

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

    1.4.5 创建 ApplicationContextConfig,配置 RestTemplate

    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    1.4.6 controller

    @RestController
    @Slf4j
    public class OrderZKController {
        public static final String INVOKE_URL = "http://cloud-provider-payment";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/zk")
        public String paymentInfo(){
            return restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    1.4.7 测试

    先启动 8004 服务提供者,再启动 80 服务消费者,然后进入 Zookeeper 客户端,查看节点创建情况:
    在这里插入图片描述
    浏览器访问 http://localhost/consumer/payment/zk,结果如下:
    在这里插入图片描述

    二、Consul 服务注册与发现

    2.1 Consul 概述

    Consul 是 HashiCorp 公司推出的开源工具,用于实现分布式系统的服务发现与配置。与其他分布式服务注册与发现的方案相比,Consul的方案更“一站式”,内置了服务注册与发现框架、分布式一致性协议实现、健康检查、Key/Value 存储、多数据中心方案,不再需要依赖其他工具(比如 Zookeeper 等)。使用起来也比较简单。Consul 使用 Go 语言编写,因此具有天然可移植性(支持 Linux、Windows 和 Mac OS X);安装包仅包含一个可执行文件,方便部署,与 Docker 等轻量级容器可无缝配合。

    Consul 的优势:

    • 使用 Raft 算法来保持一致性,比复杂的 Paxos 算法更直接。相比较而言,Zookeeper 才用的是 Paxos,而 ETCD(另一种注册中心)使用的则是 Raft。
    • 支持多数据中心,内外网的服务采用不同的端口进行监听。多数据中心集群可以避免单数据中心的单点故障,而其部署则需要考虑网络延迟、分片等情况。Zookeeper 和 ETCD 均不提供多数据中心功能的支持。
    • 支持健康检查。ETCD 不提供此功能。
    • 支持 HTTP 和 DNS 协议接口。Zookeeper 的集成较为复杂,ETCD 只支持 HTTP 协议。
    • 官方提供 Web 管理界面,ETCD 无此功能。

    特性:

    • 服务发现
    • 健康检查
    • Key/Value 存储
    • 多数据中心

    2.2 Consul 与 Eureka 的区别

    (1)一致性
    Consul 强一致性(CP)

    • 服务注册相比于Eureka 会稍慢一些。因为 Consul 的 Raft 协议要求必须过半数的节点都写入成功才认为注册成功。
    • Leader 挂掉时,重新选举期间整个 Consul 不可用。保证了强一致性但牺牲了可用性。

    Eureka 保证高可用和最终一致性(AP)

    • 服务注册相对要快,因为不需要等注册信息 replicate(复制) 到其他节点,也不保证注册信息是否 replicate 成功
    • 当数据出现不一致时,虽然 A、B 上的注册信息不完全相同,但每个 Eureka 节点依然能够正常对外提供服务,这会出现查询服务信息时如果请求 A 查询不到,但请求 B 就能查询到。如此保证了可用性但牺牲了一致性。

    (2)开发语言和使用
    Eureka 就是个 servlet 程序,跑在 servlet 容器中。
    Consul 则是 Go 语言编写而言,安装启动即可。

    2.3 Consul 的下载与安装

    这里我使用 Docker 安装的 Consul,安装步骤如下:

    1、默认拉取最新版本的 Consul

    docker pull consul
    
    • 1

    2、检查 Consul 是否拉取成功

    docker images
    
    • 1

    3、启动 Consul

    docker run -p 8500:8500/tcp consul agent -server -ui -bootstrap-expect=1 -client=0.0.0.0
    
    • 1


    启动成功后访问:http://IP:8500,可以看到 Consul 的管理界面

    在这里插入图片描述

    2.4 Consul 的基本使用

    Consul 支持健康检查,并提供了 HTTP 和 DNS 调用的 API 接口完成服务注册、服务发现,以及 K/V 存储这些功能。接下来通过发送 HTTP 请求的形式来了解一下 Consul。

    2.4.1 服务注册与发现

    (1)注册服务
    通过 postman 发送 put 请求到 http://localhost:8500/v1/catalog/register 可以完成服务注册

    {
        "Datacenter": "dc1",
        "Node": "node01",
        "Address": "192.168.74.102",
        "Service": {
            "ID": "mysql-01",
            "Service": "mysql",
            "tags": ["master", "v1"],
            "Address": "192.168.74.102",
            "Port": 3306
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (2)服务查询
    通过 postman 发送 get 请求到 http://localhost:8500/v1/catalog/services 查看所有的服务列表
    在这里插入图片描述
    通过 postman 发送 get 请求到 http://localhost:8500/v1/catalog/service/服务名 查看具体的服务详情
    在这里插入图片描述
    (3)服务删除
    通过 postman 发送 delete 请求到 http://localhost:8500/v1/catalog/deregister 删除服务

    {
        "Datacenter": "dc1",
        "Node": "mysql",
        "ServiceID": "mysql-01"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    2.4.2 Consule 的 K/V 存储

    可以参照 Consul 提供的 K/V 存储的 API 完成基于 Consul 的数据存储

    含义请求路径请求方式
    查看 Keyv1/kv/:keyGET
    保存或更新v1/kv/:keyPUT
    删除v1/kv/:keyDELETE
    • key 值中可以带/,可以看做是不同的目录结构
    • value 的值经过了 base64_encode,获取到数据后 base64_decode 才能获取到原始值。数据不能大于 512kb。
    • 不同数据中心的 K/V 存储系统是独立的,使用 dc=? 参数指定

    2.5 基于 Consul 的服务注册与发现

    2.5.1 服务提供者

    2.5.1.1 新建 consul-provide-payment8006 子模块

    2.5.1.2 修改 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>consul-provide-payment8006</artifactId>
    
        <dependencies>
             <!--SpringCloud consul-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
            <!-- SpringBoot 整合 Web 组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-actuator</artifactId>
             </dependency>
            <!-- 日常通用 jar 包配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2.5.1.3 配置 application.yml

    server:
      port: 8006
    
    spring:
      application:
        name: consul-provider-payment
      # consul 注册中心地址
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.5.1.4 配置主启动类

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

    2.5.1.5 controller

    @RestController
    @Slf4j
    public class PaymentController {
    
        @Value("${server.port}")
        private String serverPort;
    
        @RequestMapping("/payment/consul")
        public String payment(){
            return "springcloud with consul: " + serverPort + "\t" + UUID.randomUUID().toString();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.5.1.6 测试

    服务启动后,访问 http://localhost:8005,可以看到服务已经注册进 Consul
    在这里插入图片描述
    访问 http://localhost:8006/payment/consul
    在这里插入图片描述

    2.5.2 服务消费者

    2.5.2.1 新建 consul-consumer-order80 子模块

    2.5.2.2 修改 pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <parent>
            <artifactId>cloud2020</artifactId>
            <groupId>com.atguigu.springcloud</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>consul-consumer-order80</artifactId>
    
        <dependencies>
            <!--SpringCloud consul-server -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            </dependency>
            <!-- SpringBoot 整合 Web 组件 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <!-- 日常通用 jar 包配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    2.5.2.3 配置 application.yml

    server:
      port: 80
    
    spring:
      application:
        name: consul-consumer-order
      # consul 注册中心地址
      cloud:
        consul:
          host: localhost
          port: 8500
          discovery:
            service-name: ${spring.application.name}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    2.5.2.4 配置主启动类

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

    2.5.2.5 配置 RestTemplate

    @Configuration
    public class ApplicationContextConfig {
    
        @Bean
        @LoadBalanced
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    2.5.2.6 controller

    @RestController
    @Slf4j
    public class OrderController {
    
        private static final String INVOKE_URL = "http://consul-provider-payment";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/consumer/payment/consul")
        public String paymentInfo(){
            return restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    2.5.2.7 测试

    启动 80 服务:
    在这里插入图片描述
    访问 http://localhost/consumer/payment/consul
    在这里插入图片描述

  • 相关阅读:
    数组复制之System.arraycopy
    软件测试面试简历,三年测试项目经验怎么写?
    13.TCP-bite
    Nanoprobes原位杂交:基因检测的演变(从 Nanogold到EnzMet)
    Mybatis面试题
    判断字符串是否为空白字符串,““,“ “,NULL,的方法
    I.MX RT1176笔记(9)-- 程序异常追踪(CmBacktrace 和 segger rtt)
    Codeforces Round #832 (Div. 2) C. Swap Game
    杂记 | Langchain中few-shot提示词模板的使用(给提示词添加示例)
    【MATLAB教程案例50】通过VisualSFM工具箱提取360度等间隔环绕拍摄得到的图像序列点云数据,并进行目标三维重建matlab仿真
  • 原文地址:https://blog.csdn.net/dingd1234/article/details/125468121