怎么把非springboot项目集成eureka

发布时间:2021-11-16 14:29:24 作者:iii
来源:亿速云 阅读:154

这篇文章主要介绍“怎么把非springboot项目集成eureka”,在日常操作中,相信很多人在怎么把非springboot项目集成eureka问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么把非springboot项目集成eureka”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

1  背景

随着SOA架构的演进,越来越多的服务商需要提供一种通用的可以动态伸缩的基础架构,

而对嘻「」来说,亦是如此。并且随着TPS的不断增加,more and more endpoint 需要整合这种分布式基础架构。这里有点类似当年google的Bigtable 的论文刚发表时的案例,对不同业务,或者相同业务不同功能,在或者相同功能实现节点拓展,分布式处理。(要实现分布式处理能力的系统必然伴随这分布式场景的一些问题,这里不做讨论。

2 目的说明

类似Dubbo分布式服务治理,不同的微服务需要依赖注册中心做服务治理与管控,springCloud 也是这样,作为微服务的生态,不同的组件负责不同的功能(例如archaius 做配置and eureka 做服务PUBSUB等...)。

3 方案对比

3.1 directly to register

简单的说.通过分析源码发现DiscoveryClient对象是客户端启动加载的核心类,

怎么把非springboot项目集成eureka

它创建的时候就会去注册,如下

怎么把非springboot项目集成eureka

在restemplate 调用的过程中需要先通过应用层通信拿到服务器对应spring.application.name 为vipaddress的InstanceInfo,instanceOf里面维护了对应的你将要调用的服务器或者服务如下:

怎么把非springboot项目集成eureka

可能会在这里拿不出来实例信息,最后报没有对应服务实例,以上来自Applications Class,

源码后文会详细分析,这里不过多分析只解决问题

******  4.2  The Error for Register Process******

这里按照官网案例搭建注册环境,可能会出现一切准备就绪然注册不上去的情况。

走一遍注册流程如下:

怎么把非springboot项目集成eureka 怎么把非springboot项目集成eureka

怎么把非springboot项目集成eureka

Archaius会默认加载classpath下的config.properties文件作为当前内存资源配置,也可以改,通过archaius.configurationSource.defaultFileName,当然既然是动态的肯定也是可以做多数据源动态处理,具体不详细说明了

具体配置意义先不考虑,先为了解决问题考虑环境

******    4.3  eureka/apps for configuration******

手动注册这一截是没有的 显示empty

所以我们需要把这一截加上去,

代码如下:

int port = instanceInfo.getPort();
Map<String, String> maps = Maps.newHashMap();
maps.put("management.port", String.valueOf(port));
//activity-manager:dev-10.0.2.17:8010

applicationInfoManager.getInstance().registerAppMetadata(maps);

还有一个细节问题:

Archaius1Utils.initConfig("eureka-client");

这个是在查看报错日志的时候报的,为了更好的兼容框架需要解决,大概意思是说缺少这个配置 怎么把非springboot项目集成eureka

在加载此类实现类的时候就会一起初始化该类。很明显组件已经帮我们做了,我们只需要提供对应配置文件即可 怎么把非springboot项目集成eureka

依赖为

怎么把非springboot项目集成eureka

怎么把非springboot项目集成eureka

这里需要注意的两点:

怎么把非springboot项目集成eureka 怎么把非springboot项目集成eureka

怎么把非springboot项目集成eureka

继续DEBUG会看到:

怎么把非springboot项目集成eureka

这里开始递归调用拦截器,也就是我们在启动时候放进去的拦截器会在这里调用

怎么把非springboot项目集成eureka

当然这里有个细节

怎么把非springboot项目集成eureka

这里我们需要关注两个点

1 通过服务serviceId拿到核心instanceInfo ,下面会解释

2 通过对应服务信息选择对应服务调用

分别对应第一二行代码

继续debug

怎么把非springboot项目集成eureka

这里需要通过一个serviceId加载一个LloadBalancer,代码如下 怎么把非springboot项目集成eureka

Spring 默认会调用以上工厂加载,所以我们点进去会看到 怎么把非springboot项目集成eureka

这里也需要注意两点:

1 我们这个lloadbalancer是用的时候才去加载,换句话说懒加载

2 这里有个缓存的 操作 第一个if为false就会返回缓存,换句话说只会创建一次

然后我们进去

怎么把非springboot项目集成eureka

这些东西。

怎么把非springboot项目集成eureka

怎么把非springboot项目集成eureka

以上的clientConfig 就是我们在代码设置进去的EurekaClientConfigBean

现在还没有上面参数,不要紧因为还没有开始初始化。

继续加载完之后开始加载

怎么把非springboot项目集成eureka

图大概意思就是先加载父类baseLoadbalancer然后加载自己

restOfInit方法就是核心加载方法

怎么把非springboot项目集成eureka

上图标红的是比较重要的方法

怎么把非springboot项目集成eureka

这里比较重点的如上标红处

进去看到:

怎么把非springboot项目集成eureka 怎么把非springboot项目集成eureka 怎么把非springboot项目集成eureka

他就会走到这里,这是什么呢?

怎么把非springboot项目集成eureka

对应一下会从visualHostNameAppMap 里面通过vipAddress那到List<InstanceOf>,成功拿到instanceInfo之后回到刚开始进源码的地方就会返回一个包含对应instanceInfo的loadBalancer,

怎么把非springboot项目集成eureka 怎么把非springboot项目集成eureka

总结一下:

负载均衡实现原理概述为根据配置加载负载均衡拦截器,用户客户端调用遍历处理,通过servceId通过HTTP拿到对应instanceInfo(和dubbo流程差不多,第一次都需要去拿之后缓存在本地。默认90秒把重新调用一次拉取信息)多个需要依算法选取一个然后进行远程调用。

代码如下:

package com.kili.lipapay.nmc.common;

import com.google.common.collect.Maps;
import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.appinfo.providers.EurekaConfigBasedInstanceInfoProvider;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.EurekaClient;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;
import java.util.Map;
import java.util.Properties;


@Component
class SimpleEurakeService {
    private static final DynamicPropertyFactory configInstance = com.netflix.config.DynamicPropertyFactory
            .getInstance();

    public void registerWithEureka() throws IOException {

        ApplicationInfoManager applicationInfoManager = null;
        EurakeInstanceConfig config = null;
        InstanceInfo instanceInfo = null;
        // Register with Eureka
        if (applicationInfoManager == null) {
            config = new EurakeInstanceConfig();
            instanceInfo = new EurekaConfigBasedInstanceInfoProvider(config).get();
            applicationInfoManager = new ApplicationInfoManager(config, instanceInfo);

        }
       // Archaius1Utils.initConfig("eureka-client");
        Properties properties = new Properties();
        InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("config.properties");
        properties.load(inputStream);
        properties.setProperty("eureka.ipAddr", InetAddress.getLocalHost().getHostAddress());
        String instanceId = applicationInfoManager.getInfo().getAppName() + ":dev-" + properties.getProperty("eureka.ipAddr") + ":" + properties.getProperty("eureka.port");
        properties.setProperty("eureka.instanceId", instanceId);
        ConfigurationManager.loadProperties(properties);
        int port = instanceInfo.getPort();
        Map<String, String> maps = Maps.newHashMap();
        maps.put("management.port", String.valueOf(port));
        //activity-manager:dev-10.0.2.17:8010
        applicationInfoManager.getInstance().registerAppMetadata(maps);
        applicationInfoManager.getInstance().setInstanceStatus(
                InstanceInfo.InstanceStatus.UP);
        EurekaClient eurekaClient = new DiscoveryClient(applicationInfoManager, new DefaultEurekaClientConfig());
        String vipAddress = configInstance.getStringProperty(
                "eureka.vipAddress", "unknown").get();
        InstanceInfo nextServerInfo = null;
        while (nextServerInfo == null) {

            try {
                nextServerInfo = eurekaClient
                        .getNextServerFromEureka(vipAddress, false);
            } catch (Throwable e) {
                System.out
                        .println("Waiting for service to register with eureka..");
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    }


    public void unRegisterWithEureka() {
        // Un register from eureka.
        DiscoveryManager.getInstance().shutdownComponent();
    }


    private void processRequest(final Socket s) {
        try {
            BufferedReader rd = new BufferedReader(new InputStreamReader(
                    s.getInputStream()));
            String line = rd.readLine();
            if (line != null) {
                System.out.println("Received the request from the client.");
            }
            PrintStream out = new PrintStream(s.getOutputStream());
            System.out.println("Sending the response to the client...");
            out.println("Reponse at " + new Date());
        } catch (Throwable e) {
            System.err.println("Error processing requests");
        } finally {
            if (s != null) {
                try {
                    s.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    @PostConstruct
    private void init() throws IOException {
        SimpleEurakeService sampleEurekaService = new SimpleEurakeService();
        sampleEurekaService.registerWithEureka();
    }

配置如下:

# note that for a purely client usage (e.g. only used to get information about other services,
# there is no need for registration. This property applies to the singleton DiscoveryClient so
# if you run a server that is both a service provider and also a service consumer,
# then don't set this property to false.
#eureka.shouldEnforceRegistrationAtInit=false
## configuration related to reaching the eureka serversSS
eureka.client.service-url.defaultZone=http://localhost:7025/eureka
eureka.serviceUrl.default=http://localhost:7025/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=config-server
eureka.region=default
spring.application.name=sampleservice

#Name of the application to be identified by other services

eureka.name=sampleservice


#Virtual host name by which the clients identifies this service
eureka.vipAddress=sampleservice

#The port where the service will be running and serving requests
eureka.port=8080

#For eureka clients running in eureka server, it needs to connect to servers in other zones
eureka.preferSameZone=false

#Change this if you want to use a DNS based lookup for determining other eureka servers. For example
#of specifying the DNS entries, check the eureka-client-test.properties, eureka-client-prod.properties
eureka.shouldUseDns=false

eureka.us-east-1.availabilityZones=default
erueka.registration.enabled=true

到此,关于“怎么把非springboot项目集成eureka”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. springboot集成swagger-UI 开发API项目
  2. SpringBoot非Web项目运行配置的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

springboot archaius

上一篇:Angular知识点有哪些

下一篇:mysql5.1怎样升级到5.6

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》