parent
bb19a34444
commit
299195e9a1
@ -1,61 +0,0 @@ |
|||||||
package com.gitee.app.config; |
|
||||||
|
|
||||||
import com.netflix.appinfo.ApplicationInfoManager; |
|
||||||
import com.netflix.appinfo.InstanceInfo; |
|
||||||
import com.netflix.appinfo.MyDataCenterInstanceConfig; |
|
||||||
import com.netflix.discovery.DefaultEurekaClientConfig; |
|
||||||
import com.netflix.discovery.DiscoveryManager; |
|
||||||
import lombok.extern.slf4j.Slf4j; |
|
||||||
import org.springframework.context.annotation.Configuration; |
|
||||||
import org.springframework.context.annotation.PropertySource; |
|
||||||
|
|
||||||
import javax.servlet.ServletContextEvent; |
|
||||||
import javax.servlet.ServletContextListener; |
|
||||||
import java.net.InetAddress; |
|
||||||
import java.net.UnknownHostException; |
|
||||||
|
|
||||||
/** |
|
||||||
* 初始化Eureka Client |
|
||||||
* @author tanghc |
|
||||||
*/ |
|
||||||
@Slf4j |
|
||||||
public class EurekaInitAndRegisterListener implements ServletContextListener { |
|
||||||
|
|
||||||
@Override |
|
||||||
public void contextInitialized(ServletContextEvent sce) { |
|
||||||
// 初始化Eureka Client
|
|
||||||
log.info("Eureka初始化完成,正在注册Eureka Server"); |
|
||||||
DiscoveryManager.getInstance().initComponent(new MyInstanceConfig(), new DefaultEurekaClientConfig()); |
|
||||||
ApplicationInfoManager.getInstance().setInstanceStatus(InstanceInfo.InstanceStatus.UP); |
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* * 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(); |
|
||||||
} |
|
||||||
|
|
||||||
@Configuration |
|
||||||
@PropertySource(value = "classpath:eureka-client.properties") |
|
||||||
public static class AppConfig { |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
public static class MyInstanceConfig extends MyDataCenterInstanceConfig { |
|
||||||
@Override |
|
||||||
public String getHostName(boolean refresh) { |
|
||||||
try { |
|
||||||
return InetAddress.getLocalHost().getHostAddress(); |
|
||||||
} catch (UnknownHostException e) { |
|
||||||
return super.getHostName(refresh); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,15 +1,44 @@ |
|||||||
package com.gitee.app.config; |
package com.gitee.app.config; |
||||||
|
|
||||||
|
import com.alibaba.nacos.api.annotation.NacosInjected; |
||||||
|
import com.alibaba.nacos.api.annotation.NacosProperties; |
||||||
|
import com.alibaba.nacos.api.exception.NacosException; |
||||||
|
import com.alibaba.nacos.api.naming.NamingService; |
||||||
|
import com.alibaba.nacos.client.naming.utils.NetUtils; |
||||||
|
import com.alibaba.nacos.spring.context.annotation.discovery.EnableNacosDiscovery; |
||||||
import com.gitee.sop.servercommon.bean.ServiceConfig; |
import com.gitee.sop.servercommon.bean.ServiceConfig; |
||||||
import com.gitee.sop.servercommon.configuration.SpringMvcServiceConfiguration; |
import com.gitee.sop.servercommon.configuration.SpringMvcServiceConfiguration; |
||||||
|
import lombok.extern.slf4j.Slf4j; |
||||||
|
|
||||||
/** |
/** |
||||||
* 使用支付宝开放平台功能 |
* 使用支付宝开放平台功能 |
||||||
* |
* |
||||||
* @author tanghc |
* @author tanghc |
||||||
*/ |
*/ |
||||||
|
@Slf4j |
||||||
|
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) |
||||||
public class OpenServiceConfig extends SpringMvcServiceConfiguration { |
public class OpenServiceConfig extends SpringMvcServiceConfiguration { |
||||||
static { |
static { |
||||||
ServiceConfig.getInstance().setDefaultVersion("1.0"); |
ServiceConfig.getInstance().setDefaultVersion("1.0"); |
||||||
} |
} |
||||||
|
|
||||||
|
// 这两个参数需要从配置文件中获取
|
||||||
|
private String serviceId = "sop-springmvc"; |
||||||
|
private int port = 2223; |
||||||
|
|
||||||
|
@NacosInjected |
||||||
|
private NamingService namingService; |
||||||
|
|
||||||
|
@Override |
||||||
|
protected void doAfter() { |
||||||
|
super.doAfter(); |
||||||
|
try { |
||||||
|
String ip = NetUtils.localIP(); |
||||||
|
namingService.registerInstance(serviceId, ip, port); |
||||||
|
log.info("注册到nacos, serviceId:{}, ip:{}, port:{}", serviceId, ip, port); |
||||||
|
} catch (NacosException e) { |
||||||
|
log.error("注册nacos失败", e); |
||||||
|
throw new RuntimeException("注册nacos失败", e); |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
|
@ -1,27 +0,0 @@ |
|||||||
# tomcat端口,根据实际情况填 |
|
||||||
server.port=2223 |
|
||||||
# 应用名称serviceId,根据实际情况填 |
|
||||||
spring.application.name=sop-springmvc |
|
||||||
# 注册中心地址,根据实际情况填 |
|
||||||
eureka.url=http://localhost:1111/eureka/ |
|
||||||
# zookeeper地址,根据实际情况填 |
|
||||||
spring.cloud.zookeeper.connect-string=localhost:2181 |
|
||||||
|
|
||||||
# ----------- 以下内容不用改 ----------- |
|
||||||
|
|
||||||
# 控制是否注册自身到eureka中,本项目虽然不对外提供服务,但需要Eureka监控,在Eureka列表上显示 |
|
||||||
eureka.registration.enabled=true |
|
||||||
# eureka相关配置 |
|
||||||
# 默认为true,以实现更好的基于区域的负载平衡。 |
|
||||||
eureka.preferSameZone=true |
|
||||||
# 是否要使用基于DNS的查找来确定其他eureka服务器 |
|
||||||
eureka.shouldUseDns=false |
|
||||||
# 由于shouldUseDns为false,因此我们使用以下属性来明确指定到eureka服务器的路由(eureka Server地址) |
|
||||||
eureka.serviceUrl.default=${eureka.url} |
|
||||||
eureka.decoderName=JacksonJson |
|
||||||
# 客户识别此服务的虚拟主机名,这里指的是eureka服务本身 |
|
||||||
eureka.vipAddress=${spring.application.name} |
|
||||||
#服务指定应用名,这里指的是eureka服务本身 |
|
||||||
eureka.name=${spring.application.name} |
|
||||||
#服务将被识别并将提供请求的端口 |
|
||||||
eureka.port=${server.port} |
|
Loading…
Reference in new issue