elk-eureka-server做爲其餘三個項目的服務註冊中心java
elk-kafka-client調用elk-kafka-server,elk-kafka-server再調用elk-kafka-server1git
pom.xml內容以下github
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.carry.elk</groupId> 7 <artifactId>elk-eureka-server</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>elk-eureka-server</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>2.0.3.RELEASE</version> 18 <relativePath/> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 26 </properties> 27 28 <dependencies> 29 <dependency> 30 <groupId>org.springframework.cloud</groupId> 31 <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> 32 </dependency> 33 34 <dependency> 35 <groupId>org.springframework.boot</groupId> 36 <artifactId>spring-boot-starter-test</artifactId> 37 <scope>test</scope> 38 </dependency> 39 </dependencies> 40 41 <dependencyManagement> 42 <dependencies> 43 <dependency> 44 <groupId>org.springframework.cloud</groupId> 45 <artifactId>spring-cloud-dependencies</artifactId> 46 <version>${spring-cloud.version}</version> 47 <type>pom</type> 48 <scope>import</scope> 49 </dependency> 50 </dependencies> 51 </dependencyManagement> 52 53 <build> 54 <plugins> 55 <plugin> 56 <groupId>org.springframework.boot</groupId> 57 <artifactId>spring-boot-maven-plugin</artifactId> 58 </plugin> 59 </plugins> 60 </build> 61 62 63 </project>
application.yml內容以下web
1 server: 2 port: 8761 3 eureka: 4 instance: 5 hostname: localhost 6 client: 7 registerWithEureka: false 8 fetchRegistry: false 9 serviceUrl: 10 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
在啓動類ElkEurekaServerApplication上加上註解@EnableEurekaServerspring
pom.xml內容以下apache
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.carry.elk</groupId> 8 <artifactId>elk-kafka-client</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 <packaging>jar</packaging> 11 12 <name>elk-kafka-client</name> 13 <description>Demo project for Spring Boot</description> 14 15 <parent> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-parent</artifactId> 18 <version>2.0.3.RELEASE</version> 19 <relativePath /> <!-- lookup parent from repository --> 20 </parent> 21 22 <properties> 23 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 24 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 25 <java.version>1.8</java.version> 26 <spring-cloud.version>Finchley.RELEASE</spring-cloud.version> 27 </properties> 28 29 <dependencies> 30 <dependency> 31 <groupId>org.springframework.boot</groupId> 32 <artifactId>spring-boot-starter-web</artifactId> 33 </dependency> 34 <dependency> 35 <groupId>org.springframework.cloud</groupId> 36 <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> 37 </dependency> 38 <dependency> 39 <groupId>org.springframework.cloud</groupId> 40 <artifactId>spring-cloud-starter-openfeign</artifactId> 41 </dependency> 42 <dependency> 43 <groupId>org.springframework.cloud</groupId> 44 <artifactId>spring-cloud-starter-sleuth</artifactId> 45 </dependency> 46 <dependency> 47 <groupId>net.logstash.logback</groupId> 48 <artifactId>logstash-logback-encoder</artifactId> 49 <version>4.11</version> 50 </dependency> 51 <dependency> 52 <groupId>com.github.danielwegener</groupId> 53 <artifactId>logback-kafka-appender</artifactId> 54 <version>0.1.0</version> 55 <scope>runtime</scope> 56 </dependency> 57 58 <dependency> 59 <groupId>org.springframework.boot</groupId> 60 <artifactId>spring-boot-devtools</artifactId> 61 <scope>runtime</scope> 62 </dependency> 63 <dependency> 64 <groupId>org.springframework.boot</groupId> 65 <artifactId>spring-boot-starter-test</artifactId> 66 <scope>test</scope> 67 </dependency> 68 </dependencies> 69 70 <dependencyManagement> 71 <dependencies> 72 <dependency> 73 <groupId>org.springframework.cloud</groupId> 74 <artifactId>spring-cloud-dependencies</artifactId> 75 <version>${spring-cloud.version}</version> 76 <type>pom</type> 77 <scope>import</scope> 78 </dependency> 79 </dependencies> 80 </dependencyManagement> 81 82 <build> 83 <plugins> 84 <plugin> 85 <groupId>org.springframework.boot</groupId> 86 <artifactId>spring-boot-maven-plugin</artifactId> 87 </plugin> 88 </plugins> 89 </build> 90 91 92 </project>
application.yml內容以下bootstrap
1 server: 2 port: 8080 3 spring: 4 application: 5 name: ELK-KAFKA-CLIENT 6 devtools: 7 restart: 8 enabled: true 9 eureka: 10 client: 11 serviceUrl: 12 defaultZone: http://localhost:8761/eureka/
在resources目錄下新增logback.xml文件,內容以下api
1 <?xml version="1.0" encoding="UTF-8"?> 2 <configuration debug="false" scan="true" 3 scanPeriod="1 seconds"> 4 <include 5 resource="org/springframework/boot/logging/logback/base.xml" /> 6 <!-- <jmxConfigurator/> --> 7 <contextName>logback</contextName> 8 9 <property name="log.path" value="\logs\logback.log" /> 10 11 <property name="log.pattern" 12 value="%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID} --- traceId:[%X{mdc_trace_id}] [%15.15t] %-40.40logger{39} : %m%n" /> 13 14 <appender name="file" 15 class="ch.qos.logback.core.rolling.RollingFileAppender"> 16 <file>${log.path}</file> 17 18 <encoder> 19 <pattern>${log.pattern}</pattern> 20 </encoder> 21 22 <rollingPolicy 23 class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> 24 25 <fileNamePattern>info-%d{yyyy-MM-dd}-%i.log 26 </fileNamePattern> 27 28 <timeBasedFileNamingAndTriggeringPolicy 29 class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> 30 31 <maxFileSize>10MB</maxFileSize> 32 </timeBasedFileNamingAndTriggeringPolicy> 33 <maxHistory>10</maxHistory> 34 </rollingPolicy> 35 36 </appender> 37 38 <appender name="KafkaAppender" 39 class="com.github.danielwegener.logback.kafka.KafkaAppender"> 40 <encoder 41 class="com.github.danielwegener.logback.kafka.encoding.LayoutKafkaMessageEncoder"> 42 <layout class="net.logstash.logback.layout.LogstashLayout"> 43 <includeContext>true</includeContext> 44 <includeCallerData>true</includeCallerData> 45 <customFields>{"system":"kafka"}</customFields> 46 <fieldNames 47 class="net.logstash.logback.fieldnames.ShortenedFieldNames" /> 48 </layout> 49 <charset>UTF-8</charset> 50 </encoder> 51 <!--kafka topic 須要與配置文件裏面的topic一致 不然kafka會沉默並鄙視你 --> 52 <topic>applog</topic> 53 <keyingStrategy 54 class="com.github.danielwegener.logback.kafka.keying.HostNameKeyingStrategy" /> 55 <deliveryStrategy 56 class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" /> 57 <producerConfig>bootstrap.servers=192.168.68.110:9092,192.168.68.111:9092,192.168.68.112:9092</producerConfig> 58 </appender> 59 60 <!-- <logger name="Application_ERROR"> 61 <appender-ref ref="KafkaAppender" /> 62 </logger> --> 63 64 <root level="info"> 65 <appender-ref ref="KafkaAppender" /> 66 </root> 67 68 </configuration>
新增Feign接口CilentFeignApi,內容以下瀏覽器
1 package com.carry.elk; 2 3 import org.springframework.cloud.openfeign.FeignClient; 4 import org.springframework.http.MediaType; 5 import org.springframework.web.bind.annotation.RequestMapping; 6 import org.springframework.web.bind.annotation.RequestMethod; 7 8 @FeignClient("ELK-KAFKA-SERVER") 9 public interface CilentFeignApi { 10 11 @RequestMapping(value = "/server", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) 12 public String getString(); 13 14 }
新增ClientController,內容以下springboot
1 package com.carry.elk; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 public class ClientController { 11 12 // private final static Logger logger = LoggerFactory.getLogger("Application_ERROR"); 13 14 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 15 16 @Autowired 17 CilentFeignApi api; 18 19 @GetMapping("/client") 20 public String getString() { 21 logger.info("開始調用服務端Server"); 22 return api.getString(); 23 } 24 }
在啓動類ElkKafkaClientApplication上加上註解@EnableEurekaClient和@EnableFeignClients
此項目跟elk-kafka-client項目類似只需做一下修改
application.yml文件中修改端口號爲8081,application.name爲ELK-KAFKA-SERVER
Feign接口中註解修改成@FeignClient("ELK-KAFKA-SERVER1")
Controller內容修改成
1 package com.carry.elk; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.GetMapping; 7 import org.springframework.web.bind.annotation.RestController; 8 9 @RestController 10 public class ServerController { 11 12 // private final static Logger logger = LoggerFactory.getLogger("Application_ERROR"); 13 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 14 15 @Autowired 16 ServerFeignApi api; 17 18 @GetMapping("/server") 19 public String getString() { 20 logger.info("接收客戶端調用並調用服務端Server1"); 21 return api.getString(); 22 } 23 }
此項目不須要調用其餘項目因此去掉Feign的支持
application.yml文件中修改端口號爲8082,application.name爲ELK-KAFKA-SERVER1
Controller內容修改成
1 package com.carry.elk; 2 3 import org.slf4j.Logger; 4 import org.slf4j.LoggerFactory; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 @RestController 9 public class Server1Controller { 10 11 // private final static Logger logger = LoggerFactory.getLogger("Application_ERROR"); 12 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 13 14 @GetMapping("/server") 15 public String getString() { 16 logger.info("接收服務端server的調用"); 17 return "I am server1."; 18 } 19 20 }
項目已寫完讓咱們來看看結果吧
首先啓動elk-eureka-server,而後依次啓動elk-kafka-server1,elk-kafka-server,elk-kafka-client,瀏覽器中訪問localhost:8761/,查看三個項目是否成功註冊到eureka上去
訪問localhost:8080/client,查看項目後臺日誌
這時咱們能夠去Kibana上查詢日誌了,訪問http://192.168.68.112:5601/,進入Management建立index pattern kafka-logs-*,返回Discover頁面