Java version of e-commerce spring cloud distributed microservice b2b2c social e-commerce (14) service registration (consul)

Java version of e-commerce spring cloud distributed microservice b2b2c social e-commerce (14) service registration (consul)

Springcloud b2b2c e-commerce social platform source code, please add penguin request: one zero three eight seven seven four six two six. This article mainly introduces the spring cloud consul component, which is a tool that provides service discovery and configuration. Consul is distributed, highly available, and highly scalable.

1. Introduction to
Consul Consul has the following properties:

Service discovery: Consul registers services through http, and services and services interact with each other.
Service health monitoring
key/value storage
Multi-data center
consul can run on machines such as mac windows linux.
java version of e-commerce spring cloud distributed microservice b2b2c social e-commerce
two, consul install
linux

$ mkdir -p $GOPATH/src/github.com/hashicorp && cd $!
$ git clone https://github.com/hashicorp/consul.git
$ cd consul
$ make bootstrap
$ make bootstrap
 

3. the construction project to
build a consul-miya springboot project, import dependent pring-cloud-starter-consul-discovery, its dependent files:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>com.forezp</groupId>
    <artifactId>consul-miya</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
 
    <name>consul-miya</name>
    <description>Demo project for Spring Boot</description>
 
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
 
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
 
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project><br>
 

Add the annotation @EnableDiscoveryClient to its entry file ConsulMiyaApplication to enable service discovery:

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class ConsulMiyaApplication {
 
    @RequestMapping("/hi")
    public String home() {
        return "hi ,i'm miya";
    }
 
    public static void main(String[] args) {
        new SpringApplicationBuilder(ConsulMiyaApplication.class).web(true).run(args);
    }
}
 

Specify the port of consul service as 8500 in its configuration file application.yml:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
      discovery:
        healthCheckPath: ${management.contextPath}/health
        healthCheckInterval: 15s
        instance-id: consul-miya
  application:
    name: consul-miya
server:
  port: 8502<br>
 

The source code of the B2B2C e-commerce platform that needs JAVASpring Cloud large-scale enterprise distributed microservice cloud construction, please add penguin.