基础代码
创建learning-online-search
模块,其Parent
为learning-online-parent
,但其父目录为learning-online
。
在pom文件中添加如下依赖:
pom.xml<dependencies> <dependency> <groupId>com.swx</groupId> <artifactId>learning-online-base</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies>
|
创建启动类,在com.swx.system
包下创建启动类SearchApplication
SearchApplication@SpringBootApplication public class SearchApplication { public static void main(String[] args) { SpringApplication.run(SearchApplication.class, args); } }
|
配置文件
bootstrap.ymlspring: application: name: search cloud: nacos: discovery: server-addr: 124.221.23.47:8848 namespace: learning-online-dev group: learning-online-project config: server-addr: 124.221.23.47:8848 namespace: learning-online-dev group: learning-online-project file-extension: yaml refresh-enabled: true shared-configs: - data-id: logging-${spring.profiles.active}.yaml group: learning-online-common refresh: true profiles: active: dev
|
日志配置,在learning-online-search
工程的resources
下创建log4j2-dev.xml
log4j2-dev.xml<?xml version="1.0" encoding="UTF-8"?> <Configuration monitorInterval="180" packages=""> <properties> <property name="logdir">logs</property> <property name="PATTERN">%date{YYYY-MM-dd HH:mm:ss,SSS} %level [%thread][%file:%line] - %msg%n%throwable</property> </properties> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="${PATTERN}"/> </Console>
<RollingFile name="ErrorAppender" fileName="${logdir}/error.log" filePattern="${logdir}/$${date:yyyy-MM-dd}/error.%d{yyyy-MM-dd-HH}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile>
<RollingFile name="DebugAppender" fileName="${logdir}/info.log" filePattern="${logdir}/$${date:yyyy-MM-dd}/info.%d{yyyy-MM-dd-HH}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true" /> </Policies> </RollingFile> <Async name="AsyncAppender" includeLocation="true"> <AppenderRef ref="ErrorAppender"/> <AppenderRef ref="DebugAppender"/> </Async> </Appenders> <Loggers>
<logger name="cn.itcast.wanxinp2p.consumer.mapper" level="DEBUG"> </logger>
<logger name="springfox" level="INFO"> </logger> <logger name="org.apache.http" level="INFO"> </logger> <logger name="com.netflix.discovery" level="INFO"> </logger> <logger name="RocketmqCommon" level="INFO" > </logger> <logger name="RocketmqRemoting" level="INFO" > </logger> <logger name="RocketmqClient" level="WARN"> </logger>
<logger name="org.dromara.hmily" level="WARN"> </logger>
<logger name="org.dromara.hmily.lottery" level="WARN"> </logger>
<logger name="org.dromara.hmily.bonuspoint" level="WARN"> </logger> <Root level="DEBUG" includeLocation="true"> <AppenderRef ref="AsyncAppender"/> <AppenderRef ref="Console"/> <AppenderRef ref="DebugAppender"/> </Root> </Loggers> </Configuration>
|
配置ElastciSearch
创建ElasticSearch的配置文件,其内容如下:
ElasticsearchConfig@Data @Component @RefreshScope @ConfigurationProperties(prefix = "elasticsearch") public class ElasticsearchConfig {
private String hostList;
@Bean public RestHighLevelClient restHighLevelClient() { String[] split = hostList.split(","); HttpHost[] httpHosts = new HttpHost[split.length]; for (int i = 0; i < split.length; i++) { String item = split[i]; httpHosts[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")[1]), "http"); } return new RestHighLevelClient(RestClient.builder(httpHosts)); } }
|