B站尚硅谷最新版SpringCloud 学习笔记(4)-Consul服务注册与发现

B站尚硅谷最新版SpringCloud 学习笔记(4)-Consul服务注册与发现

上一篇:B站尚硅谷最新版SpringCloud 学习笔记(3)-Zookeeper服务注册与发现

B站视频地址:https://www.bilibili.com/video/BV18E411x7eT

一、Consul简介

1.是什么

https://www.consul.io/intro/index.html

image-20210116170913081

image-20210116170958170

2.能干嘛

image-20210116171228859

服务发现提供HTTP和DNS两种发现方式
健康监测支持多种协议,HTTP、TCP、Docker、Shell脚本定制化
KV存储key , Value的存储方式
多数据中心Consul支持多数据中心
可视化Web界面

3.哪里下载和怎么操作

https://www.consul.io/downloads.html

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

二、Centos7安装并运行Consul

1.下载Consul包

进入官网https://www.consul.io/downloads.html

复制下载链接到迅雷

image-20210116172334084

迅雷下载会快一点点(再下载的时间里,进行下面操作)

image-20210116172856604

2.Centos7安装Consul

参考文章:https://www.cnblogs.com/yangk1996/p/12656667.html

①创建一个consul的文件夹,并修改权限

cd /usr/local

mkdir consul

chmod 777 consul

cd consul

image-20210116172955728

②确认安装了

image-20210116173100141

③将下载好的consul传输

image-20210116173405754

④解压consul

unzip consul_1.9.1_linux_amd64.zip

验证consul是否安装完成

image-20210116174350665

⑤启动consul

./consul agent -dev -ui -node=consul-dev -client=192.168.88.77

image-20210116174631124

关闭防火墙 systemctl stopfirewalld

image-20210116174741169

⑥访问http://192.168.88.77:8500

image-20210116174906309

3.服务提供者

①创建cloud-providerconsul-payment8006

image-20210116181005025

②修改pom,添加application.yml,添加启动类

pom

image-20210116181041713

<?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>mycloud2020</artifactId>
        <groupId>org.yezhinao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-providerconsul-payment8006</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.yezhinao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



    </dependencies>

</project>

application.yml

image-20210116181109372

server:
  port: 8006


spring:
  application:
    name: consul-provider-payment
  cloud:
    consul:
      host: http://192.168.88.77  #家里局域网
#      host: http://192.168.75.215  #公司局域网
      port: 8500
      discovery:
        service-name: ${spring.application.name}

启动类

image-20210116181143438

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

③创建接口

image-20210116181208272

@RestController
@Slf4j
public class PaymentController {

    @Value("${server.port}")
    private String serverPort;

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

④验证测试

image-20210116180930799

我遇到的错误

image-20210116180848970

4.服务消费者

①新建cloud-consumerconsul-order80

image-20210116182514853

②修改pom,添加application.yml,添加启动类

pom

<?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>mycloud2020</artifactId>
        <groupId>org.yezhinao</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumerconsul-order80</artifactId>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-consul-discovery -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.yezhinao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>



    </dependencies>


</project>

application.yml

server:
  port: 80


spring:
  application:
    name: consul-consumer-order
  cloud:
    consul:
      host: http://192.168.88.77  #家里局域网
      #      host: http://192.168.75.215  #公司局域网
      port: 8500
      discovery:
        service-name: ${spring.application.name}

启动类

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

③注入RestTemplate 和 创建接口

image-20210116182804382

@Configuration
public class ApplicationContextConfig {

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

}

image-20210116182842664

@RestController
@Slf4j
public class OrderConsulController {

    public static final String INVOME_URL = "http://consul-provider-payment";

    @Resource
    private RestTemplate restTemplate;

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


}

④测试验证

image-20210116182926361

输入http://localhost/consumer/payment/consul

image-20210116182934825

三、三个注册中心异同点

1.CAP

image-20210116183902412

  • C:Consistency(强一致性)

  • A:Availability(可用性)

  • P:Partition tolerance(分区容错)

  • CAP理论关注粒度是数据,而不是整体系统设计的策略

image-20210116183943969

image-20210116184002142

2.AP和CP

AP(Eureka)

image-20210116184044200

image-20210116184037319

CP(Zookeeper/Consul)

image-20210116184104195

image-20210116184124881

代码:https://gitee.com/yezhinao/SpringCloud-Learn/tree/master/mycloud2020(4)

下一篇:B站尚硅谷最新版SpringCloud 学习笔记(5)-Ribbon负载均衡和OpenFeign服务接口调用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值