如何设计一个注册中心?(2)实现注册接口

1. 创建SpringBoot工程

创建父工程及三个子模块,其中一个模块作为注册中心,另外两个作为服务提供者。
如何设计一个注册中心?(2)实现注册接口_第1张图片
pom


<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.0modelVersion>

    <groupId>org.examplegroupId>
    <artifactId>registerTestartifactId>
    <packaging>pompackaging>
    <version>1.0-SNAPSHOTversion>
    <modules>
        <module>servermodule>
        <module>provider1module>
        <module>provider2module>
    modules>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.1.RELEASEversion>
        <relativePath/> 
    parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.httpcomponentsgroupId>
            <artifactId>httpclientartifactId>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>fastjsonartifactId>
            <version>1.1.23version>
        dependency>
    dependencies>

project>

2. 实现http访问

注意:这里目前用的是同步请求,实际用异步才是合适的

package http.client;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpClient {
    private String url;
    private String contentType;
    private String encoding;
    private JSONObject data;

    public HttpClient(String url, String encoding, String contentType, JSONObject data) {
        this.url = url;
        this.data = data;
        this.encoding = encoding == null ? "UTF-8" : encoding;
        this.contentType = contentType == null ? "application/json" : contentType;
    }

    public JSONObject httpGet() throws Exception {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            HttpGet httpGet = new HttpGet(url);
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() != 200) {
//            throw new Exception("调用服务端异常.");
                return getResult(false, "调用服务端异常.");
            }
            HttpEntity res = response.getEntity();
            String resultData = EntityUtils.toString(res);
            System.out.println("从服务端返回结果: " + resultData);
            return JSONObject.parseObject(resultData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getResult(false, "");
    }

    public JSONObject httpPost() throws Exception {
        try (CloseableHttpClient httpClient = HttpClientBuilder.create().build()) {
            String json = JSONObject.toJSONString(data);
            StringEntity entity = new StringEntity(json);
            entity.setContentEncoding(encoding);
            entity.setContentType(contentType);
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() != 200) {
                return getResult(false, "调用服务端异常.");
            }
            HttpEntity res = response.getEntity();
            String resultData = EntityUtils.toString(res);
            System.out.println("从服务端返回结果: " + resultData);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return getResult(false, "");
    }

    public static JSONObject getResult(boolean res, String msg) {
        return new JSONObject(){{
            put("res", res);
            put("msg", msg);
        }};
    }
}

实现一个简单的注册表结构,代表模块的名称和ip

package common;

public class RegisterTable {
    private String name;
    private String ip;

    public RegisterTable(String name, String ip) {
        this.name = name;
        this.ip = ip;
    }

    public String getIp() {
        return ip;
    }
}

3. 注册中心实现

实现注册表管理,增加,删除,查询

package register.list;

import com.alibaba.fastjson.JSONObject;
import common.RegisterTable;
import http.client.HttpClient;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;

@Component
public class RegisterList {
    HashMap<String, ArrayList<RegisterTable>> hashMap = new HashMap<>();

    public JSONObject add(String name, String ip) {
        ArrayList<RegisterTable> list = hashMap.get(name);
        if (list == null) {
            list = new ArrayList<RegisterTable>();
        } else {
            for (RegisterTable table : list) {
                if (table.getIp().equals(ip)) {
                    return HttpClient.getResult(false, "repeat");
                }
            }
        }
        RegisterTable registerTable = new RegisterTable(name,ip);
        list.add(registerTable);
        hashMap.put(name, list);
        return HttpClient.getResult(true, "success");
    }

    public HashMap<String, ArrayList<RegisterTable>> getAll() {
        return hashMap;
    }

    public ArrayList<RegisterTable> getByName(String name) {
        return hashMap.get(name);
    }
}

提供注册接口:

package register.control;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import register.list.RegisterList;

@RestController
@RequestMapping("/register")
public class Register {

    @Autowired
    RegisterList registerList;

    @PostMapping("/register")
    public JSONObject register(@RequestBody JSONObject data) {
        //处理注册逻辑
        return registerList.add(data.getString("name"), data.getString("ip"));
    }

    @GetMapping("/list/{name}")
    public Object getList(@PathVariable(value = "name", required = true) String name) {
        //获取注册列表
        return registerList.getByName(name);
    }

    @GetMapping("/list/all")
    public Object getList() {
        return registerList.getAll();
    }
}

4. provider模块

功能:项目启动后发送注册信息到注册中心模块。

package common.register;

import com.alibaba.fastjson.JSONObject;
import http.client.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class Register implements ApplicationRunner {

    @Value("${model.name}")
    String name;

    @Value("${model.host}")
    String host;

    @Value("${model.port}")
    String port;

    @Value("${register.url}")
    String url;

    //发送注册消息
    @Override
    public void run(ApplicationArguments args) throws Exception {
        register();
    }

    private void register() throws Exception {
        JSONObject data = new JSONObject() {{
            put("name", name);
            put("ip", "http://" + host + ":" + port);
        }};
        HttpClient client = new HttpClient(url, null, null, data);
        client.httpPost();
    }
}

application.yml

server:
  port: 8001

model:
  name: "provider"
  port: "8001"
  host: "127.0.0.1"

register:
  url: "http://localhost:8080/register/register"

另外一个provider也是一样,不过端口不同

5. 测试

启动后:
如何设计一个注册中心?(2)实现注册接口_第2张图片
查询注册表:两个模块都已经注册。
如何设计一个注册中心?(2)实现注册接口_第3张图片

你可能感兴趣的:(java,spring,boot,注册中心)