Now the idea of micro-service is in full swing, and various companies are carrying out technology transformation in full swing. Mastering SpringCloud has become a stepping stone for promotion and salary increase. The author will start from this paper to record the learning process of SpringCloud from the real world and principles.

Yards cloud address

Gitee.com/zhaowenyi/s…

1. The first SpringCloud

Open the Chinese official website of SpingCloud, a line of “a master of microservices, cloud computing best business practices” is impressed, which shows the influence of SpringCloud in the field of microservices.

SpringCloud is a combination of technology stacks:

  • Spring Cloud Config: Manages configuration packages that allow you to centrally manage cluster configuration files on remote servers. Support for local storage, Git, subversion.
  • Spring Cloud Bus: Event and message Bus that propagates state changes, such as configuration file changes, across the cluster. It can be used in combination with Spring Cloud Config.
  • Netflix Eureka: Service Registry and Discovery, a REST-based service for service registry and discovery, failover, and more
  • Netflix Hystrix: Fuses, fault-tolerant management tools designed to provide greater fault-tolerance for delays and failures by controlling nodes of services and third-party libraries through circuit breakers.
  • Netflix Zuul: A framework that provides edge services such as dynamic routing, monitoring and security.
  • Netflix Ribbon: Cloud load balancing with a variety of load balancing options that can be combined with fuses and service discovery
  • Open Feign: Open Feign is a declarative, modular HTTP client

These are just a few of the common frameworks listed. There are many more that are not listed. It is difficult to understand what these frameworks are for just by definition.

2. Meet SpringBoot

SpringBoot is not listed in the framework above because SpringBoot is the most basic framework, the building block, and any microservice starts with building a SpringBoot project.

So what is Spring Boot?

Opening the Spring Boot website, we learned that with Spring Boot we can easily create stand-alone, production-level Spring-based applications that we can “run.” Spring Boot helps us to configure the cumbersome configuration of the Spring project, and has a built-in server, such as Tomcat, which we can run directly.

In short, the Spring idea has not changed, just to help us build a simple and fast Spring project.

3. Build SpringCloud project

The next step is to start building the SpringCloud project. The first choice is to build the Spring Cloud project parent project.

3.1 Obtaining Tools

  • The JDK: 1.8
  • Spring the Boot: 2.0
  • IntelliJ IDEA: ULTIMATE 2017.02
  • Mysql: 5.6.24
  • Win10
  • Maven: 3.6

3.2 Setup Procedure

3.2.1 Creating a Parent Project

Click file-settings-build tools-maven and select your local Maven path.

Set automatic import dependencies

Editor – File Types, add “.idea; . On iml;” , set to hide the IMPl file and. Idea file

File-settings-build-complier – Java Complier, you need to set the compiled version to 1.8

The final project structure

Pom.xml adds dependencies


      
<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.elio.springcloud</groupId>
    <artifactId>springcloudtest</artifactId>
    <version>1.0 the SNAPSHOT</version>

    <! -- Unified management of JAR package versions -->
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
        <lombok.version>1.18.10</lombok.version>
        <log4j.version>1.2.17</log4j.version>
        <mysql.version>5.1.47</mysql.version>
        <druid.version>1.1.16</druid.version>
        <mybatis.spring.boot.version>1.3.0</mybatis.spring.boot.version>
    </properties>

    <! (groupId and version are not written to the submodule) -->
    <dependencyManagement><! -- Define specification, but do not import -->
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-project-info-reports-plugin</artifactId>
                <version>3.0.0</version>
            </dependency>
            <! - spring boot 2.2.2 -- -- >
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.2.2. RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <! --spring cloud Hoxton.SR1-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <! -- Spring Cloud Alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0. RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <! --mysql-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
                <scope>runtime</scope>
            </dependency>
            <! -- druid-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <! --mybatis-->
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>${mybatis.spring.boot.version}</version>
            </dependency>
            <! --junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <! --log4j-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <! -- Hot start plugin -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code
3.2.2 Creating a Subproject

After the parent project is completed, we will create two subprojects, one to be a provider of goods and services and one to be a consumer of goods and services. Start by creating a provider of goods and services

Right-click the project name and click new-Module

Enter the subproject name “Springcloud-product-Provider-8100”, which represents the provider of the product, using port 8100

After clicking Finish, we can see the subproject that was built successfully

Add the POM.xml configuration for the commodity service provider

Right-click the resources folder of the goods service provider project and add the application. Yml configuration file. After the new application

The configuration files in YML are as follows

server:
  port: 8100 # port

spring:
  application:
    name: springcloud-product-provider-8100
  datasource:
      url: jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8&userSSL=false
      driverClassName: com.mysql.jdbc.Driver
      username: root
      password: 111111

mybatis:
  mapper-locations: classpath:mapping/*mapper.xml # Mybatis maps file location
  type-aliases-package: com.elio.springcloud.entity  # Entity class package for table


Copy the code

Right-click on the Java folder and add the entity class Product, which will be the entity class for our next Product table

package com.elio.springcloud.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
public class Product {

    private Long id; / / on the id
    private String name;// Product name
    private int stock;/ / inventory

}

Copy the code

Added dao layer interface ProductMapper

package com.elio.springcloud.dao;

import com.elio.springcloud.entity.Product;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductMapper {
    /** * Query *@param id
     * @return* /
    public Product selectById(@Param("id") Long id);


    /** * Delete *@param id
     * @return* /
    public int deleteById(@Param("id") Long id);

    /** * Modify *@param id
     * @param name
     * @return* /
    public int updateById(@Param("id") Long id, @Param("name") String name);

    /** * Add *@param product
     * @return* /
    public int insertOne(Product product);
}

Copy the code

Right-click the resources file and addmapping\product_mapper.xmlThis is the SQL file for the product table. It contains simple additions, delesions, modifications and queries


      
<! DOCTYPEmapper PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.elio.springcloud.dao.ProductMapper">

    <resultMap id="BaseResultMap" type="com.elio.springcloud.entity.Product">
        <result column="id" jdbcType="BIGINT" property="id" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="stock" jdbcType="INTEGER" property="stock" />
    </resultMap>

    <! Query - - - >
    <select id="selectById" resultType="Product">
        select * from product where id = #{id}
    </select>

    <! - delete - >
    <delete id="deleteById" parameterType="Long">
        delete from product where id = #{id}
    </delete>

    <! - change - >
    <update id="updateById" parameterType="Product">
        update product set name = #{name} where id = #{id}
    </update>

    <! -- -- -- > new
    <insert id="insertOne" parameterType="Product">
       insert into product(name, stock) values (#{name}, #{stock})
    </insert>

</mapper>

Copy the code

The productService. Java file was added

package com.elio.springcloud.service;

import com.elio.springcloud.entity.Product;
import org.apache.ibatis.annotations.Param;

/** * Goods and services */
public interface ProductService {

    /** * Query *@param id
     * @return* /
    public Product selectById(Long id);


    /** * Delete *@param id
     * @return* /
    public int deleteById(Long id);

    /** * Modify *@param id
     * @param name
     * @return* /
    public int updateById(Long id, String name);

    /** * Add *@param product
     * @return* /
    public int insertOne(Product product);

}


Copy the code

New ProductServiceImpl. Java classes

package com.elio.springcloud.service.impl;

import com.elio.springcloud.dao.ProductMapper;
import com.elio.springcloud.entity.Product;
import com.elio.springcloud.service.ProductService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class ProductServiceImpl implements ProductService{

    @Resource
    private ProductMapper productMapper;

    public Product selectById(Long id) {
        return productMapper.selectById(id);
    }

    public int deleteById(Long id) {
        return productMapper.deleteById(id);
    }

    public int updateById(Long id, String name) {
        return productMapper.updateById(id, name);
    }

    public int insertOne(Product product) {
        returnproductMapper.insertOne(product); }}Copy the code

The new Result. Java

package com.elio.springcloud.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class Result {

    private int code;
    private String message;
    private Object result;
}

Copy the code

New ProductProviderController. Java

package com.elio.springcloud.controller;

import com.elio.springcloud.dto.Result;
import com.elio.springcloud.entity.Product;
import com.elio.springcloud.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/")
public class ProductProviderController {

    @Resource
    private ProductService productService;

    /** * Query *@param id
     * @return* /
    @GetMapping("product/provider/get/{id}")
    public Result selectById(@PathVariable("id") Long id){
        return new Result(200."Query succeeded", productService.selectById(id));
    }

    /** * Delete *@param id
     * @return* /
    @GetMapping("product/provider/delete/{id}")
    public Result deleteById(@PathVariable("id") Long id){
        return new Result(200."Deletion successful", productService.deleteById(id));
    }

    /** * Modify *@param product
     * @return* /
    @PostMapping("product/provider/update")
    public Result updateById(@RequestBody Product product){
        return new Result(200."Modification succeeded", productService.updateById(product.getId(), product.getName()));

    }

    /** * Add *@return* /
    @PutMapping( "product/provider/add")
    public Result insertById(@RequestBody Product product){
        return new Result(200."Modification succeeded", productService.insertOne(product)); }}Copy the code

Right-click the Java folder and add the main startup class ProductProvider8100

ProductProvider8100 contains the following contents, with @SpringBootApplication and MapperScan annotations

package com.elio.springcloud;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.elio.springcloud.dao")
public class ProductProvider8100 {

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

Create table structure


-- ----------------------------
-- Table structure for product
-- ----------------------------
DROP TABLE IF EXISTS `product`;
CREATE TABLE `product` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `stock` int(10) unsigned DEFAULT NULL.PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Copy the code

test

Now that we’ve created a good provider for the microservice, it’s time to test the provider

You can view the following results after the startup is successful

Browser address input [http://localhost:8100/product/provider/get/1]

3.3.3 Create consumers of goods and services

Right click on the parent project and click Next

Configure the pim.xml file for the consumer of the goods


      
<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">
    <parent>
        <artifactId>springcloudtest</artifactId>
        <groupId>com.elio.springcloud</groupId>
        <version>1.0 the SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-product-consumer-8200</artifactId>


    <dependencies>
        <! --spring boot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <! -- Hot deployment -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <! --lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <! -- Hot start plugin -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
Copy the code

Add the application.yml file to the resources folder

server:
  port: 8200

spring:
  application:
    name: springcloud-product-consumer-8200

Copy the code

Added the main startup class ProductConsumer8200, because the consumer does not need to connect to the database, so there are no import dependencies in POM.xml, Therefore, classes that are automatically injected into the data source need to be filtered out @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

package com.elio.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
public class ProductConsumer8200 {

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

The new business class ProductConsumerController

package com.elio.springcloud.controller;

import com.elio.springcloud.dto.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
public class ProductConsumerController {

    @Resource
    RestTemplate restTemplate;

    public static String url = "http://localhost:8100/";

    /** * Query *@param id
     * @return* /
    @GetMapping("product/consumer/get/{id}")
    public Result selectById(@PathVariable("id") Long id){

        return new Result(200."Query succeeded",
                restTemplate.getForObject(url+"product/provider/get/"+id, Result.class)); }}Copy the code

New Result return entity class Result

package com.elio.springcloud.dto;


import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Setter
@Getter
public class Result {

    private int code;
    private String message;
    private Object result;
}

Copy the code

Since we are calling the consumer’s API, we need to inject the restTemplate object and add RestConfig

package com.elio.springcloud.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {

    @Bean
    public RestTemplate getRestTemplate(a){
        return newRestTemplate(); }}Copy the code

Now that we’ve created the consumer project, it’s time to test it

Right-click the main boot class. After the boot is successful, the following information is displayed

Browser address input http://localhost:8200/product/consumer/get/1 this path is to use consumer access provider API, the results are as follows

4. To summarize

We have so far created a parent project, SpringCloudTest, and then a good service provider, Springcloud-product-Provider-8100, and a good service consumer, Springcloud-product-consumer-8200. Then, these are the two simplest microservices, which realize the decoupling of functions, but there are many problems with this simple microservice, such as using the common entity class Result, and the provider address is not written in the consumer, etc., these problems will be solved one by one. If you have any questions, please correct them in the comments section.