Loading

七、consul服务注册与发现

clipboard


1、Consul简介

是什么?

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

提供了微服务系统中的服务治理,配置中心,控制总线等功能。这些功能中的每一个可以根据需要单独使用,也可以一起

使用构建全方位的服务网络,总之Consul提供了一种完整的服务网格解决方案

 

能干嘛?

clipboard

 

去哪下载?

clipboard

怎么使用 (中文版 需要仔细学的话可以看这里!)

https://www.springcloud.cc/spring-cloud-consul.html

 

2、运行并安装 Consul

下载完成后解压:

clipboard

 

在consul.exe的目录下 cmd

clipboard

 

输入命令 :consul agent -dev (以开发者模式启动consul)

clipboard

 

访问 http://localhost:8500

clipboard

 

3、服务提供者注册进consul

我这里就标注下依赖和配置文件,不想粘贴了

① pom文件

<dependencies>
    <!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <!--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>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>RELEASE</version>
        <scope>test</scope>
    </dependency>
</dependencies>

 

②、yml文件

server:
  port: 8006
spring:
  application:
    name: consul-provider-payment
  #consul服务注册中心地址
  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);
    }
}

 

④、业务类

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

    @RequestMapping(value = "/payment/consul")
    public String paymentzk(){
        return "springcloud with consul:"+ serverPort+ " "+ UUID.randomUUID().toString();
    }
}

 

4、服务消费者注册进consul

① pom文件

<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>

 

②、配置文件

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

 

③ 主启动类

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

 

④、调用consul-payment8006服务

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

    @Autowired
    private RestTemplate restTemplate;


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

 

查看:发现服务消费者 和 提供者确实注册进了consul

clipboard

订单服务消费者远程调用支付提供者的服务成功!

clipboard

 

5、三个注册中心的异同点

由于是分布式服务,所以p(分区容错性)是必占的,要么ap,要么cp

clipboard

AP : eureka

CP: zookeeper 、 consul

posted @ 2021-05-30 10:19  青岑  阅读(186)  评论(0编辑  收藏  举报