SpringCloud initializes parent-child projects and integrates Consul service discovery
The preparatory work
- IDEA2020.1
- JDK1.8
- Spring Cloud version: Hoxton.sr5
- Spring Boot version: 2.3.0.release
Install the Consul
- Windows installation: www.yuque.com/ekko/app/go…
- Mac installation: www.yuque.com/ekko/app/nz…
start
Profile: Consul’s features
- Service discovery
- The Key/Value store
- Health check
Today we are going to practice service discovery by starting with start.spring. IO/initializing a parent project
</build>
<repositories>
<! -- Ali Cloud repository, which represents Maven Central and JCenter repository -->
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<! -- Ali Cloud agent Spring official warehouse -->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
Copy the code
Save, then open with IDEA, and wait for Maven to finish loading
New subproject
Right-click the project name, New -> Module
Finish
Associated parent project
Add it above the
tag in the parent Module’s POM.xml
<modules>
<module>pro-service</module>
</modules>
Copy the code
Such as graphic
pro-service
The < modelVersion > 4.0.0 < / modelVersion >
<parent>
<groupId>com.github.springtools</groupId>
<artifactId>SpringCloudPro</artifactId>
<version>0.0.1</version>
</parent>
Copy the code
Maven can’t find some Spring Boot dependencies to add the definition of the version number of the management dependencies in the parent project
pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.springtools</groupId>
<artifactId>SpringCloudPro</artifactId>
<version>0.0.1</version>
<name>SpringCloudPro</name>
<description>The parent project</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR5</spring-cloud.version>
<spring-boot.version>2.3.0. RELEASE</spring-boot.version>
</properties>
<modules>
<module>pro-service</module>
</modules>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<! - monitoring - >
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<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>
<! -- Support Spring Boot 2.3.X-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</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>
<repositories>
<! -- Ali Cloud repository, which represents Maven Central and JCenter repository -->
<repository>
<id>aliyun</id>
<name>aliyun</name>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<! -- Ali Cloud agent Spring official warehouse -->
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://maven.aliyun.com/repository/spring</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
Copy the code
Subprojects pom. XML
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.github.springtools</groupId>
<artifactId>SpringCloudPro</artifactId>
<version>0.0.1</version>
</parent>
<artifactId>pro-service</artifactId>
<version>0.0.1</version>
<name>pro-service</name>
<description>Business module</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
Project structure drawing
Integration service finds Consul
The parent project adds the following dependencies, which were added in the final parent project pom.xml above, as outlined here
pro-service
resources
application.properties
bootstrap.yml
Since the config will be used later, bootstrap has a higher loading priority than application.properties
Complete subprogram and register with Consul
bootstrap.yml
spring:
application:
name: pro-service
cloud:
consul:
discovery:
health-check-path: /actuator/health Check instance health
health-check-interval: 10s Check every 10 seconds
hostname: localhost Configure the instance address
register: true # Automatic registration
service-name: ${spring.application.name} # instance name
host: localhost
port: 8500
Copy the code
Ps: the spring here. The application. The name and spring. Cloud. Consul. Discovery. The service – name must add, or it will throw an exception
Caused by: java.lang.IllegalArgumentException: Consul service ids must not be empty, must start with a letter, end with a letter or digit, And have the as interior characters only letters, who - reference: https://www.jianshu.com/p/83d3a8105620Copy the code
Define start class ProServiceApplication. Java
package com.github.springtools.proservice;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableDiscoveryClient
@SpringBootApplication
public class ProServiceApplication {
@GetMapping("/")
public String hello(a) {
return "Hello world";
}
public static void main(String[] args) { SpringApplication.run(ProServiceApplication.class, args); }}Copy the code
Start the
http://localhost:8500
Source code address
Github.com/Gleans/Spri…
It’s one o ‘clock in the morning, my hair is gone, every day goes by so fast, fuck