非Spring Boot Web项目 注册节点到Eureka Server并提供服务 - Go语言中文社区

非Spring Boot Web项目 注册节点到Eureka Server并提供服务


相信有很多团队在老Web项目(zookeeper,dubbo,tomcat)想要过渡到新的Eureka注册管理的Spring Boot都会遇到这样一个问题,新项目想调用老项目提供的服务,或者不想采用Spring Boot 而是直接想使用Eureka 替换掉原有的zookeeper和dubbo,那怎么办0.0 能不能将老项目注册到Eureka Server?

1.项目jar依赖:
gradle:

    compile "com.netflix.eureka:eureka-client:1.4.12"

maven:

<dependency>
    <groupId>com.netflix.eureka</groupId>
    <artifactId>eureka-client</artifactId>
    <version>1.4.12</version>
</dependency>

此处主要依赖的是Netflix的eureka-client jar包(spring boot也是对它的一个简单封装)要注意这里的jar包版本要与Spring Boot项目依赖的eureka-client jar包版本一致 不然可能不兼容
下面是该jar包所有的依赖(有jar包冲突的要处理解决一下~)
eureka-client

2.添加Listener用于将节点注册到Eureka Server

package com.kowalski.web.eureka;

import com.netflix.appinfo.ApplicationInfoManager;
import com.netflix.appinfo.InstanceInfo;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.discovery.DefaultEurekaClientConfig;
import com.netflix.discovery.DiscoveryManager;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * Created by Kowalski on 2017/5/18
 * Updated by Kowalski on 2017/5/18
 */
@Slf4j
public class EurekaInitAndRegisterListener implements ServletContextListener {

    private static final DynamicPropertyFactory configInstance = DynamicPropertyFactory
            .getInstance();

    /**
     * * Notification that the web application initialization
     * * process is starting.
     * * All ServletContextListeners are notified of context
     * * initialization before any filter or servlet in the web
     * * application is initialized.
     *
     * @param sce
     */
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        /**设置被读取配置文件名称  默认config.properties*/
        Properties properties = new Properties();
        properties.setProperty("archaius.configurationSource.defaultFileName", "config.properties");
        System.setProperties(properties);
        /**注册*/
        registerWithEureka();
    }

    public void registerWithEureka() {
        /**加载本地配置文件 根据配置初始化这台 Eureka Application Service 并且注册到 Eureka Server*/
        DiscoveryManager.getInstance().initComponent(
                new MyInstanceConfig(),
                new DefaultEurekaClientConfig());

        /**本台 Application Service 已启动,准备好侍服网络请求*/
        ApplicationInfoManager.getInstance().setInstanceStatus(
                InstanceInfo.InstanceStatus.UP);

        log.info("o2o eureka Application Service initing and registering");

        /**Application Service 的 Eureka Server 初始化以及注册是异步的,需要一段时间 此处等待初始化及注册成功 可去除*/
//        String vipAddress = configInstance.getStringProperty(
//                "eureka.vipAddress", "o2o").get();
//        InstanceInfo nextServerInfo = null;
//        while (nextServerInfo == null) {
//            try {
//                nextServerInfo = DiscoveryManager.getInstance()
//                        .getDiscoveryClient()
//                        .getNextServerFromEureka(vipAddress, false);
//            } catch (Throwable e) {
//                log.info("Waiting for service to register with eureka..");
//                try {
//                    Thread.sleep(10000);
//                } catch (InterruptedException e1) {
//                    e1.printStackTrace();
//                }
//
//
//            }
//        }
//        log.info("Service started and ready to process requests..");
    }

    /**
     * * Notification that the servlet context is about to be shut down.
     * * All servlets and filters have been destroy()ed before any
     * * ServletContextListeners are notified of context
     * * destruction.
     *
     * @param sce
     */
    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        DiscoveryManager.getInstance().shutdownComponent();
    }
}

在web.xml中添加listener,项目在启动时进行注册到eureka Server(异步)

3.Eureka配置

在配置目录下添加config.properties文件(默认读取的文件名)添加配置:

###Eureka Client configuration for Sample Eureka Service


#Properties based configuration for eureka client. The properties specified here is mostly what the users
#need to change. All of these can be specified as a java system property with -D option (eg)-Deureka.region=us-east-1
#For additional tuning options refer <url to go here>

#部署应用程序的区域
# - 对于AWS指定一个AWS区域
#  - 对于其他数据中心,指定一个指示该区域的任意字符串。
# 这里主要指定美国东部D
eureka.region=default

#服务指定应用名,这里指的是eureka服务本身(相当于boot中的app.name)
eureka.name=o2oProject

#客户识别此服务的虚拟主机名,这里指的是eureka服务本身(相当于boot中的serviceId)
eureka.vipAddress=o2o

#服务将被识别并将提供请求的端口(web服务部署的tomcat端口)
eureka.port=8810


#设置为false,因为该配置适用于eureka服务器本身的eureka客户端。
#在eureka服务器中运行的eureka客户端需要连接到其他区域中的服务器。
#对于其他应用程序,不应设置(默认为true),以实现更好的基于区域的负载平衡。
eureka.preferSameZone=true

#如果要使用基于DNS的查找来确定其他eureka服务器(请参见下面的示例),请更改此选项
eureka.shouldUseDns=false
eureka.us-east-1.availabilityZones=default
#由于shouldUseDns为false,因此我们使用以下属性来明确指定到eureka服务器的路由(eureka Server地址)
eureka.serviceUrl.default=http://localhost:7003/eureka/

以上是一些基本配置,想了解更多配置可以去看com.netflix.appinfo.EurekaInstanceConfig
这里有时候会有个问题,项目会报找不到eureka Server的hostName的错,在boot中有个preferIpAdress的配置,配置后会默认使用ip访问而不是主机名hostName,在原生的eureka 中写preferIpAdress配置不会被解析读取,因此我们仿照boot的该配置做法:

package com.kowalski.web.eureka;

import com.netflix.appinfo.MyDataCenterInstanceConfig;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * Created by kowalski on 2017/5/25
 */
public class MyInstanceConfig extends MyDataCenterInstanceConfig {
    @Override
    public String getHostName(boolean refresh) {
        try {
            return InetAddress.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            return super.getHostName(refresh);
        }
    }
}

到这里,在tomcat启动的时候,该节点就会注册到Eureka Server上并供其他项目正常调度,同样的,也不影响原来的zk,dubbo,调用走的都是Http请求,我们目前使用Spring web client中的Restemplate,便于对所有Http请求的统一管理(使用HttpClient连接池,统一的中文乱码处理等等),使用feignClient也可以。

4.简易客户端demo
好久没更新了,这个还是以前用的了,很多细节也记不起来了,奉上刚写的demo供大家学习之用~
https://pan.baidu.com/s/1clbu0pkdoNjgxKWDDogXFw

Email:995517265@qq.com

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_32193151/article/details/72559783
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-04-19 17:30:53
  • 阅读 ( 1707 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢