Summary of solving cross-domain issues through agreements

1. HttpClient

1.1 Description of business requirements

Insert picture description here

1.2 Introduction to HttpClient

The HTTP protocol may be the most used and most important protocol on the Internet. More and more Java applications need to directly access network resources through the HTTP protocol. Although the basic functions of accessing the HTTP protocol have been provided in the java net package of JDK, for most applications, the functions provided by the JDK library itself are not rich and flexible enough. HttpClient is a sub-project of Apache Jakarta Common, used to provide efficient, up-to-date, feature-rich client programming toolkit that supports the HTTP protocol, and it supports the latest version and recommendations of the HTTP protocol. HttpClient has been used in many projects. For example, two other well-known open source projects on Apache Jakarta, Cactus and HTMLUnit, both use HttpClient. The latest version of HttpClient is HttpClient 4.5.6 (2015-09-11)

1.3 HttpClient entry case

1.3.1 Import jar package

 <!--添加httpClient jar包 -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>

1.3.2 HttpClient entry case

package com.jt.test;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

//@SpringBootTest   //从spring容器中获取bean对象,之后完成测试业务.
public class TestHttpClient {
    
    

    /**
     * 1.实例化HttpClient对象
     * 2.定义远程访问的url地址
     * 3.定义请求类型的对象
     * 4.发起http请求,获取响应的结果
     * 5.对返回值结果进行校验.获取真实的数据信息.
     * */
    @Test
    public void testGet() throws IOException {
    
    
       HttpClient httpClient = HttpClients.createDefault();
       String url = "http://www.baidu.com";
       HttpGet httpGet = new HttpGet(url);
       HttpResponse httpResponse = httpClient.execute(httpGet);
       //常见结果状态 200 404 406参数异常  500后端服务器异常 504超时  502访问的网址不存在
        //获取状态码
        int status = httpResponse.getStatusLine().getStatusCode();
        if(status == 200){
    
    
            //获取响应结果
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            System.out.println(result);
        }
    }
}

1.4 HttpClient implements business logic

1.4.1 Business requirements The
user requests through http://www.jt.com/user/testHttpClient to obtain the UserList collection information.
Request when the JT-WEB server accesses JT-SSO http://sso.jt.com/user/ testHttpClient

1.4.2 Edit front-end Controller

 @RequestMapping("/testHttpClient")
    @ResponseBody
    public List<User> testHttpClient(){
    
    

        return userService.testHttpClient();
    }

1.4.2 Edit front-end Service

package com.jt.service;
import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    
    
    @Override
    public List<User> testHttpClient() {
    
    
        List userList = new ArrayList<>();
        //由jt-web服务器去链接jt-sso的服务器
        String url = "http://sso.jt.com/user/testHttpClient";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {
    
    
            HttpResponse httpResponse =httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){
    
    
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
            userList = ObjectMapperUtil.toObject(result, userList.getClass());
           /* for (LinkedHashMap<String,Object> map : userList){
                    User userTemp = new User();
                    userTemp.setId( Long.parseLong(map.get("id")+""));
                    userTemp.setUsername((String)map.get("username"));
                    userTemp.setPassword((String)map.get("password"));
                    userTemp.setPhone((String)map.get("phone"));
                    userTemp.setEmail((String)map.get("phone"));
                    userList2.add(userTemp);
                }*/
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        return userList;
    }
}

1.4.3 Edit backend Controller

 /**
     * http://sso.jt.com/user/testHttpClient
     * 返回List<User>
     */
    @RequestMapping("testHttpClient")
    public List<User> testHttpClient(){
    
    

        return userService.findAll();
    }

1.4.4 Edit backend Service

 @Override
    public List<User> findAll() {
    
    

        return userMapper.selectList(null);
    }

2. SOA Thought

2.1 Introduction to SOA ideas

Service-oriented architecture (SOA) is a component model that separates different functional units (called services) of an application and connects them through well-defined interfaces and protocols between these services. The interface is defined in a neutral way, and it should be independent of the hardware platform, operating system, and programming language that implement the service. This allows services built in a variety of systems to interact in a unified and common way.
Insert picture description here

3. RPC introduction (collective term for call form)

3.1 Introduction to RPC

RPC (Remote Procedure Call) remote procedure call, the simple understanding is that a node requests a service provided by another node.
Local procedure call: If you need to add the age+1 of the local student object, you can implement an addAge() method to pass the student object Enter, and return after updating the age. The function body called by the local method is specified by a function pointer.
Remote procedure call: The addAge method is in other servers. If you need to call it, you must notify other servers to help me complete the business call.

Summary: Use a third-party server to help me complete the process of business calls.
Understanding: Almost all business calls in a distributed environment are RPC.

4. Microservices

4.1 What is a microservice

Description:

  1. In order to reduce the coupling of the code, the project is split. It is split into several projects according to functional modules. This project is called a service. (Distributed thinking).
  2. If the structure of microservices is adopted, it is required that if the server fails, it should realize automatic failure migration (high availability HA)

4.2 Analysis of existing services

Note: Since both nginx load balancing/reverse proxy require artificial configuration, and the failure of the migration cannot be implemented in time if there is a problem, it needs to be upgraded to the design of the microservice architecture.
Insert picture description here

4.3 Microservice architecture design

Insert picture description here
Implementation steps:

  1. When the service provider starts, it registers its information in the registry.
  2. After the registration center receives the user's request, it updates the service list information.
  3. When the consumer starts, it will first link to the registry to obtain the service list data.
  4. The registration center synchronizes its own service list information to the client (consumer)
  5. After the consumer receives the service list data, he saves the information to his local. It is convenient for the next call
  6. When the consumer receives the user's request, it performs load balancing based on the information in its own service list, selects one of the service providers, and makes RPC calls based on IP:PORT.
  7. When the service provider is down, the registry will have a heartbeat detection mechanism.If the check is down, the local service list data will be updated, and the entire network will broadcast to notify all consumers to update the service list.

4.4 Why are clusters generally odd?

Formula: Surviving nodes> N/2
Common sense: The smallest cluster has 3 units.
Example:
Can one node build a cluster? 1-1> 1/2 Fake 1 node cannot build a cluster,
2 nodes can build a cluster Cluster? 2-1> 2/2 Fake 2 nodes cannot build a cluster.
Can 3 nodes build a cluster? 3-1> 3/2 Real 3 nodes can build a cluster.
4 nodes can build a cluster? 4- 1> 4/2 It is true that 4 nodes can build a cluster.
For example:
3 nodes allow up to 1 downtime, otherwise the cluster will crash.
4 nodes are allowed up to 1 downtime, otherwise the cluster will collapse.
Actually build odd and even numbers Both are ok, but from the perspective of disaster tolerance, it is found that odd and even numbers have the same effect, so we build odd numbers.

4.5 ZK cluster election rules

Note: The zk cluster election is implemented by the algorithm of the maximum value (myid) first. If there are no hosts in the cluster, the election will start (more than half is sufficient), and if there are hosts, the election will end.
Exam questions: 1 2 3 4 5 6 7 in order When starting up
Question 1: Who is the host? 4 When the host
Question 2: Who can never be elected as the host? 1,2,3

Guess you like

Origin blog.csdn.net/qq_41536934/article/details/112250365