The registry in Spring Cloud’s own architecture is Eureka, and other services are also supported for service registration and discovery. This article describes how to use Consul for service registration and discovery and integrate it into the Spring Cloud project.

The environment version of this project is:

  • The Consul – 1.5.0
  • The Spring Cloud – Edgware SR5
  • Spring the Boot – 1.5.21
  • Operating system → Win10 (PowerShell)

Enabling Consul service

To start Consul, use the following command:

.\consul.exe agent-server-uI-bootstrap-client 0.0.0.0 -data-dir="d:\ consul-data "-bind 127.0.0.1Copy the code

You can also use development mode (simpler, automatically start UI services, but can’t save data state)

  .\consul.exe agent -dev
Copy the code

After the service is successfully started, visit http://localhost:8500. If the following page is displayed, the service is successfully started

Create a Consumer project

Create a Spring Boot project, specify leon-consumer, select Web, Consul Discovery from the dependency drop-down list, and select version 1.5.21

Modify the configuration file application.yml to specify service port, name, and Consul service address

server:
  port: 8081
spring:
  application:
    name: leon-consumer
  cloud:
    consul:
      host: localhost
      port: 8500
Copy the code

Start the Consul web interface and view the Consul web interface:

At this time, you can see that the service has been registered on Consul, but the red X is reminded when you find it. Click enter to view the detailed information:

You can see that the error message is for a health check because Consul relies on the Actuator module for health check. Add the dependencies in pop.xml

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

Copy the code

Then restart the program, refresh the browser and look again

Creating a Provider program

Create a Spring Boot project, set the name to Leon-provider-1, select Web, Consul Discovery from the dependency drop-down list, select 1.5.21 Modify the configuration file application.yml, and specify the service port, name, and Consul service address

server:
  port: 8082
spring:
  application:
    name: leon-provider
  cloud:
    consul:
      host: localhost
      port: 8500
Copy the code

Add monitoring dependencies:

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

Copy the code

Create a LoginService class that provides the login method for the Consumer to call

@RestController
public class LoginService {

    @RequestMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        if (userName.equals("leon") && passWord.equals("888")) {
            return "login success";
        }
        return "login fail"; }}Copy the code

Transformation of Consumer Services

Add Feign dependencies to leon-consumer, and then call the interface provided by the Provider to add the dependencies to the pom.xml of the Leon-Consumer project:

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

Create an interface: UserFeignClient, define the interface methods to be called (as provided by the Provider), add the @feignClient annotation, and specify the service name to be called by name:

@FeignClient(name = "leon-provider")
public interface UserFeignClient {
    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord);
}

Copy the code

Create a Controller class that calls the Provider service method:

@RestController
public class UserController {

    @Autowired
    private UserFeignClient userFeignClient;

    /** * User account name and password login **@param userName
     * @param passWord
     * @return* /
    @RequestMapping("/login")
    public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
        returnuserFeignClient.login(userName, passWord); }}Copy the code

Add an annotation to the startup class: @enableFeignClients

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

Run the test

Run leon-provider and leon-consumer respectively. Then access the login methods provided by leon-consumer and pass the correct and incorrect parameters to check the effect.

The service can find the corresponding service invocation through the registry.

Service cluster

Copy the leon-provider-1 project to leon-provider-2, then open the project and change the project port to 8083

server:
  port: 8083
spring:
  application:
    name: leon-provider
  cloud:
    consul:
      host: localhost
      port: 8500
Copy the code

Modify the leon-provider-1 and leon-provider-2 methods and add the identification information:

@RequestMapping("/login")
public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
    if (userName.equals("leon") && passWord.equals("888")) {
        return "leon-provider-1: login success";
    }
    return "leon-provider-1: login fail";
}
Copy the code
@RequestMapping("/login")
public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord) {
    if (userName.equals("leon") && passWord.equals("888")) {
        return "leon-provider-2: login success";
    }
    return "leon-provider-2: login fail";
}
Copy the code

Restart the services respectively, and you can see that a new service with the same name is added

Then refresh the call login method. You can see that different services are called based on the default load balancing policy of the Ribbon in the Spring Cloud system.

Other configuration

Service detection interval

Cloud: Consul: host: localhost port: 8500 Discovery: health-check-interval: 3s // If the service stops after 3 seconds, the detection interface changes to abnormal status immediatelyCopy the code