是什么?
Consul是一套开源的分布式服务发现的配置管理系统,由HashiCorp用go语言开发
提供了微服务系统中的服务治理,配置中心,控制总线等功能。这些功能中的每一个可以根据需要单独使用,也可以一起
使用构建全方位的服务网络,总之Consul提供了一种完整的服务网格解决方案
能干嘛?
去哪下载?
怎么使用 (中文版 需要仔细学的话可以看这里!)
https://www.springcloud.cc/spring-cloud-consul.html
下载完成后解压:
在consul.exe的目录下 cmd
输入命令 :consul agent -dev (以开发者模式启动consul)
访问 http://localhost:8500
我这里就标注下依赖和配置文件,不想粘贴了
① 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(); } }
① 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
订单服务消费者远程调用支付提供者的服务成功!
由于是分布式服务,所以p(分区容错性)是必占的,要么ap,要么cp
AP : eureka
CP: zookeeper 、 consul