2018-08-23 11:35:12.0|分类: docker+node.js+zookeeper构建微服务|浏览量: 1332
项目registration-service改变的地方 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节点,启动了两个服务 查看服务的ip和端口号 |