Previous: Consul registration center 01: Spring Cloud microservice architecture practice You can click review. Next, let's talk about the introduction case of consumer.
For the detailed video tutorial of spring cloud actual combat project, please leave a message.
1. Create project
Let's create an aggregation project to explain Consul. First, create a pom parent project.
2. Add dependency
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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- Project coordinate address --> <groupId>com.example</groupId> <!-- Project module name --> <artifactId>consul-demo</artifactId> <!-- Project version name snapshot version SNAPSHOT,Official version RELEASE --> <version>1.0-SNAPSHOT</version> <!-- inherit spring-boot-starter-parent rely on --> <!-- Use inheritance method to realize reuse, and all inheritable can be used --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.4.RELEASE</version> </parent> <!-- The dependent component version number is defined centrally, but is not imported, When a declared dependency is used in a subproject, the version number of the dependency can be omitted, In this way, the dependent versions used in the project can be managed uniformly --> <properties> <!-- Spring Cloud Hoxton.SR1 rely on --> <spring-cloud.version>Hoxton.SR1</spring-cloud.version> </properties> <!-- Project dependency management the parent project only declares dependency, and the child project needs to specify the required dependency(Version information can be omitted) --> <dependencyManagement> <dependencies> <!-- spring cloud rely on --> <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> </project>
3. Service provider
3.1 create project
Create a service provider service provider project under the parent project.
3.2 add dependency
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>service-provider</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- Inherit parent dependency -->
<parent>
<groupId>com.example</groupId>
<artifactId>consul-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<!-- Project dependency -->
<dependencies>
<!-- spring cloud consul rely on -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- spring boot actuator rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- spring boot web rely on -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- lombok rely on -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- spring boot test rely on -->
<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>
</project>
3.3 configuration file
application.yml
server: port: 7070 # port spring: application: name: service-provider # apply name # Configure Consul registry cloud: consul: # Access address of the registry host: localhost port: 8500 # Service provider information discovery: register: true # Need to register instance-id: ${spring.application.name}-01 # Registration instance id (must be unique) service-name: ${spring.application.name} # Service name port: ${server.port} # Service port prefer-ip-address: true # Use ip address to register ip-address: ${spring.cloud.client.ip-address} # Service request ip
3.4 entity class
Product.java
package com.example.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.io.Serializable; @Data @NoArgsConstructor @AllArgsConstructor public class Product implements Serializable { private Integer id; private String productName; private Integer productNum; private Double productPrice; }
3.5 writing services
ProductService.java
package com.example.service; import com.example.pojo.Product; import java.util.List; /** * Goods and services */ public interface ProductService { /** * Query product list * * @return */ List<Product> selectProductList(); }
ProductServiceImpl.java
package com.example.service.impl; import com.example.pojo.Product; import com.example.service.ProductService; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; /** * Goods and services */ @Service public class ProductServiceImpl implements ProductService { /** * Query product list * * @return */ @Override public List<Product> selectProductList() { return Arrays.asList( new Product(1, "Huawei Mobile", 1, 5800D), new Product(2, "Lenovo notebook", 1, 6888D), new Product(3, "Mi Pad ", 5, 2020D) ); } }
3.6 control layer
ProductController.java
package com.example.controller; import com.example.pojo.Product; import com.example.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/product") public class ProductController { @Autowired private ProductService productService; /** * Query product list * * @return */ @GetMapping("/list") public List<Product> selectProductList() { return productService.selectProductList(); } }
This project can be tested by unit test, or directly by url using postman or browser.