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

非Spring Boot Web项目 注册节点到Eureka Server并提供服务 - CSDN博客 非Spring Boot Web项目 注册节点到Eureka Server并提供服务 -evernote 非Spring Boot Web项目 注册节点到Eureka Server并提供服务 - CSDN博客

step1: 引用 eureka client的包,要注意这里的jar包版本要与Spring Boot项目依赖的eureka-client jar包版本一致 不然可能不兼容

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

step2: 写监听代码注册到eureka server

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 {

    /**
     * * 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) {
        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 初始化以及注册是异步的,需要一段时间 此处等待初始化及注册成功 可去除*/
    // private static final DynamicPropertyFactory configInstance = DynamicPropertyFactory
    //         .getInstance();
    // 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();
    }
}

step3: 重写配置文件。项目会报找不到eureka Server的hostName的错,在boot中有个preferIpAdress的配置,配置后会默认使用ip访问而不是主机名hostName,在原生的eureka 中写preferIpAdress配置不会被解析读取,因此我们仿照boot的该配置做法:

step4: 在web.xml中添加监听器配置

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

特别注意: eureka.name 是服务在服务注册中心的页面中展示的名字(Application一列显示的名字) eureka.vipAddress 是调用方调用时的服务名字,即FeignClient注解位置填写的名字@FeignClient(name = "MVC-SERVICE") fegin调用使用的是eureka.vipAddress,而在spring cloud中的网关服务,即zuul服务,则使用的是eureka.name来访问,所以两个尽可能一致

Last updated

Was this helpful?