DEMO code for the project: https://github.com/heyu52/-spring-cloud
What is Consul?
Official website: https://www.consul.io/
The following is from: https://www.jianshu.com/p/e0986abbfe48
Consul has several components, but in general, it is a tool for service discovery and configuration in the infrastructure. It provides several key functions:
-
Service Discovery
Consul client can provide services, such as api or mysql, or use Consul client to discover the provider of a specified service. With DNS or HTTP, applications can easily find the services they depend on. -
Health examination
Consul client can provide any number of health checks, either associated with a given service ("Web server returns 200 OK") or with a local node ("memory utilization is less than 90%"). This information can be used to monitor the performance of the cluster, and the service discovery component uses this information to remove traffic from the problematic host. -
KV Store
Applications can use Consul's hierarchical key/value storage, including dynamic configuration, function tags, coordination, leader election, and so on. The simple HTTP API makes it easy to use. -
Multiple Data Centers
Consul supports multiple data centers. This means that Consul users don't have to worry about building additional abstraction layers to extend to multiple regions. -
Secure Inter-Service Communication
Bright spot
- HTTP API
- DNS( xxx.service.consul )
- Linkage with Nginx, such as ngx_http_consul_backend_module
Docker Installation
Address: https://hub.docker.com//consul
docker pull consul
Function
docker run --name consul -d -p 8500:8500 -p 8600:8600/udp consul
Enter the Management Interface
Browser input: http://192.168.0.123:8500/ui/dc1/services
This page is more concise.
Create a project
Create goodspo
pom additions
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
To configure
server.port=0 spring.application.name=GoodsPO management.endpoint.health.show-details=always spring.cloud.consul.host=192.168.0.123 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.prefer-ip-address=true
Startup class
package com.csdn.GoodsPO; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class GoodsPoApplication { public static void main(String[] args) { SpringApplication.run(GoodsPoApplication.class, args); } }
At the same time, create goodspr
pom additions
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
To configure
server.port=0 spring.application.name=GoodsPR management.endpoint.health.show-details=always spring.cloud.consul.host=192.168.0.123 spring.cloud.consul.port=8500 spring.cloud.consul.discovery.prefer-ip-address=true
Startup class
package com.csdn.GoodsPR; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class GoodsPrApplication { public static void main(String[] args) { SpringApplication.run(GoodsPrApplication.class, args); } }
Start the project and refresh http://192.168.0.123:8500/ui/dc1/services
Here you can see the service registration. I showed an error because I restarted the project. Let's try GoodsPO
As you can see here, the system has allocated 13464 port number by default. Let's click GoodsPO-0 again.
Here I want to explain that I set the version to 2.1.4.RELEASE,2.1.8 health check is a problem.
Modify goodspo startup class
package com.csdn.GoodsPO; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @EnableDiscoveryClient @SpringBootApplication public class GoodsPoApplication { @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(GoodsPoApplication.class, args); } }
Increase control
package com.csdn.GoodsPO.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController public class MyController { private final RestTemplate restTemplate; @Autowired public MyController(RestTemplate restTemplate) {this.restTemplate = restTemplate;} @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) public String echo(@PathVariable String str) { return restTemplate.getForObject("http://GoodsPR/echo/" + str, String.class); } }
Modify goodspr and add control
package com.csdn.GoodsPR.web; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class EchoController { @RequestMapping(value = "/echo/{string}", method = RequestMethod.GET) public String echo(@PathVariable String string) { return "Hello Nacos Discovery " + string; } }
Restart the project, browser input: http://192.168.0.148:13732/echo/5231
The project call was successful.