服务启动自动注册到zookeeper (六)
cookqq ›博客列表 ›docker+node.js+zookeeper构建微服务

服务启动自动注册到zookeeper (六)

2018-08-23 11:35:12.0|分类: docker+node.js+zookeeper构建微服务|浏览量: 1490

摘要: 服务启动的时候自动注册到zookeeper,而不需要进行其他任何操作。实现注册自动化。


项目registration-service改变的地方

blob.png


application.properties配置服务的ip和端口号,已经zookeeper服务器地址

server.address=127.0.0.1

server.port=8080

registry.servers=127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183


registry.servers是自己定义的,需要进行配置

RegistryConfig类


package com.cookqq.registry;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.boot.context.properties.ConfigurationProperties;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;


@Configuration

@ConfigurationProperties(prefix="registry")

public class RegistryConfig {


private static Logger logger = LoggerFactory.getLogger(RegistryConfig.class);

private String servers;


@Bean

public ServiceRegistry serviceRegistry(){

logger.error("RegistryConfig is coming...");

return new ServiceRegistryImpl(servers);

}

public void setServers(String servers) {

this.servers = servers;

}

}



web启动监听器WebListenter


package com.cookqq.registry;


import java.util.Map;


import javax.servlet.ServletContext;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;


import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import org.springframework.web.method.HandlerMethod;

import org.springframework.web.servlet.mvc.method.RequestMappingInfo;

import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;


@Component

public class WebListenter implements ServletContextListener {


private static Logger logger = LoggerFactory.getLogger(WebListenter.class);

@Value("${server.address}")

private String serverAddress;

@Value("${server.port}")

private String serverPort;

@Autowired

private ServiceRegistry serviceRegistry;

@Override

public void contextInitialized(ServletContextEvent sce) {

logger.error("WebListenter is init .... ");

ServletContext servletContext = sce.getServletContext();

WebApplicationContext applicationContext =

WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);

//RequestMappingHandlerMapping封装了所有@RequestMapping方法的相关信息

Map<RequestMappingInfo, HandlerMethod> infoMap = mapping.getHandlerMethods();

for(RequestMappingInfo info : infoMap.keySet()){

String serverName = info.getName();

if(serverName!=null){

serviceRegistry.register(serverName, serverAddress+":"+serverPort);

}

}

}


@Override

public void contextDestroyed(ServletContextEvent sce) {


}


}



项目render-services增加依赖

<dependencies>

    <dependency>

      <groupId>org.springframework.boot</groupId>

      <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

     <dependency>

      <groupId>com.cookqq</groupId>

      <artifactId>registration-service</artifactId>

      <version>0.0.1-SNAPSHOT</version>

    </dependency>

  </dependencies>



启动render-services项目,默认端口是8080

修改application.properties端口号server.port=8081,再次启动项目。


查看zookeeper节点,启动了两个服务

blob.png

查看服务的ip和端口号

blob.png



一键分享文章

分类列表

  • • struts源码分析
  • • flink
  • • struts
  • • redis
  • • kafka
  • • ubuntu
  • • zookeeper
  • • hadoop
  • • activiti
  • • linux
  • • 成长
  • • NIO
  • • 关键词提取
  • • mysql
  • • android studio
  • • zabbix
  • • 云计算
  • • mahout
  • • jmeter
  • • hive
  • • ActiveMQ
  • • lucene
  • • MongoDB
  • • netty
  • • flume
  • • 我遇到的问题
  • • GRUB
  • • nginx
  • • 大家好的文章
  • • android
  • • tomcat
  • • Python
  • • luke
  • • android源码编译
  • • 安全
  • • MPAndroidChart
  • • swing
  • • POI
  • • powerdesigner
  • • jquery
  • • html
  • • java
  • • eclipse
  • • shell
  • • jvm
  • • highcharts
  • • 设计模式
  • • 列式数据库
  • • spring cloud
  • • docker+node.js+zookeeper构建微服务
版权所有 cookqq 感谢访问 支持开源 京ICP备15030920号
CopyRight 2015-2018 cookqq.com All Right Reserved.