关于springcloud:Spring-Cloud-学习三Consul服务注册与发现

35次阅读

共计 3881 个字符,预计需要花费 10 分钟才能阅读完成。

Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。

1、概述

Consul 提供了微服务零碎中的服务治理、配置核心、管制总线等性能。这些性能中的每一个都能够依据须要独自应用,也能够一起应用以构建全方位的服务网络,总之 Consul 提供了一种残缺的服务网络解决方案。它具备很多长处,包含:基于 raft 协定,比拟简洁;反对健康检查,同时反对 HTTP 和 DNS 协定反对跨数据中心的 WAN 集群 提供图形界面,跨平台,反对 linux,mac,windows。

2、装置并运行 consul

https://www.consul.io/downloa…,下载实现后,解压会呈现 consul.exe 文件,在 cmd 下进入 consul 目录

拜访 http://localhost:8500 就能够看见 consul 的界面

3、应用办法

新建 cloud-providerconsul-payment8006 模块

pom.xml

<dependencies>
        <dependency><!-- 援用本人定义的 api 通用包,能够应用 Payment 领取 Entity -->
            <groupId>com.yu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-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>

yml

server:
  port: 8006

spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

主启动

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

controller

@RestController
@Slf4j
public class PaymentController {@Value("${server.port}")
    private String serverPort;

    @RequestMapping(value = "/payment/consul")
    public String paymentConsul(){return "springCloud--consul:"+serverPort+"\t"+ UUID.randomUUID().toString();}
}

新建 cloud-consumerconsul-order80 模块

pom.xml

 <dependencies>
        <dependency><!-- 援用本人定义的 api 通用包,能够应用 Payment 领取 Entity -->
            <groupId>com.yu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <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>
        <!--SpringCloud consul-server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-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>

yml

server:
  port: 8090
spring:
  application:
    name: cloud-consumer-order
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

主启动

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

配置 Bean

@Configuration
public class ApplicationContextConfig {

    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){return new RestTemplate();
    }
}

controller

@RestController
@Slf4j
public class ConsumerController {
    public static final String INVOKE_URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping(value = "/consumer/consul/zk")
    public String paymentInfo(){String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class);
        return result;
    }
}

测试

正文完
 0