- Spring Cloud Alibaba引用版本
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- springboot引用版本:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
- Nacos-Windows版本:nacos-server-2.1.1
- spring-cloud-starter-openfeign依赖spring-cloud-starter-loadbalancer
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
a.小提示:在SpringBoot 2.4.x的版本之后,对于bootstrap.properties/bootstrap.yaml配置文件(我们合起来成为Bootstrap配置文件)的支持,需要导入如下的依赖
,参考连接:https://blog.csdn.net/GCTTTTTT/article/details/126660215
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>
- spring cloud alibaba是Spring Cloud的子项目,所以使用也需要在各个组件中引用spring cloud,下面是其中一个组件gulimall-coupon的pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.atguigu.gulimall</groupId>
<artifactId>gulimall-coupon</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>gulimall-coupon</name>
<description>谷粒商城-优惠券服务</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>com.atguigu.gulimall</groupId>
<artifactId>gulimall-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- gulimall-common为公共组件,因为被其他组件共同使用,所以为公共.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>gulimall</artifactId>
<groupId>com.atguigu.gulimall</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>gulimall-common</artifactId>
<description>每个微服务公共依赖,比如bean或工具类</description>
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.22</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.14</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.17</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.renren</groupId>
<artifactId>renren-fast</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
- 在公共组件中引入spring-cloud-starter-alibaba-nacos-discovery,在application.yml中引入nacos服务器地址:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://192.168.1.7:3306/gulimall_sms
driver-class-name: com.mysql.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: gulimall-coupon
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
server:
port: 7000
- 在主启动类启用nacos注解@EnableDiscoveryClient,然后启动服务.
- 登录nacos网站http://127.0.0.1:8848/nacos/index.html#/login,用户名密码均为nacos,在服务管理->服务列表中查看发现并上线的服务.列表中显示的服务名是application.yml中指定的application:name名
- 创建组件时每个组件都已经引入feign,可以远程调用,这里先在gulimall-member中编写一个CouponFeignService接口:
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();
}
@FeignClient("gulimall-coupon")是Feign注解必须的,gulimall-coupon表示要远程调用那个服务,调用coupon服务中的那个请求?可以直接复制coupon服务中的请求代码,这里是@RequestMapping("/coupon/coupon/member/list") public R membercoupons();
11. 然后再member服务主启动类添加注解@EnableFeignClients(basePackages = "com.atguigu.gulimall.member.feign")
扫描带注解的包
12. 测试访问请求:http://localhost:8000/member/member/coupons
使用ncos配置中心
- 首先common组件引入maven包
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
2.新增src\main\resources\bootstrap.properties配置文件,配置 Nacos server 的地址和应用名
spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
#spring.cloud.nacos.config.refresh-enabled=true
#spring.cloud.nacos.config.enable-remote-sync-config=true
在application.properties中配置键值
coupon.user.name=zhangsan2
coupon.user.age=18
这里的参数值可以通过@Value注解获得
@Value("${coupon.user.name}")
private String name;
@Value("${coupon.user.age}")
private Integer age;
@RequestMapping("/test")
public R test(){
return R.ok().put("name",name).put("age",age);
}
写在配置文件中的参数修改需要重启应用,使用nacos无需重启应用,登录http://localhost:8848/nacos/index.html#,
配置管理->配置列表->新建配置,
Data Id就是配置文件中的应用名:spring.application.name=gulimall-coupon
3. 然后在control中需要请求的类中添加注解@RefreshScope
然后测试访问:
命名空间管理
Nacos 基于Namespace 帮助用户逻辑隔离多个命名空间,这可以帮助用户更好的管理测试、预发、生产等多环境服务和配置
创建好命名空间后,先选择命名空间,再创建配置:
最后在bootstrap.properties中指定prop的命名空间ID值spring.cloud.nacos.config.namespace=1b64f1ec-e3aa-46ff-9e7e-107618bae01d
- 也可以基于每个组件服务配置单独的命名空间配置管理
- 可以在不同的命名空间下创建分组:
注意在配置文件中不同的分组要在同一命名空间下,否则不生效.
将所有配置写在nacos上,我们注释掉本地的配置application.yml
#spring:
# datasource:
# username: root
# password: 123456
# url: jdbc:mysql://192.168.1.7:3306/gulimall_sms
# driver-class-name: com.mysql.jdbc.Driver
# cloud:
# nacos:
# discovery:
# server-addr: 127.0.0.1:8848
# application:
# name: gulimall-coupon
#mybatis-plus:
# mapper-locations: classpath:/mapper/**/*.xml
# global-config:
# db-config:
# id-type: auto
#
#server:
# port: 7000
将以上配置写在nacos上,首先指定spring.cloud.nacos.config.namespace=a84ad...
,这是将coupon的配置全部写在coupon命名空间里的配置中
然后在bootstrap.properties中继续填nacos具体的配置如下:
spring cloud gateway
小提示:目前使用的的gateway版本是3.1.4,gateway中集成了org.springframework.boot:spring-boot-starter-webflux:2.6.6,这和spring-boot-starter-web会出现冲突,如果出现冲突,根据错误提示反馈:Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency
,我测试了第一种方法有效,在application.properties中添加spring.main.web-application-type=reactive
- gateway组件先不使用数据库,所以主启动类先不启用数据库连接类,否则报错无法连接数据库
/*开启服务注册发现*/
@EnableDiscoveryClient
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DruidDataSourceAutoConfigure.class ,})
public class GulimallGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GulimallGatewayApplication.class, args);
}
}
- gateway功能之一:Query路由断言接受两个参数:一个必需的参数和一个可选的regexp(这是一个Java正则表达式)。配置查询路由断言(谓词)的application.yml配置示例如下:
spring:
cloud:
gateway:
routes:
- id: test_route
uri: https://www.baidu.com
predicates:
- Query=url,baidu
- id: qq_route
uri: https://www.qq.com
predicates:
- Query=url,qq
访问测试http://localhost:88/hello?url=qq 如果请求连接中包含url=qq的查询参数,那么就会跳到https://www.qq.com/hello上.
参考连接:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#gatewayfilter-factories中的5.9. The Query Route Predicate Factory章节