Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure the microservice development environment

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge of "how to configure the micro-service development environment". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Microservice development environment prepares SpringCloud version

Related documents

SpringCloud official website https://spring.io/projects/spring-cloud/

Naming rules for historical background version numbers

With regard to the classification of SpringCloud, the change in the naming style of the SpringCloud version can be divided into two stages:

Name of London Underground Station

Prior to SpringCloud 2020.0.0, all versions of SpringCloud were named from Amurz according to the names of London subway stations, as follows:

For ease of calling, it is generally called A version, B version, F version and H version.

SR9, every time SpringCloud solves a major bug, the SR version is added, and SR9 means that nine major bug are solved on the basis of H version.

Calendar version

Starting with Spring Cloud 2020.0.0-M1, Spring Cloud abolished the naming of London subway stations in the UK and used a new "calendar" version of naming.

What is a calendar version?

English name: Calendar Versioning

The calendar version is not based on any number, but a version control convention based on the release date of the project, and the version will get better and better over time.

Spring Cloud uses the naming convention of YYYY.MINOR.MICRO:

The suffix delimiter for the pre-release version is also derived from. Change to -, as shown in the naming of 2020.0.0-M1 and 2020.0.0-RC2.

Using the calendar version naming method, you can see the year of the current version more clearly, and you don't know how long you haven't upgraded when you see the alphanumeric version number.

YYYY: indicates a 4-digit year

MINOR: represents an increasing number, starting with 0 each year

MICRO: represents the version number suffix, just like the previously used. 0 is similar to .RELEASE, and .2 is similar to .SR2.

What kind of changes have the version changes brought to SpringCloud?

At present, the change of SpringCloud is mainly reflected in the support for the Netflix suite, the early SpringCloud mainly encapsulated the Netflix suite to achieve service registration discovery, load balancing, circuit breaker and other functions, but with the iteration of the version, because Netflix stopped the update of open source projects, so SpringCloud began to gradually reduce the support for Netflix suite. Simplicity can be divided into the following three time phases:

In the early days, SpringCloud fully encapsulated the Netflix suite, and the core functions were provided by Netflix components

Around 2018, Netflix announced that its core components Hystrix, Ribbon, Zuul and so on are in a state of maintenance. At the same time, the Spring Cloud Netflix project entered maintenance mode and plans to abolish it in 2020.

Late on the night of December 22, 2020, Beijing time, Spring Cloud 2020.0.0 was officially released, all Netflix modules were removed, and alternative components were given.

According to the above changes, in short, H version and 2020 version are currently a demarcation line version, from then on, SpringCloud abandoned the Netflix suite and entered a new era, but for enterprise development, it is not necessary to update the latest version immediately.

On the one hand, a large number of projects are still developed using Netflix components, so there is no need to replace them at present. On the other hand, some technologies are not fully compatible with the new version, such as SpringCloud Alibaba.

Summary

This tutorial is mainly based on H version, and still uses Netflix components to develop micro-service projects. at the same time, we will also learn more about SpringCloud Alibaba-related components, and according to the actual situation of our project, we will explain the functions of business scenarios such as authentication, distributed transactions, distributed locks, distributed log management and so on.

In my opinion, this tutorial serves as a connecting link between the preceding and the following. For enterprises that widely use Netflix components, learning this tutorial can quickly get started with development, is also familiar with future technology trends, and can carry out technical iterations of the project at the right time.

The next version of the tutorial will be SpringCloud 2020, which removes Netflix components while leaning more towards cloud native development.

Select the version of the tutorial

The main consideration for the selection of SpringCloud version is the compatibility between SpringCloud and SpringBoot. It is recommended to strictly follow the official website documents, as follows:

Version selection:

SpringCloud Hoxton.SR9

SpringBoot 2.3.5.RELEASE

SpringCloud Alibaba 2.1.2.RELEASE

Java version of Java8

Project build parent project creation

Create a parent project

Confirm IDEA settings

Maven Settin

File filtering may not be configured

Do not display the .idea folder

Parent project pom file

4.0.0 com.baizhi microservices-cloud 1.0-SNAPSHOT pom Unified dependent version 1.8 UTF-8 1.8 Hoxton.SR9 2.3.5.RELEASE 2.1.2.RELEASE org.springframework.boot spring-boot-dependencies ${spring.boot-version} pom import org.springframework.cloud spring-cloud-dependencies ${spring.cloud-version} pom import com .alibaba.cloud spring-cloud-alibaba-dependencies ${spring.cloud.alibaba-version} pom import org.springframework.boot spring-boot-maven-plugin

Set to skip unit tests

Merchandise service and order service creation

Create a project through Spring Initializr

Spring Initializr SpringBoot project engineering scaffolding, you can directly create a SpringBoot project, including application configuration files and startup classes. Https://start.spring.io/

Similar scaffolding also requires Alibaba's Java project construction tool: https://start.aliyun.com/bootstrap.html

IDEA integrates Alibaba Java Engineering Scaffold https://start.aliyun.com/article/initializr/springbootideaplugin

Creation of goods and services

Choose to create a project through Spring Initializr

Specify the address of using Alibaba Java engineering scaffolding https://start.aliyun.com/

Choose to rely on creating a project

Modify the pom file to inherit the parent project

Modify the pom file for goods and services as follows

4.0.0 cloud-product 0.0.1-SNAPSHOT cloud-product goods and Services com.baizhi cloud-demo 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test Org.junit.vintage junit-vintage-engine

Modify the parent project pom file

Create a test Controller interface in a commodity service

Write an application configuration file to start the project

Order Service creation

The effect is as follows:

Choose to create a project through Spring Initializr

Specify the address where Alibaba Java engineering scaffolding is used

Choose to rely on creating a project

Modify the pom file to inherit the parent project

Create a test Controller interface in the order service

Write an application configuration file to start the project

How to obtain goods and Service data by RestTemplate order Service

Service invocation: order service sends requests, requests goods and services, and obtains response data

How does the Java code send a http request?

HTTPClient Apache

RestTemplate Spring

Reference: https://mp.weixin.qq.com/s/3MfNWtTfckGr2hG9Ly9OPg

Introduction and basic use of RestTemplate

The template class encapsulated in Spring that sends RestFul requests (HTTP) through Java code, with built-in methods for sending requests such as get post delete, can be used directly in SpringBoot as long as the dependency of spring-boot-starter-web is imported.

Import dependent spring-boot-starter-web org.springframework.boot spring-boot-starter-web configuration RestTemplate

Send the request package com.baizhi.cloudorder;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.http.ResponseEntity;import org.springframework.web.client.RestTemplate;import java.util.HashMap;import java.util.Map;@SpringBootTestclass CloudOrderApplicationTests {@ Autowired private RestTemplate restTemplate; private String url = "http://localhost:8081/product/getProductById?id=1"; using RestTemplate's APi in the test class / * * send request for merchandise service * * Commodity service interface address: http://localhost:8081/product/getProductById * / @ Test void contextLoads () {/ * getForObject getForEntity * Parameter 1 url * Parameter 2 Class object of response data * Parameter 3 Url parameters [can not be parameters can be directly? Stitching] * * getForObject return value is the response data of the called interface * getForEntity return value is the response body object, which contains more data: interface response data, response status code, etc. * / String result = restTemplate.getForObject (url, String.class); System.out.println (result); ResponseEntity entity = restTemplate.getForEntity (url, String.class) System.out.println (entity.getStatusCode ()); System.out.println (entity.getBody ());} @ Test void test1 () {/ / encapsulated by map parameter Map map = new HashMap (); map.put ("id", 1) String result = restTemplate.getForObject ("http://localhost:8081/product/getProductById?id={id}", String.class, map); System.out.println (result);}}

Finally, the merchandise service calls the order service through RestTemplate

This is the end of the content of "how to configure the microservice development environment". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report