当前位置:网站首页>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

边栏推荐
- Dependency injection method
- MCU如何选型?
- JS学习笔记14-15:JS数组及数组字母量
- Understanding of fast and slow pointer
- 真实案例:系统上线后Cpu使用率飙升如何排查?
- Redis publishing and subscription
- Viewing the technology stack of distributed system from the crash report of station B
- openpyxl跨工作簿复制sheet页
- Seaport 以及 ERC-4907 能否成为释放NFT流动性的新途径?| Tokenview
- 65、Restful规范
猜你喜欢

依赖注入方式

5g at that time, where will driverless driving go in the future?

Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi

Eureka Basics

Demo collection injection

60. Initial knowledge of wsgiref handwritten web framework +jinja2 module

Redis cache avalanche, penetration, breakdown

mySQL 2502 2503错误

微服务与微服务架构

黑马程序员-软件测试-16阶段3-功能测试-175-198,URL组成介绍,请求内容以及组成说明行功能测试与数据库,url组成扩展说明,客户端与服务器请求与响应,-Fiddler按照以及功能检查确认,
随机推荐
Real case: how to check the soaring usage of CPU after the system goes online?
【Kernel】驱动开发学习之字符设备
812. Maximum triangle area
How does the V8 engine recycle garbage memory?
Unity: window size adaptation when running on the browser after webgl Publishing
Redis data persistence
JS学习笔记06-08:数组的遍历以及数组的四个方法
通过ip获取归属地
Not so large number of combinations
[kernel] character device that drives development and learning
46、IO模型
Eureka基础知识
openpyxl跨工作簿复制sheet页
No module named 'yaml' solution
總結的太好了!終於有人把SQL的各種連接Join都講明白了
Obtain the home location through IP
Redis6新数据类型——HyperLogLog
行为型模式之策略模式
如何将读取列表中的str转化为float
Wvppro-zlm-gb21818-camera