Introduction to spring cloud (general understanding)

Introduction to spring cloud (general understanding)

1. Learning objectives

Understanding the evolution of the system

Ø understand the difference between RPC and Http

Ø master the simple use of HttpURLConnection

Ø master the simple use of HttpClient

Ø know what spring cloud is

Ø independent establishment of Eureka Registration Center

Ø master Eureka high availability

Familiar with the concept of load balancing

Ø use Ribbon for load balancing

Ø use Feign to make declarative calls

Ø use Hystrix for fault tolerance

Ø use Hystrix Dashborad to monitor the call

Ø build gateway Zuul

Ø build configuration center

2. System architecture evolution

With the development of the Internet, the scale of website application is expanding. The surge in demand brings technical pressure. Therefore, the system architecture continues to evolve, upgrade and iterate. From single application, to vertical splitting, to distributed services, to SOA, and now the hot micro service architecture, as well as the surging Service Mesh led by Google.

2.1 centralized architecture

When the website traffic is very small, only one application is needed to deploy all functions together to reduce deployment nodes and costs. At this time, the data access framework (ORM) used to simplify the workload of addition, deletion, modification and query is the key to project development.

Existing problems:

Code coupling makes development and maintenance difficult

Ø targeted optimization cannot be carried out for different modules

Ø cannot expand horizontally

Ø low single point fault tolerance and poor concurrency

2.2 vertical split

When the number of visits increases gradually and a single application cannot meet the requirements, in order to meet higher concurrency and business requirements, we split the system according to business functions.

advantage:

Ø the system split realizes traffic sharing and solves the problem of concurrency

Ø it can be optimized for different modules

Convenient horizontal expansion, load balancing and improved fault tolerance

Disadvantages:

Ø systems are independent of each other, and there will be a lot of repeated development work, which will affect the development efficiency

For example, both the order system and the search system need to query goods, resulting in the repeated writing of the code for querying goods in these two systems.

2.3 distributed services

When there are more and more vertical applications, the interaction between applications is inevitable. Extract the core business as an independent service, and gradually form a stable service center, so that the front-end application can respond to the changing market demand more quickly. At this time, the distributed call used to improve business reuse and integration is the key.

advantage:

Ø the basic services are extracted, and the systems call each other, which improves the code reuse and development efficiency

Disadvantages:

Ø the coupling between systems becomes higher, and the calling relationship is complex and difficult to maintain

2.4 service governance (SOA)

When there are more and more services, problems such as capacity evaluation and waste of small service resources gradually appear. At this time, it is necessary to add a dispatching center to manage the cluster capacity in real time based on the access pressure and improve the cluster utilization. At this time, the resource scheduling and Governance Center (SOA service-oriented architecture) for improving machine utilization is the key

What happened before?

There are more and more services, and the address of each service needs to be managed

Ø the calling relationship is complex and it is difficult to clarify the dependency relationship

Ø there are too many services, so the service status is difficult to manage and cannot be dynamically managed according to the service situation

What should service governance do?

Ø service registration center, which realizes automatic service registration and discovery without manually recording the service address

Ø services are automatically subscribed, service lists are automatically pushed, service calls are transparent, and there is no need to care about dependencies

Ø dynamically monitor the service status monitoring report and artificially control the service status

Disadvantages:

Ø large service granularity (multiple functions form a service)

Ø there will be strong dependencies between services. Once a link goes wrong, it will have a great impact.

Ø the service relationship is complex, the operation and maintenance, test and deployment are difficult, which is not in line with the idea of DevOps

Introduction to DevOps:

Who is involved in the DevOps process? Development team and IT operation and maintenance team! So what is DevOps's intention? That is, establish good communication and cooperation between the two teams to create high-quality software faster and more reliably! The feeling of "integration" of development and operation and maintenance is caused by the bridging of skill combination and practice between developers and operation engineers and the implementation of automation (DevOps) tools. Large Internet companies around the world have adopted the DevOps approach to radically improve their performance, security and team dynamics.

2.5 microservices

The SOA mentioned above is service-oriented. Microservices, it seems, are also services. They all split the system. Therefore, it is easy to confuse the two, but there are some differences:

Characteristics of microservices:

  • Single responsibility: each service in the micro service corresponds to a unique business capability to achieve a single responsibility
  • Microservices: the service splitting granularity of microservices is very small. For example, a user management can be used as a service. Although each service is small, it has "five internal organs".

  • Service oriented: service oriented means that each service should expose the service interface API. It doesn't care about the technical implementation of the service. It has nothing to do with the platform and language, and it is not limited to what technology to implement, as long as the Rest interface is provided.

  • Autonomy: autonomy means that services are independent and do not interfere with each other

  • Team independence: each service is an independent development team, and the number cannot be too large.

  • Technology independence: because it is service-oriented and provides Rest interface, there is no interference with what technology is used

  • Front and rear end separation: the front and rear end separation development is adopted to provide a unified Rest interface, and the back end does not need to develop different interfaces for PC and mobile terminals

  • Database separation: each service uses its own data source

  • The deployment is independent. Although there are calls between services, service restart should not affect other services. Conducive to continuous integration and continuous delivery. Each service is an independent component, reusable and replaceable, reducing coupling and easy to maintain

Microservice structure diagram:

3 remote call mode

Both microservices and SOA are faced with remote invocation between services. So what are the remote invocation methods between services?

Common remote calling methods are as follows:

RPC: remote procedure call, similar to RMI. Custom data format, based on native TCP communication, fast and efficient. Early web services and now popular dubbo are typical of RPC

Http: http is actually a network transmission protocol, which is based on TCP and specifies the format of data transmission. At present, the communication between client browser and server basically adopts HTTP protocol. It can also be used for remote service calls. The disadvantage is that the message encapsulation is bloated.

Now the popular Rest style can be realized through http protocol.

3.1 understanding RPC

RPC, or Remote Procedure Call, is a computer communication protocol. The protocol allows a program running on one computer to call a subroutine of another computer without extra programming for this interaction.

To put it more popularly, computer A provides A service, and computer B can call the service of computer A as it calls the local service.

From the above concepts, we can know that there are two main points to realize RPC:

  • Realize the remote call of services of other computers

  • To realize remote call, data must be transmitted through the network. Program A provides services. Program B passes the request parameters to program A through the network. After local execution, program A obtains the results, and then returns the results to program B. There are two points to pay attention to here:

  1. What kind of network communication protocol is adopted?
  • The popular RPC frameworks now use TCP as the underlying transmission protocol (three waves to establish the connection and four waves to close the connection. Because the connection establishment takes time, it is suitable for long connections)
  1. What is the format of data transmission?
  • When the two programs communicate, the data transmission format must be agreed. Just like two people chatting, they should use the same language, otherwise they can't communicate. Therefore, we must define the format of request and response. In addition, data transmission in the network needs to be serialized, so a unified serialization method needs to be agreed.

  • Call a remote service as if it were a local service

  • If it is only a remote call, it is not RPC, because RPC emphasizes procedure call. The called process should be transparent to users. Users should not care about the details of the call. They can call remote services like calling local services. Therefore, RPC must encapsulate the calling process

RPC call flow chart:

3.2 understanding Http

Http protocol: Hypertext Transfer Protocol, which is an application layer protocol. It specifies the request format, response format, resource location and operation mode of network transmission. However, there is no regulation on what network transmission protocol is used at the bottom, but now TCP protocol is used as the bottom transmission protocol. At this point, you may think that Http is very similar to RPC remote calls. They all communicate through the network according to a specified data format, with requests and responses. Yes, at this point, the two are very similar, but there are still some subtle differences.

  • RPC does not specify the data transmission format, which can be specified arbitrarily. The data format is not necessarily the same for different RPC protocols.

  • The path of resource location is also defined in Http, which is not required in RPC

  • The most important point: RPC needs to call remote services like local services, that is, encapsulate the calling process at the API level. Http protocol does not have such requirements, so the details of request and response need to be implemented by ourselves.

  • Advantages: RPC mode is more transparent and more convenient for users. Http is more flexible. There is no API and language specified. It is cross language and cross platform
  • Disadvantages: RPC needs to be encapsulated at the API level, which limits the development language environment.

For example, when we visit a website through a browser, we use the Http protocol. It's just that the browser encapsulates the request, initiates the request, receives the response, and parses the response. If it is not through the browser, then these things need to be done by yourself.

3.3 difference between RPC and HTTP




3.4 how to select

Since both methods can realize remote call, how should we choose?

  • In terms of speed, RPC is faster than http. Although the underlying layer is TCP, the information of HTTP protocol is often bloated, but gzip compression can be used.

  • In terms of difficulty, RPC implementation is more complex, and http is relatively simple

  • In terms of flexibility, http is better because it doesn't care about implementation details, cross platform and cross language.

Therefore, both have different usage scenarios:

  • If the efficiency requirements are higher and the development process uses a unified technology stack, RPC is still good.

  • If you need to be more flexible, cross language and cross platform, http is obviously more appropriate

So how do we choose?

Microservices put more emphasis on independence, autonomy and flexibility. The RPC mode has many restrictions, so the Rest style service based on Http is generally adopted in the micro service framework

3.5 Http client tools

HTTP protocol is the most widely used and important protocol on the Internet. More and more Java applications need to access network resources directly through HTTP protocol.

Since the micro service chooses HTTP, we need to consider ourselves to realize the processing of requests and responses. However, there are many HTTP client tools in the open source world that can help us do these things, such as:

​ Ø HttpURLConnection

​ Ø HttpClient

​ Ø OKHttp

3.5.1 HttpURLConnection

java.net in JDK Net package has provided a class to access the basic functions of HTTP protocol: HttpURLConnection.

HttpURLConnection is a standard class of Java. It inherits from URLConnection and can be used to send GET requests and POST requests to specified websites. Based on URLConnection, it provides the following convenient methods:

int getResponseCode(); // Get the response code of the server.
String getResponseMessage(); // Get the response message of the server.
String getResponseMethod(); // Gets the method to send the request.
void setRequestMethod(String method); // Set the method of sending the request.

3.5.2 HttpClient

HttpClient is a product of Apache Company and a component under Http Components.

HttpClient is an enhanced version of HttpURLConnection. HttpURLConnection can do all the things that HttpClient can do; HttpURLConnection does not provide some functions, and HttpClient does, but it only focuses on how to send requests, receive responses, and manage HTTP connections.

Compared with the URLConnection of traditional JDK, HttpClient increases ease of use and flexibility. It not only makes it easy for the client to send HTTP requests, but also facilitates developers to test the interface (based on HTTP protocol), which improves the efficiency of development and the robustness of the code.

Official website address: http://hc.apache.org/httpcomponents-client-5.0.x/index.html

characteristic:

  • Based on standard and pure Java language. Implemented http1 0 and http1 one

  • All Http methods (GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE) are implemented in an extensible object-oriented structure

  • Support HTTPS protocol.

  • Establish transparent connection through Http proxy.

  • Automatically process cookies in set cookies.

3.5.3 RestTemplate of spring

Spring provides a RestTemplate template tool class, encapsulates the Http based client, and realizes the serialization and deserialization of objects and json, which is very convenient. RestTemplate does not limit the client type of Http, but abstracts it. At present, three commonly used types are supported:

​ Ø HttpClient

​ Ø OkHttp

Ø JDK native URLConnection (default)

4 Http client example

4.1 service providers and service consumers

Using microservices to build distributed services, microservices communicate with each other through the network.

We use service providers and service consumers to describe the invocation relationship between microservices.

noun definition
Service provider The callee of the service (i.e. the service that provides services for other services)
Service consumer The caller of the service (i.e. services that depend on other services)

Taking ticketing network as an example, as shown in the figure below, the user initiated a ticket purchase request to the film micro service. Before the ticket purchase business operation, the movie microservice needs to call the user microservice interface to query the current user's balance and whether it meets the ticket purchase standards.

In this scenario, the user micro service is a service provider, and the movie micro service is a service consumer

Service providers and service consumers

4.1 writing service providers

Self written

Consumer services

4.2.1 using HttpURLConnection

GET request

/**
 * GET Request example
 *
 * @author Thomas Guo
 *
 */
public class GetDemo {

    public static void main(String[] args) {
        try {
            // 1. Get the URL of the access address
            URL url = new URL(
                    "http://localhost:8888/user/3");
            // 2. Get the network access object Java net. HttpURLConnection
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            /* 3. Set request parameters (expiration time, input / output stream and access mode) to connect in the form of stream */
            // Set whether to output to HttpURLConnection
            connection.setDoOutput(false);
            // Set whether to read from httpUrlConnection
            connection.setDoInput(true);
            // Set request mode
            connection.setRequestMethod("GET");
            // Set whether to use cache
            connection.setUseCaches(true);
            // Sets whether this HttpURLConnection instance should automatically perform HTTP redirection
            connection.setInstanceFollowRedirects(true);
            // Set timeout
            connection.setConnectTimeout(3000);
            // connect
            connection.connect();
            // 4. Get the return value responseCode of the response status code
            int code = connection.getResponseCode();
            // 5. If the return value is normal, the data in the network is the data returned by the server in the form of stream
            String msg = "";
            if (code == 200) { // Normal response
                // Read response information from stream
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String line = null;

                while ((line = reader.readLine()) != null) { // Loop reads from stream
                    msg += line + "\n";
                }
                reader.close(); // Close flow
            }
            // 6. Disconnect and release resources
            connection.disconnect();

            // Display response results
            System.out.println(msg);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

POST request

/**
 * POST Request example
 *
 * @author Thomas Guo
 *
 */
public class PostDemo {

    public static void main(String[] args) {
        try {
            // 1. Get access address URL
            URL url = new URL("http://localhost:8888/user/add");
            // 2. Create HttpURLConnection object
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            /* 3. Set request parameters, etc */
            // Request mode
            connection.setRequestMethod("POST");
            // Timeout
            connection.setConnectTimeout(3000);
            // Set whether to output
            connection.setDoOutput(true);
            // Set whether to read in
            connection.setDoInput(true);
            // Set whether to use cache
            connection.setUseCaches(false);
            // Sets whether this HttpURLConnection instance should automatically perform HTTP redirection
            connection.setInstanceFollowRedirects(true);
            // Set the name value pair of encoding parameters using the standard encoding format
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            // connect
            connection.connect();
            /* 4. Processing input and output */
            // Write parameters to request
            String params = "username=hanqi&age=28&name=hanqiname&balance=123.56";
            OutputStream out = connection.getOutputStream();
            out.write(params.getBytes());
            out.flush();
            out.close();
            // Read response information from connection
            String msg = "";
            int code = connection.getResponseCode();
            if (code == 200) {
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                String line;

                while ((line = reader.readLine()) != null) {
                    msg += line + "\n";
                }
                reader.close();
            }
            // 5. Disconnect
            connection.disconnect();

            // Processing results
            System.out.println(msg);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

4.2.2 using HttpClient

GET request

public class GetDemo {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient= HttpClients.createDefault();
        HttpGet request = new HttpGet("http://localhost:8888/user/3");
        String response = httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }
}

POST request

public class PostDemo {
    public static void main(String[] args) throws IOException {
        CloseableHttpClient httpClient= HttpClients.createDefault();
        HttpPost httpPost = new HttpPost("http://localhost:8888/user/add");
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        Map<String, String> paramMap = new HashMap();
        paramMap.put("name","qianba");
        paramMap.put("username","qianba");
        paramMap.put("age","12");
        paramMap.put("balance","125.68");
        // Create parameter list
        if (paramMap != null) {
            List<NameValuePair> paramList = new ArrayList<>();
            for (String key : paramMap.keySet()) {
                paramList.add(new BasicNameValuePair(key, paramMap.get(key)));
            }
            // Simulation form
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList,"utf-8");
            httpPost.setEntity(entity);
        }


        // Execute http request
        CloseableHttpResponse response = httpClient.execute(httpPost);
        String resultString = EntityUtils.toString(response.getEntity(), "utf-8");

        System.out.println(resultString);
    }
}

4.3 using RestTemplate

First, register a RestTemplate object in the project, which can be registered at the startup class location

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }
    //Inject restTemplate object
    @Bean
    public RestTemplate restTemplate() {

        return new RestTemplate(); //URLConnection is used by default
    }
}

Pass the url address and the bytecode of the entity class through the getForObject() method of RestTemplate. RestTemplate will automatically initiate the request, receive the response, and help us deserialize the response result.

New springboot project test

Use unit test classes to test

@RunWith(SpringRunner.class)
@SpringBootTest
public class RestTemplateTest {

    @Autowired
    private RestTemplate restTemplate;

    @Test
    public void httpGet() {
        //Parameter 1 indicates the access address, and parameter 2 indicates the return type (automatically convert the user of json string into an object)
        User user = this.restTemplate.getForObject("http://localhost:8888/user/3", User.class);
        System.out.println(user);
    }
    @Test
    public void httpPost() throws JsonProcessingException {
        HttpHeaders header = new HttpHeaders();
        header.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
        postParameters.add("name", "Jiang Jiu");
        postParameters.add("username", "jiangjiu");
        postParameters.add("age", 88);
        postParameters.add("balance", 128.99);

        HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(postParameters, header);
        ResponseEntity<Integer> res = restTemplate.postForEntity("http://localhost:8888/user/add", entity, Integer.class);
        System.out.println(res.getBody());
    }
}

Continuous update: see other chapters

MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
postParameters.add("name", "Jiang Jiu");
postParameters.add("username", "jiangjiu");
postParameters.add("age", 88);
postParameters.add("balance", 128.99);

    HttpEntity<MultiValueMap<String, Object>> entity = new HttpEntity<>(postParameters, header);
    ResponseEntity<Integer> res = restTemplate.postForEntity("http://localhost:8888/user/add", entity, Integer.class);
    System.out.println(res.getBody());
}

}

# Continuous update: see other chapters

Tags: Java Spring Distribution Spring Cloud

Posted by shibobo12 on Wed, 11 May 2022 21:35:49 +0300