Distributed project consul service registration and discovery

Keywords: Spring Java JSON jvm

When it comes to distributed nature, we need to talk about distributed and microservices.

Microsoft service is a way of software architecture, or a style of structure design. It is not a standard. Its logic is to split an entire service into separate services by business, reduce service dependency, and eliminate server message-driven. Common examples: shopping involves orders, warehouses, deductions. These functions were previously used as a project.What about micro-services when running in one jvm process, making orders, warehouses, money-deducting into three separate projects, running in three different jvm processes, using http calls or mq drivers between services.

What is distributed?Distribution places more emphasis on a deployment approach where micro-services have split a project into several different projects, and now the project needs to run to provide the corresponding services, so this is the concept of distribution, where different services are distributed across different servers, where you need to distinguish between distribution and clustering.Cluster We generally refer to redis, MySQL cluster. Here the main point is to distribute deployment of the same service, expand horizontally to achieve disaster tolerance and improve performance, while the distributed focus on different services, distributed deployment.

Last time, in the previous development, we have completed a complete set of processes and have the conditions for initial deployment. Now we are ready to prepare the spring cloud environment. At the request of the general public who are not afraid of big things, instead of eruka (due to a series of eruka2.0 issues), consul is used.

consul

Distribution is a topic that can't be bypassed: service registration, service discovery, service governance. In other words, you have to let others know where you are, how to contact you. consul is such an open source distributed server discovery and configuration management system with built-in service registration and discovery, distributed consistency protocols, and health checks. key/value storage (redis?Slightly suspended), multiple data centers ((o)...So), visualize the ui, and it has a running mode server/client (Oh, does the client send some data like a server)?

Service Registration

Consul has two ways of registering, one is for the service to register itself by calling the http api call provided by consul, the other is for spring cloud, and the other is for registering through a json configuration file, such as if you want to register mysql.

Service Discovery

There are also two ways to discover services, one through http api calls and the other using consul agent DNS.

consul installation

download https://www.consul.io/downloads.html , Download it, unzip it, and get a binary of consul.

start-up

./consul agent -server -bootstrap --bind=127.0.0.1 -client 0.0.0.0 -data-dir consul_data -ui

Visit http://127.0.0.1:8500/ui You can see consul's management interface.

spring consul

Now spring cloud is started, so create a new project, cloud-work

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

consul-discovery-client

Create a consul-discovery-client module to integrate consul clients with users.

<dependencies>
        <!--spring Realized consul Client, Provides Service Registration and Discovery-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--actuator Provide health check-ups, consul server Called on time-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--provide http Call Service-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
@EnableDiscoveryClient
@SpringBootApplication
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class,args);
    }
}
@EnableDiscoveryClient
@SpringBootApplication
public class DiscoveryApplication {
    public static void main(String[] args) {
        SpringApplication.run(DiscoveryApplication.class,args);
    }
}

iot-pt transformation

Put consul-discovery-client in a maven package, reference it in the iot-pt project, and add a spring cloud dependency.

	<dependency>
            <groupId>cn.le</groupId>
            <artifactId>consul-discovery-client</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
<dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>

Modify coap-server

Let's take the coap-server implementation first and modify the configuration file

coap.port=5683
coap.iot.byte=iot-byte
coap.iot.json=iot-json

spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500

server.port=8090
server.servlet.context-path=/coap-server
spring.application.name=coap-server
#Health Check Path
spring.cloud.consul.discovery.health-check-path= 
	${server.servlet.context-path}/actuator/health

Start the project, open the consul administration page

Concluding remarks

Other iot-pt modules do this way, which is not explained here.To learn more about consul, Click https://www.cnblogs.com/duanxz/p/7053301.html Here I also learn from losing (covering my face).

https://gitee.com/distant/cloud-work.git

https://gitee.com/distant/iot-pt.git

Posted by easmith on Wed, 15 May 2019 23:13:03 -0700