This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

What is a Consul

Consul is a multi-data center distributed high availability tool for service discovery and configuring sharing.

Consul contains multiple components, but as a whole, provides tools for service discovery and service configuration for your infrastructure. It provides the following key features:

Service discovery Consul clients can provide a service, such as API or mysql, while others can use Consul to discover a specific service provider. It is easy to find the services it depends on through DNS or HTTP applications.

Health Check Consul clients can provide any number of health checks, specifying a service (for example, whether the WebServer returns a 200 OK status code) or using a local node (for example, whether memory usage is greater than 90%). This information can be used by the operator to monitor the health of the cluster. Used by service discovery components to avoid sending traffic to unhealthy hosts.

Key/Value store Applications can use Consul’s level of Key/Value store based on their own needs. Such as dynamic configuration, feature marking, coordination, leader election, and so on, the simple HTTP API makes it easier to use.

Multi-data Center: Consul supports out-of-the-box multi-data centers. This means that users don’t need to worry about building additional layers of abstraction to scale their business across multiple regions.

Install the Consul

Download the Consul

Go to www.consul.io/downloads

Select your own system to download, here is the Windows version 1.7.1 demo

After downloading, unzip and get a consul.exe

run

Run the consul agent-dev command in CMD

If you see Consul Agent Running! The command is successfully executed

Then we go to the address: http://127.0.0.1:8500/

The registration service

Create a project

First add dependencies to the parent project

<spring-cloud-consul-discovery.version>2.2.0. RELEASE</spring-cloud-consul-discovery.version>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-consul-discovery</artifactId>
                <version>${spring-cloud-consul-discovery.version}</version>
            </dependency>
        </dependencies>
</dependencyManagement>
Copy the code

Create a sub-project: Spring-cloud-consul-discovery-provider

pom

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <! -- Consul Registry -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>

    <! -- Health check -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Copy the code

application.yml

server:
  port: 4444
spring:
  application:
    name: spring-cloud-consul-discovery-provider
  cloud:
    consul:
      host: 127.0. 01. The address of # Consul registry
      port: 8500      # port

management:
  endpoints:
    web:
      exposure:
        include: The '*'
Copy the code

ConsulDiscoveryProviderApplication.java

package io.mvvm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ConsulDiscoveryProviderApplication {

    public static void main(String[] args) { SpringApplication.run(ConsulDiscoveryProviderApplication.class,args); }}Copy the code

DemoController.java

package io.mvvm.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @GetMapping("/get/{str}")
    public String get(@PathVariable("str") String str){
        System.out.println("provider");
        System.out.println("-- -- -- -- -- -" + str + "-- -- -- -- -- -- --");
        returnstr; }}Copy the code

test

After the service is started, visit the Consul registry panel to check whether the registration is successful

(Ignoring other services, there are only two services to do this step)

Consumer services

Create a project

Create a sub-project: Spring-Cloud-consul-Discovery-Consumer

pom

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <! -- Consul Registry -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>

    <! -- Health check -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <! -- openfeign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>
</dependencies>
Copy the code

application.yml

server:
  port: 4433
spring:
  application:
    name: spring-cloud-consul-discovery-consumer
  cloud:
    consul:
      host: 127.0. 01. The address of # Consul registry
      port: 8500      # port

management:
  endpoints:
    web:
      exposure:
        include: The '*'
Copy the code

ConsulDiscoveryConsumerApplication.java

package io.mvvm;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "io.mvvm.client")    // Scan the openFeign package
public class ConsulDiscoveryConsumerApplication {

    public static void main(String[] args) { SpringApplication.run(ConsulDiscoveryConsumerApplication.class,args); }}Copy the code

DemoClient.java

package io.mvvm.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("spring-cloud-consul-discovery-provider")
public interface DemoClient {

    @GetMapping("/get/{str}")
    String get(@PathVariable("str") String str);

}
Copy the code

DemoController.java

package io.mvvm.controller;

import io.mvvm.client.DemoClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class DemoController {

    @Resource
    private DemoClient demoClient;

    @GetMapping("/consumer/{str}")
    public String get(@PathVariable("str") String str){
        System.out.println("consumer");
        String client = demoClient.get(str);
        System.out.println("-- -- -- -- -- -" + client + "-- -- -- -- -- -- --");
        returnclient; }}Copy the code

test

Go to http://localhost:4433/getc/123

Check whether the Controller of the Provider service is invoked