7.1 是什么?
Consul是一种分布式,高度可用且具有数据中心感知能力的解决方案,用于跨动态,分布式基础架构连接和配置应用程序。
7.2 能干什么
领事提供了几个关键功能:
- 多数据中心 -Consul旨在支持数据中心,并且无需复杂的配置即可支持任意数量的区域。
- 服务网格/服务细分 -Consul Connect通过自动TLS加密和基于身份的授权来实现安全的服务间通信。应用程序可以在服务网格配置中使用sidecar代理来为入站和出站连接建立TLS连接,而根本不知道Connect。
- 服务发现 -Consul使服务易于注册自己并通过DNS或HTTP接口发现其他服务。诸如SaaS提供程序之类的外部服务也可以注册。
- 运行状况检查 -运行状况检查使Consul可以快速提醒操作员有关群集中的任何问题。与服务发现的集成可防止将流量路由到不正常的主机,并启用服务级别的断路器。
- 键/值存储 -灵活的键/值存储可存储动态配置,功能标记,协调,领导者选举等。简单的HTTP API使得在任何地方都可以轻松使用。
7.3 怎么玩
- 下载consul.exe
- 运行 consul agent -dev
- 通过一下的地址可以访问 consul首页 http://localhost:8500
7.4 服务提供者 cloud-provider-paymentconsul8004
7.4.1 编写pom.xml
<dependencies>
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.etc.cloud</groupId>
<artifactId>cloud-api-commons</artifactId>
</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>
<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>
7.4.2 编写yml
server:
# consul服务端口
port: 8006
spring:
application:
name: consul-provider-payment
cloud:
consul:
# consul注册中心地址
host: localhost
port: 8500
discovery:
hostname: 127.0.0.1
service-name: ${spring.application.name}
7.4.3 主启动类
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
log.info("*********** PaymentMain8006启动成功 **********");
}
}
7.4.4 业务类
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping(value = "payment/consul")
public String paymentConsul() {
return "SpringCloud with consul:" + serverPort + "\t" + UUID.randomUUID().toString();
}
}
7.5 服务消费者 cloud-consumer-order8000
7.5.1 编写pom.xml
<dependencies>
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<groupId>org.example</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>
<!--热部署-->
<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>
7.5.2 编写yml
server:
port: 8007
spring:
application:
name: cloud-consumer-order
cloud:
consul:
# consul注册中心地址
host: localhost
port: 8500
discovery:
hostname: 127.0.0.1
service-name: ${spring.application.name}
7.5.3 主启动类
package com.etc.springcloud;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author kalista
* @Description
* @Date 2020/8/14 18:25
**/
@Slf4j
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class, args);
log.info("*********** PaymentMain80启动成功 **********");
}
}
7.5.4 业务类
package com.etc.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* @Author kalista
* @Description
* @Date 2020/8/14 18:27
**/
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/consul")
public String paymentInfo()
{
String result = restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
return result;
}
}