Skip to content

微服务架构学习笔记

Spring Cloud Alibaba 学习笔记

Nacos服务

启动Nacos服务端

下载Nacos

Releases · alibaba/nacos (github.com)

启动nacos-server

双击bin文件夹中的startup.cmd

访问http://127.0.0.1:8848/nacos/

用户名密码 nacos/nacos

image-20220724134532875

Nacos服务注册

引入依赖
xml

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
配置服务地址、名称
yml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: member
为启动类开启服务发现功能

在启动类上添加注解

java
@EnableDiscoveryClient
运行应用,并在服务中心查看服务列表

image-20220724135926352

Nacos服务调用

引入依赖
xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
编写用于远程调用的服务接口
java
package com.atguigu.gulimall.member.feign;

import com.atguigu.common.utils.R;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;

@FeignClient("gulimall-coupon")
public interface CouponFeignService {

  @RequestMapping("/coupon/coupon/member/list")
  public R memberCoupons();
}
通过注解开启远程调用功能

在启动类上添加注解

java
@EnableFeignClients("com.atguigu.gulimall.member.feign")

Nacos配置中心

使用步骤

引入依赖
xml

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
添加配置文件

bootstrap.properties

properties
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
在配置中心添加配置

Data Id : 应用名.properties

在其中添加配置

动态获取配置

在需要动态获取配置的类上添加@RefreshScope注解,在需要获取配置值的变量上添加@Value("${配置项}")

优先使用配置中心中的配置

细节

命名空间

默认为public

可以在bootstrap.properties中添加

properties
spring.cloud.nacos.config.namespace=xxxxxxxxx

来修改(xxxxxxxxx代表命名空间对应id)

可以利用命名空间做环境隔离,也可以隔离每个微服务

配置分组

默认为DEFAULT_GROUP

可以在bootstrap.properties中添加

properties
spring.cloud.nacos.config.group=xxx

来修改

Gateway 网关

bootstrap.properties 配置文件

properties
spring.application.name=gulimall-gateway
spring.cloud.nacos.config.server-addr=192.168.3.200:8848
spring.cloud.nacos.config.namespace=7e5b5a74-94b5-4b70-952a-a488a6268428
server.port=88

Route: 发一个请求给网关,网关要将请求路由到指定的服务。路由有id,目的地uri,断言的集合,匹配了断言就能到达指定位置。 Predicate断言: 匹配请求里的任何信息,包括请求头等。根据请求头路由哪个服务。 Filter: 过滤器请求和响应都可以被修改。

yml
- id: product_route
uri: lb://gulimall-product
predicates:
  - Path=/api/product/**
filters:
  - RewritePath=/api/(?<segment>.*),/$\{segment}

前端项目的请求url使用/api前缀,来到网关后断言先匹配到,过滤器修改url。

例如:

配置gulimall-product应用的端口为12001,则前端请求http://localhost:88/api/product/category/list/tree会被转换为http://localhost:12001/product/category/list/tree

Spring Cloud

Eureka注册

搭建EurekaServer

  1. 引入eureka-server依赖
xml

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
  1. 添加@EnableEurekaServer注解,开启eureka的注册中心功能

  2. 在application.yml文件中配置eureka地址

yaml
server:
  port: 8989
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8989/eureka