G
N
I
D
A
O
L

# 微服务平台(后端)

版本选择:

Spring Boot: 2.5.7

Spring Cloud:2020.0.5

  • Spring Cloud gateway: 3.0.6
  • Spring cloud Eureka: 3.0.5

# 1. Eureka-Server 配置

引入依赖:

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

application.yml 文件:

spring:
  application:
    name: guan-service-register
server:
  port: 8888
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    register-with-eureka: false
    fetch-registry: false
    serviceUrl:
      defaultZone: http://localhost:${server.port}/eureka/

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class ServiceRegisterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceRegisterApplication.class, args);
    }
}

# 2. gateway 配置

引入 maven 依赖:

<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>
    <!--actuator 监控 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

application.yml 文件:

server:
  port: 9000
spring:
  application:
    name: guan-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      # 配置路由
      routes:
        - id: oauth
          uri: lb://guan-auth
          predicates:
            - Path=/auth-api/**
          filters:
            - StripPrefix=1 # 跳过 1 级前缀,访问会映射到 lb://guan-auth/**
logging:
  pattern:
    dateformat: HH:mm:ss.SSS
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    service-url:
      defaultZone: http://127.0.0.1:8888/eureka/
# 开放所有页面节点,默认只开启 health、info 两个节点
management:
  endpoints:
    web:
      exposure:
        include: '*'

启动类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

# 3. 引入服务类

以 guan-auth 为例

maven 依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>edu.dublbo.platform</groupId>
        <artifactId>guan-common</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

application.yml 配置文件:

# 服务启动的端口
server:
  port: 8000
spring:
  application:
    name: guan-auth
logging:
  pattern:
    console: '%d{HH:mm:ss.SSS,CTT} %level (%file:%line\)- %m%n'
eureka:
  instance:
    prefer-ip-address: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port}
  client:
    # 注册到 Eureka 的注册中心
#    register-with-eureka: true
    # 获取注册实例列表
#    fetch-registry: true
    service-url:
      # 配置注册中心地址
      defaultZone: http://127.0.0.1:8888/eureka

启动类:

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GuanAuthApplication {
    public static void main(String[] args) {
        SpringApplication.run(GuanAuthApplication.class, args);
    }
}

# 4. 测试

访问 auth 服务:

  • 通过 path 访问:localhost:9000/auth-api/auth 服务接口 url
  • 通过 spring.application.name 访问:localhost:9000/guan-auth/auth 服务接口 url