Reprinted Disclaimer: The source of the article is to carry sacks of teenager
Contents of this article:
Write at the beginning
Continued from the previous article: Spring Cloud integrates Eureka to implement a service registry . With the closed source of Eureka 2.0 stop. As the preferred service registration and discovery component of Spring Cloud, Eureka's status as a prince is not guaranteed.
In addition to the Zookeeper already introduced, it can be used as the service registry of Spring Cloud Consul 也可以作为 Spring Cloud 的服务注册中心
. Although Eureka, Zookeeper, and Consul can all be used as the service registry of Spring Cloud, but 它们的实现原理都是相通的
.
Next, let's get to know Consul first.
1. Introduction of Consul
This article only uses Consul as a Spring Cloud service registry, and other functions will not be introduced.
Consul official website: https://www.consul.io/intro/index.html . Consul is set by HashiCorp company with development.开源的
分布式服务发现和配置管理系统
Go语言
Consul provides micro-services system 服务治理
, 配置中心
, 控制总线
and other functions. Each of these functions can be used individually or together to build a comprehensive service grid. In short, Consul provides a complete set of service grid solutions.
Download link: https://www.consul.io/downloads.html
Attachment: Spring Cloud Consul Chinese Document Reference Manual: https://www.springcloud.cc/spring-cloud-consul.html
Consul advantages:
- Based on Raft protocol, relatively concise
- Support health check
- Support both HTTP and DNS protocol service discovery methods
- Support KV key-value storage (similar to Redis)
- Support multiple data centers
- Provides a visual web management interface
- Support Linux, Mac, Windows
Consul Service Registry Architecture
2. Preparation
1. Consul installation
Now that you want to use Consul as the service registry of Spring Cloud, you must first install a Consul. Used here Windows 64 bit 版本
, just unzip the downloaded file, there is only one after unzip consul.exe 文件
. Double-click to open it to run.
Through the cmd command line, enter the directory where consul.exe is located, and use the command: consul --version
you can view the version number; through the command: consul agent -dev
use the development mode to start.
The startup process, as shown in the figure : Enter the web management page
through http://localhost:8500 :
So far, the Consul client installation is complete. If you are in a Linux environment, you can also install the Consul Linux client.
2. Realize the construction of service registration center based on Consul
2.1 Operation at the service provider
The module name is defined as:, cloud-providerconsul-payment8006
to act as a service provider.
Ⅰ. Pom.xml introduces dependencies
The introduction of spring-cloud-starter-consul-discovery
dependency.
<!--整合 springcloud-consul 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
Ⅱ. Application.yml configuration file modification
server:
port: 8006
spring :
application : #service name
name : consul - provider - payment
cloud :
consul : #consul address
host : localhost
port : 8500
discovery :
service-name : $ {
spring.application.name }
Ⅲ. Write the controller
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
@GetMapping(value = "/payment/consul")
public String paymentConsul(){
return "springcloud with consul: "+serverPort+"\t"+ UUID.randomUUID().toString();
}
}
Ⅳ. Project start
Start the cloud-providerconsul-payment8006
service providing module, and through the Consul web management interface, we can find that the current service has been successfully registered. As shown below:
2.2. Operation on the service consumer
The module name is defined as:, cloud-consumerconsul-order80
to act as a service consumer.
Ⅰ pom.xml introduces dependencies
With 服务提供模块
the same. Also need to introduce spring-cloud-starter-consul-discovery
dependencies.
<!--整合 springcloud-consul 依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
Ⅱ. Application.yml configuration file modification
server:
port: 80
spring:
application:
name: consul-consumer-order
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${
spring.application.name}
Ⅲ. Use RestTemplate to make service calls
@Configuration
public class ApplicationContextConfig {
@LoadBalanced
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
Ⅳ. Write the controller
@RestController
@Slf4j
public class OrderConsulController {
public static final String INVOME_URL = "http://consul-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/consul")
public String payment (){
String result = restTemplate.getForObject(INVOME_URL+"/payment/consul",String.class);
return result;
}
}
Ⅴ. Project start
Start the cloud-consumerconsul-order80
consumption module, and through the Consul web management interface, we can also find that the current service has been successfully registered. As shown below:
3. Interface call test
The code download address for this article: SpringCloud integrates Consul to implement service registration center (extract code: 2xzq)
Next article: SpringCloud integrates Ribbon to implement service calls (load balancing)