使用Maven搭建SpringCloud项目

使用Eureka搭建注册中心

增加eureka子模块作为注册服务端

pom.xml

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

解决注册中心服务启动失败的问题

application.properties

1
2
3
4
5
6
7
spring.application.name=eureka
server.port=8761

#表示是否从Eureka Server获取注册的服务信息
eureka.client.fetch-registry=false
#表示是否将自己注册到Eureka Server
eureka.client.register-with-eureka=false

注册为服务中心@EnableEurekaServer
EurekaApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.javami.course;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.core.env.Environment;

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
private static final Logger LOG = LoggerFactory.getLogger(EurekaApplication.class);

public static void main(String[] args) {
SpringApplication app = new SpringApplication(EurekaApplication.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("Eureka地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}

}

优化启动日志显示

logback.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- 修改一下路径-->
<property name="PATH" value="/log/javami/course/eureka"></property>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<!-- <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) %blue(%-50logger{50}:%-4line) %msg%n</Pattern>-->
<Pattern>%d{mm:ss.SSS} %highlight(%-5level) %blue(%-30logger{30}:%-4line) %msg%n</Pattern>
</encoder>
</appender>

<appender name="TRACE_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${PATH}/trace.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${PATH}/trace.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-50logger{50}:%-4line %green(%-8X{UUID}) %msg%n</pattern>
</layout>
</appender>

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${PATH}/error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>${PATH}/error.%d{yyyy-MM-dd}.%i.log</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout>
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %-50logger{50}:%-4line %green(%-8X{UUID}) %msg%n</pattern>
</layout>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<root level="ERROR">
<appender-ref ref="ERROR_FILE" />
</root>

<root level="TRACE">
<appender-ref ref="TRACE_FILE" />
</root>

<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

搭建业务模块-system

将system配置成最简单的SpringBoot项目

system/pom.xml

1
2
3
4
5
6
7
8
9
10
11
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>

application.properties

1
2
3
4
spring.application.name=system
server.port=9001
#注册到http://localhost:8761/eureka/
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

注册为Eureka客户端@EnableEurekaClient
SystemApplication..java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.javami.course.system;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.env.Environment;

@EnableEurekaClient
@SpringBootApplication
public class SystemApplication {
private static final Logger LOG = LoggerFactory.getLogger(SystemApplication.class);

public static void main(String[] args) {
SpringApplication app = new SpringApplication(SystemApplication.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("Eureka地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}

}

搭建路由模块-gateway

新增gateway子模块,增加gateway依赖,并注册到注册中心

pom.xml

1
2
3
4
5
6
7
8
9
10
11
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>

注册为Eureka客户端@EnableEurekaClient
gateway/GatewayApplication.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.javami.gateway;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.core.env.Environment;

@SpringBootApplication
@EnableEurekaClient
public class GatewayApplication {

private static final Logger LOG = LoggerFactory.getLogger(GatewayApplication.class);

public static void main(String[] args) {
SpringApplication app = new SpringApplication(GatewayApplication.class);
Environment env = app.run(args).getEnvironment();
LOG.info("启动成功!!");
LOG.info("Gateway地址: \thttp://127.0.0.1:{}", env.getProperty("server.port"));
}

}

application.properties

1
2
3
spring.application.name=gateway
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

在gateway增加system路由转发配置,调试成功

定义: server.servlet.context-path= # Context path of the application.应用的上下文路径,也可以称为项目路径,是构成url地址的一部分。
system/src/main/resources/application.properties

1
server.servlet.context-path=/system

resources/application.properties

1
2
3
4
5
6
7
8
spring.application.name=gateway
server.port=9000
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

spring.cloud.gateway.routes[0].id=system
spring.cloud.gateway.routes[0].uri=http://127.0.0.1:9001
spring.cloud.gateway.routes[0].predicates[0].name=Path
spring.cloud.gateway.routes[0].predicates[0].args[0]=/system/**

主要参数说明

  • spring.cloud.gateway.routes[0].id 标志号
  • spring.cloud.gateway.routes[0].uri 路由映射目标路径
  • spring.cloud.gateway.routes[0].predicates 匹配规则,也是暴露的映射路径