1. About Consul
What is consol?
- Consul official website: Official website
- Consul is an open source distributed service discovery and configuration management system developed by HashICorp in Go language. It provides service governance, configuration center, control bus and other functions in the microservice system. Each of these functions can be used alone or together as required. A comprehensive service network has been built. In short, * * consul provides a complete service network solution * *.
- Advantages: Based on raft protocol, it is relatively simple; It supports health check, HTTP and DNS protocols, WAN clusters across data centers, graphical interfaces across platforms, Linux, Mac and Windows.
What can Consu do?
- Service discovery: provides two discovery methods: HTTP and DNS.
- Health detection: support multiple methods, including HTTP, TCP, Docker and Shell script customization
- KV storage: storage method of Key and Value
- Multiple data centers: Consul supports multiple data centers
- Visual Web interface
Consu download address
Download website: Download address
How does Consu play
Reference documents: Reference documents
2. Install and run Consul
1. After decompressing the windows version, you can get consumer exe
cmd in the D:\Environment path and check the version number
Start using development mode
- Use the command * * consumer agent - Dev * * to start consumer
- Through address http://localhost:8500 You can visit the homepage of consult.
3. The service provider registers with Consul
New cloud providerconsumer payment8006 project
Modify pom file
<?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>cloud2020</artifactId> <groupId>com.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-providerconsul-payment8006</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--SpringCloud consul-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- SpringBoot integration Web assembly --> <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> <!--Daily general jar Package configuration--> <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> </project>
Write yml
# Consumer service port number server: port: 8006 spring: application: name: consul-provider-payment # Address of consumer Registration Center cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
Main startup class
@SpringBootApplication @EnableDiscoveryClient public class PaymentMain8006 { public static void main(String[] args) { SpringApplication.run(PaymentMain8006.class, args); } }
Business Controller
@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(); } }
Test and start 8006 project
-
visit http://localhost:8500/ , check the Consul home page
-
Access interface http://localhost:8006/payment/consul , the results will be displayed normally.
4. Service consumers register with Consul
New cloud-consumerconsul-order80 project
Modify pom file
<?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>cloud2020</artifactId> <groupId>com.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-consumerconsul-order80</artifactId> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <!--SpringCloud consul-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency> <!-- SpringBoot integration Web assembly --> <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> <!--Daily general jar Package configuration--> <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> </project>
Write yml
server: port: 80 spring: application: name: cloud-consumer-consul cloud: consul: host: localhost port: 8500 discovery: service-name: ${spring.application.name}
Main startup class
@SpringBootApplication @EnableDiscoveryClient public class OrderConsulMain80 { public static void main(String[] args) { SpringApplication.run(OrderConsulMain80.class, args); } }
Configure Bean (create config directory and add configuration)
@Configuration public class ApplicationContextConfig { @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
Controller business class
@RestController @Slf4j public class OrderConsulController { public static final String INVOKE_URL = "http://consul-provider-payment"; @Resource private RestTemplate restTemplate; @RequestMapping(value = "/consumer/consul") public String payment() { String result = restTemplate.getForObject(INVOKE_URL + "/payment/consul", String.class); return result; } }
Start project test
-
Run consumer to access http://localhost:8500/ , the home page will display two services: cloud provider consumer payment8006 and cloud consumer consumer order80
-
visit http://localhost/consumer/consul , view the results.
5. Similarities and differences of the three registries
Component name | Language CAP | Service health testing | External exposure interface | Spring Cloud integration |
---|---|---|---|---|
Eureka | java | AP | Configurable support | HTTP |
Consul | Go | CP | support | HTTP/DNS |
Zookeeper | java | CP | Support client | Integrated |
Introduction to CAP
- C: Consistency (strong consistency)
- A: Availability
- P: Partition tolerance
- CAP theory focuses on granularity, which is data, not the strategy of overall system design.
Classic CAP graph
-
CAP can only meet two requirements at most.
-
The core of CAP Theory: a distributed system cannot meet the three requirements of consistency, availability and partition fault tolerance at the same time.
-
Therefore, according to the CAP principle, NoSQL database is divided into three categories: meeting the CA principle, meeting the CP principle and meeting the AP principle.
- CA principle: single point cluster, a system that meets consistency and availability, is usually not very powerful in scalability.
- CP principle: systems that meet consistency and partition tolerance usually have low performance.
- AP principle: systems that meet availability and partition tolerance may generally have lower requirements for consistency.
-
AP: Eureka
-
CP: Zookeeper/Consul
AP architecture (high availability)
After the network partition appears, in order to ensure the availability, system B can return the old value to ensure the availability of the system.
- Conclusion: it violates the requirements of consistency C and only meets the requirements of availability and partition fault tolerance, that is, AP
CP architecture
When the network partition appears, in order to ensure the consistency, the request must be rejected, otherwise the consistency cannot be guaranteed.
- Conclusion: it violates the requirements of availability A and only meets consistency and partition fault tolerance, that is, CP
Learning website: Go you