SpringCloud之Consul

学习笔记:

1、官方网址:https://www.consul.io/

https://spring.io/projects/spring-cloud-consul

2、Consul 是一套开源的分布式服务发现和配置管理系统,由 HashiCorp 公司用 Go 语言开发。
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每一个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之Consul提供了一种完整的服务网格解决方案。
它具有很多优点。包括: 基于 raft 协议,比较简洁; 支持健康检查, 同时支持 HTTP 和 DNS 协议 支持跨数据中心的 WAN 集群 提供图形界面 跨平台,支持 Linux、Mac、Windows。

3、下载地址

https://developer.hashicorp.com/consul/install

以windows系统为例:

4、安装运行

查看版本: consul --version

使用开发模式运行

consul agent -dev
访问地址:localhost:8500

5、服务注册与发现

5.1 pom

<!--SpringCloud consul config-->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

5.2 主启动类添加注解

@EnableDiscoveryClient

5.3 cloud-payment-service服务注册,yml配置

server:
  port: 8001
# ==========applicationName + druid-mysql8 driver===================
spring:
  application:
    name: cloud-payment-service

  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud_test_db?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}

# ========================mybatis===================
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.cloud.entities
  configuration:
    map-underscore-to-camel-case: true

5.4  cloud-consumer-order服务注册,yml配置

server:
  port: 80

spring:
  application:
    name: cloud-consumer-order
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
        prefer-ip-address: true # 优先使用服务ip进行注册

6、微服务调用,解决硬编码问题

@RestController
public class OrderController {

//    public static final String paymentUrl = "http://localhost:8001";
    public static final String paymentUrl = "http://cloud-payment-service";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/pay/add")
    public ResultData<Object> addOrder(PayDTO payDTO) {
        return restTemplate.postForObject(paymentUrl + "/pay/add", payDTO, ResultData.class);
    }

    @GetMapping("/consumer/pay/get/{id}")
    public ResultData<Object> getPayInfo(@PathVariable("id") Integer id) {
        return restTemplate.getForObject(paymentUrl + "/pay/get/" + id, ResultData.class, id);
    }
}

测试接口:

GET localhost:80/consumer/pay/get/1

7、CAP

C:Consistency  强一致性

A:Availability     可用性

P:Partition tolerance  分区容错性

CAP理论的核心:一个分布式系统不可能同时很好的满足一致性,可用性和分区容错性三个需求,因此,根据CAP远离将NoSQL数据库分成了满足CA原则、CP原则和AP原则三个大类。

CA:单点集群,满足一致性,可用性的系统,通常在扩展性上不太强大。

CP:满足一致性,分区容错性的系统,通常性能不是特别高。

AP:满足可用性,分区容错性的系统,通常可能对一致性要求低一些。

8、服务配置与刷新

官网:https://docs.spring.io/spring-cloud-consul/reference/config.html

配置参数:https://docs.spring.io/spring-cloud-consul/reference/appendix.html

8.1、pom

        <!--SpringCloud consul config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>

8.2、yml

bootstrap.yml

spring:
  application:
    name: cloud-payment-service
  ####Spring Cloud Consul for Service Discovery
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        service-name: ${spring.application.name}
      config:
        profile-separator: '-'  # default value is ","
        format: YAML
        watch:
          wait-time: 55  #实际开发尽量别改

application.yml

server:
  port: 8001
# ==========applicationName + druid-mysql8 driver===================
spring:
  profiles:
    active: dev #多环境配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud_test_db?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
    username: root
    password: 123456

# ========================mybatis===================
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.cloud.entities
  configuration:
    map-underscore-to-camel-case: true

8.3、consul服务器key/value配置

参考规则:

config/testApp,dev/
config/testApp/
config/application,dev/
config/application/

测试接口

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

    @GetMapping("/get/info")
    public String getInfoByConsul(@Value("${test.info}") String testInfo) {
        return "test-info:" + testInfo + "\t" + "port:" + port;
    }

8.4、配置动态及时刷新

主启动类添加:@RefreshScope

@MapperScan("com.atguigu.cloud.mapper")
@RefreshScope //动态刷新
@EnableDiscoveryClient
@SpringBootApplication
public class Payment8001Application {
    public static void main(String[] args) {
        SpringApplication.run(Payment8001Application.class, args);
    }
}

9、服务配置持久化

9.1、新建文件夹(mydata)及脚本文件(consul_start.bat)

9.2、consul_start.bat脚本信息   D:\developer\consul_1.18.1_windows_amd64替换成你自己的电脑路径

@echo.服务启动......  
@echo off  
@sc create Consul binpath= "D:\developer\consul_1.18.1_windows_amd64\consul.exe agent -server -ui -bind=127.0.0.1 -client=0.0.0.0 -bootstrap-expect  1  -data-dir D:\developer\consul_1.18.1_windows_amd64\mydata   "
@net start Consul
@sc config Consul start= AUTO  
@echo.Consul start is OK......success
@pause

9.3、右键管理员权限打开

添加数据

  • 8
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值