1,什么是consul(康搜)
提供了微服务系统中的服务治理、配置中心、控制总线等功能。这些功能中的每—个都可以根据需要单独使用,也可以一起使用以构建全方位的服务网格,总之 Consu提供了一种完整的服务网格解决方案。
它具有很多优点。包括:基于raft协议,比较简洁;支持健康检查,同时攴持HTP和DNs协议攴持跨数据中心的wAN集群提供图形界面跨平台,支持Lnux、Mac、 Windows.
2,下载和安装
https://www.consul.io/downloads
下载好之后解压只有一个文件
打开cmd 运行consul --version 查看consul版本号
启动consul:consul agent -dev
然后访问http://localhost:8500
3,微服务的提供者注册进去
新建一个model支付服务
导入依赖pom
<dependencies>
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>cloud-api-common</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>
编写yml
server:
# consul服务端口
port: 8006
spring:
application:
name: cloud-provider-payment
cloud:
consul:
# consul注册中心地址
host: localhost
port: 8500
discovery:
hostname: 127.0.0.1
service-name: ${spring.application.name}
编写主启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
*
* @author zzyy
* @create 2020/2/17 21:13
**/
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class, args);
}
}
编写业务类controller
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* @author zzyy
* @create 2020-01-30 16:55
**/
@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();
}
}
启动启动类之后可以看到
4,微服务的消费者注册进去
创建model
pom
<dependencies>
<!--SpringCloud consul-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.duan.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>
<!--热部署-->
<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: 80
spring:
application:
name: cloud-consumer-order 服务消费者
cloud:
consul:
# consul注册中心地址
host: localhost
port: 8500
discovery:
hostname: 127.0.0.1
service-name: ${spring.application.name}
主启动类
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class, args);
}
}
配置调用服务接口的发送http请求的RestTemplate的bean
@Configuration
public class ApplicationContextConfig {
@Bean
/**
* 负载均衡的注解
* 发送http请求
*/
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
controller
@RestController
@Slf4j
public class OrderConsulController {
//注册中心的服务名字
public static final String url="http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@RequestMapping("/consumer/payment/consul")
public String paymentConsul(){
//调用服务的接口
String result = restTemplate.getForObject(url+"/payment/consul", String.class);
return result;
}
}
测试
输入http://localhost/consumer/payment/consul
三个注册中心异同点
CAP分区容错性要保证,所以要么是CP,要么是AP
C:强一致性
A:可用性
P:容错性
AP:eurake
CP:zookeeper/consul