当前位置:网站首页>Consul service registration and discovery
Consul service registration and discovery
2022-07-19 08:27:00 【Illusory clarity】
Stand-alone version
1.Consul brief introduction
Consul It is a set of open source distributed services Current and configuration management system , from HashiCorp Company use Go Language development .
It provides service governance in microservice system . Configure central control bus and other functions . Each of these functions can be used separately as needed , It can also be a Use to form a comprehensive service network , All in all Consul Provides a complete service grid solution .
It has many advantages . Include : be based on raft agreement , Simple comparison ; Support health checks Support at the same time HTTP and DNS The protocol supports cross data center WAN Clusters provide graphical interfaces across platforms , Support Linux、Mac、 Windows.
1.1 What can I do? ?
1.2 Consul Download address
1.3 How do you play? ?
Chinese version of the course
2. Install and run Consul
2.1 Official website installation instructions
Official website installation instructions
mac System installation Consul see
mac install brew and Consul
3. Service providers
3.1 newly build Module Payment services provider8006
cloud-providerconsul-payment8006
3.2 modify pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zhang.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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
3.3 establish application.yml
#####consul Service port number
server:
port: 8006
spring:
application:
name: consul-provider-payment
####consul Address of Registration Center
cloud:
consul:
host: localhost
discovery:
#hostname: 127.0.0.1
service-name: ${spring.application.name}
3.4 Main startup class
@SpringBootApplication
@EnableDiscoveryClient
public class PaymentMain8006 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8006.class,args);
}
}
3.5 controller class
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@RequestMapping("/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
4. Serving consumers
4.1 newly build Module Consumer service order8006
cloud-consumerconsul-order80
4.2 modify pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.zhang.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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
4.3 establish application.yml file
###consul Service port number
server:
port: 80
spring:
application:
name: cloud-consumer-order
###consul Registry Center
cloud:
consul:
host: localhost
port: 8500
discovery:
#hostname:127.0.0.1
service-name: ${spring.application.name}
4.4 Main startup class
@SpringBootApplication
@EnableDiscoveryClient
public class OrderConsulMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderConsulMain80.class,args);
}
}
4.5 To configure Bean
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
4.6 controller
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOKE_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/consul")
public String paymentInfo(){
return restTemplate.getForObject(INVOKE_URL+"/payment/consul",String.class);
}
}
Be careful :
Eureka 、 Zookeeper 、 Consul The similarities and differences between the three registries :

What is the CAP?
At most, two can be satisfied at the same time .
CAP The core of the theory is : A distributed system can't satisfy the consistency well at the same time , Availability and partition fault tolerance are three requirements , therefore , according to CAP The principle will NoSQL The database is divided into satisfying CA principle 、 Satisfy CP Principles and satisfaction AP Three categories of principles :
CA- Single point cluster , Meet consistency , Availability system , Usually not very strong in scalability .
CP Meet consistency , Partition tolerates the necessary system ,. Usually the performance is not very high .
**AP ** Meet availability , Partition tolerant system , Generally, there may be lower requirements for consistency .
AP framework
When network partition appears , To ensure availability , System B You can return the old value , Ensure the availability of the system . Conclusion : Violate one Sexual nature C The requirements of , Only for availability and partition fault tolerance , namely AP
CP framework
When network partition appears , To ensure consistency , You have to reject the request , Otherwise, the consequent conclusion cannot be guaranteed : Violation of availability A The requirements of , Only satisfied - Consistency and partition fault tolerance , namely CP

边栏推荐
- 49、Mysql使用
- How to select MCU?
- Redis的发布和订阅
- Excellent résumé! Enfin quelqu'un a compris toutes les connexions SQL
- unity 自定义天空球模型防止被裁剪
- JS学习笔记01-03——this的引用,全局作用域,方法
- Solution to sudo PIP install gevent installation failure
- sudo pip install gevent 安装失败的解决办法
- Openpyxl copy sheet pages across workbooks
- Redis common data types - hash and ordered set Zset (sorted set)
猜你喜欢
随机推荐
Visual studio 2022 (vs 2022) cannot read memory
Excellent résumé! Enfin quelqu'un a compris toutes les connexions SQL
65、Restful规范
OI回忆录
JS学习笔记06-08:数组的遍历以及数组的四个方法
Redis common data types - redis list and redis set
Not so large number of combinations
Strategic model of behavioral model
Redis introduction
Address monitoring API: how to trace and monitor uniswap hacker addresses
60、wsgiref手写web框架+jinja2模块初识
Eureka自我保护
1. Decision tree
最新一代互联网:WEB 3.0
leetcode:287. 寻找重复数【快慢指针板子】
SPARK闲杂--为什么复用Exchange和subquery
php存储密码
JS学习笔记06-08:数组的遍历以及数组的四个方法
Redis6 new data type - hyperloglog
OpenFeign服务接口调用









