mirror of
https://gitee.com/durcframework/SOP.git
synced 2025-08-11 21:57:56 +08:00
2.0
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
<java-version>1.8</java-version>
|
||||
<org.springframework-version>5.0.7.RELEASE</org.springframework-version>
|
||||
<org.aspectj-version>1.6.10</org.aspectj-version>
|
||||
<org.slf4j-version>1.6.6</org.slf4j-version>
|
||||
<org.slf4j-version>1.7.25</org.slf4j-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -22,11 +22,11 @@
|
||||
<artifactId>sop-service-common</artifactId>
|
||||
<version>2.0.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<!-- eureka 服务发现 -->
|
||||
<!-- nacos -->
|
||||
<dependency>
|
||||
<groupId>com.netflix.eureka</groupId>
|
||||
<artifactId>eureka-client</artifactId>
|
||||
<version>1.7.0</version>
|
||||
<groupId>com.alibaba.nacos</groupId>
|
||||
<artifactId>nacos-spring-context</artifactId>
|
||||
<version>0.3.3</version>
|
||||
</dependency>
|
||||
|
||||
<!-- sop接入依赖 end -->
|
||||
|
@@ -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;
|
||||
|
||||
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.configuration.SpringMvcServiceConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* 使用支付宝开放平台功能
|
||||
*
|
||||
* @author tanghc
|
||||
*/
|
||||
@Slf4j
|
||||
@EnableNacosDiscovery(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848"))
|
||||
public class OpenServiceConfig extends SpringMvcServiceConfiguration {
|
||||
static {
|
||||
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}
|
@@ -14,10 +14,6 @@
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<listener>
|
||||
<listener-class>com.gitee.app.config.EurekaInitAndRegisterListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Processes application requests -->
|
||||
<servlet>
|
||||
<servlet-name>appServlet</servlet-name>
|
||||
|
Reference in New Issue
Block a user