【雷丰阳-谷粒商城 】【分布式基础篇-全栈开发篇】【02】Nacos、Feign、Gateway

news2025/4/12 4:24:35

持续学习&持续更新中…

学习态度:守破离


【雷丰阳-谷粒商城 】【分布式基础篇-全栈开发篇】【02】

  • 微服务的几大组件
  • SpringCloud Alibaba 简介
    • 简介
    • 为什么使用
    • 版本选择
  • Nacos作为注册中心
  • Feign声明式远程调用
  • Nacos作为配置中心
    • 基本使用
    • 核心概念
      • 命名空间:配置隔离
      • 配置集:所有的配置的集合
      • 配置集ID:类似文件名(Nacos中的DataID)
      • 配置分组
      • 总结
    • 加载多配置集
  • SpringCloud-Gateway
    • 简介
    • 工作流程
    • 简单示例
  • 参考

微服务的几大组件

在这里插入图片描述

SpringCloud Alibaba 简介

简介

SpringCloudAlibaba官网:https://spring.io/projects/spring-cloud-alibaba/

在这里插入图片描述

为什么使用

在这里插入图片描述

版本选择

在这里插入图片描述

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

Nacos作为注册中心

官网:https://nacos.io/zh-cn/

下载nacos-server1.1.3:

https://objects.githubusercontent.com/github-production-release-asset-2e65be/137451403/d6c1ee80-b87c-11e9-8f32-79d0ba6f9c89?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20221129%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20221129T110414Z&X-Amz-Expires=300&X-Amz-Signature=609c8b94c94e42fa5463ef109f3ea2df6a86e9c292822be96b99ce4c7d89a915&X-Amz-SignedHeaders=host&actor_id=46433032&key_id=0&repo_id=137451403&response-content-disposition=attachment%3B%20filename%3Dnacos-server-1.1.3.zip&response-content-type=application%2Foctet-stream

运行nacos-server:

在这里插入图片描述

在这里插入图片描述

依赖:

	<dependency>
	    <groupId>com.alibaba.cloud</groupId>
	    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	</dependency>

application.yml:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
  application:
    name: gulimall-coupon

注解:

@EnableDiscoveryClient
@SpringBootApplication
public class GulimallCouponApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallCouponApplication.class, args);
    }

}

可视化界面:

在这里插入图片描述

Feign声明式远程调用

在这里插入图片描述

依赖:【feign被闭源了,SpringCloud开源了一个OpenFeign。属于SpringCloud,不属于SpringCloudAlibaba】

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
    </dependencyManagement>
	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-openfeign</artifactId>
	</dependency>

开启Feign的使用:

@EnableFeignClients(basePackages = "l.p.gulimall.member.feign")
@EnableDiscoveryClient
@SpringBootApplication
public class GulimallMemberApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallMemberApplication.class, args);
    }

}

声明式远程调用

gulimall-coupon:

@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Autowired
    private CouponService couponService;

    /**
     * 测试feign
     */
    @RequestMapping("/memberCoupon")
    public R memberCoupon() {
        CouponEntity coupon = new CouponEntity();
        coupon.setCouponName("买一送一");
        return R.ok().put("coupon", coupon);
    }
}

gulimall-member:

/*
这是一个声明式的远程调用
 */
@FeignClient("gulimall-coupon")
public interface CouponFeignService {
    @RequestMapping("/coupon/coupon/memberCoupon")
    R memberCoupon();
}

MemberController:

@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity member = new MemberEntity();
        member.setUsername("lp");
        return R.ok().put("member", member).put("coupons", couponFeignService.memberCoupon());
    }
}

Nacos作为配置中心

基本使用

引入依赖:

	<dependency>
	    <groupId>com.alibaba.cloud</groupId>
	    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
	</dependency>

新建bootstrap.properties:【.properties与.yml文件都可以】

spring.application.name=gulimall-coupon
spring.cloud.nacos.config.server-addr=localhost:8848

Nacos中添加配置:

在这里插入图片描述

微服务中使用配置:

@RefreshScope
@RestController
@RequestMapping("coupon/coupon")
public class CouponController {
    @Value("${coupon.user.name}")
    private String cun;
    @Value("${coupon.user.age}")
    private Integer cua;

    @RequestMapping("/test")
    public R test() {
        return R.ok().put("name", cun).put("age", cua);
    }
}

核心概念

命名空间:配置隔离

在这里插入图片描述

默认:public(保留空间);默认新增的所有配置都在public命名空间,默认使用的也是public。

举例:

  • 开发,测试,生产:利用命名空间来做环境隔离。
  • 每一个微服务之间互相隔离配置:每一个微服务都创建自己的命名空间,只加载自己命名空间下的所有配置

注意:在bootstrap.properties配置上,需要使用哪个命名空间下的配置:spring.cloud.nacos.config.namespace=9de62e44-cd2a-4a82-bf5c-95878bd5e871

配置集:所有的配置的集合

在这里插入图片描述

配置集ID:类似文件名(Nacos中的DataID)

在这里插入图片描述

配置分组

在这里插入图片描述

默认所有的配置集都属于:DEFAULT_GROUP

总结

项目中的使用:

  • 每个微服务创建自己的命名空间;
  • 使用配置分组区分环境:dev,test,prod

加载多配置集

bootstrap.yml:

spring:
  application:
    name: gulimall-coupon
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        namespace: ddecbef0-996d-48bd-ab3c-910af2291344
        group: dev
        ext-config:
          - dataId: datasource.yaml
            group: dev
            refresh: true
          - dataId: mybatis.yaml
            group: dev
            refresh: true
          - dataId: others.yaml
            group: dev
            refresh: true

在这里插入图片描述

datasource.yaml:

在这里插入图片描述

others.yaml:

在这里插入图片描述

mybatis.yaml:

在这里插入图片描述

gulimall-coupon.properties:

在这里插入图片描述

总结

  • 微服务任何配置信息,任何配置文件都可以放在配置中心中
  • 只需要在bootstrap.properties / bootstrap.yml说明加载配置中心中哪些配置文件即可
  • SpringBoot启动加载的配置、@Value、@ConfigurationProperties都能使用Nacos所管理的配置

SpringCloud-Gateway

简介

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

工作流程

在这里插入图片描述

简单示例

引入依赖:

    <dependencies>
        <dependency>
            <groupId>l.p.gulimall</groupId>
            <artifactId>gulimall-common</artifactId>
            <version>1.0.0</version>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
    </dependencies>

Nacos配置:

在这里插入图片描述

在这里插入图片描述

server:
  port: 88
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

bootstrap.yml:

spring:
  application:
    name: gulimall-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
      config:
        server-addr: localhost:8848
        namespace: a46c9ea5-c78b-4191-bb0c-14da3991c2f7
        group: dev
        file-extension: yaml

启动gateway网关服务:

@EnableDiscoveryClient
// 父项目中有依赖MySQL的starter,因此需要在这儿排除一下【或者在pom.xml中使用<exclusion>标签】
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class GulimallGatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GulimallGatewayApplication.class, args);
    }

}

访问测试:

在这里插入图片描述

参考

雷丰阳: Java项目《谷粒商城》Java架构师 | 微服务 | 大型电商项目).


本文完,感谢您的关注支持!


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/52387.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【Linux】进程通信 | 信号

本篇博客让我们一起来康康信号部分的内容 系统为CentOS7.6&#xff0c;完整代码见 Gitee 文章目录1.什么是信号1.1 何为异步?1.2 信号的种类1.3 信号产生1.4 信号动作2.系统接口2.1 signal2.1.1 前台进程和后台进程2.1.2 循环捕捉所有信号2.1.3 信号9/192.2 kill2.2.1 killall…

人工智能数学基础--概率与统计13:连续随机变量的标准正态分布

一、引言 在《人工智能数学基础–概率与统计12&#xff1a;连续随机变量的概率密度函数以及正态分布》介绍了连续随机变量概率分布及概率密度函数的概念&#xff0c;并介绍了连续随机变量一个重要的概率密度函数&#xff1a;正态分布的概率密度函数的定义以及推导、使用场景&a…

培养出最多亿万富翁的美国大学TOP10榜单

若论世界大学排名&#xff0c;除了U.S. News、QS、软科、泰晤士这四大权威排名外&#xff0c;另有一些依据不同指标的排名。下面知识人网小编就推荐这份福布斯榜单给出的美国大学排名&#xff0c;供感兴趣的读者围观。 福布斯去年发布了一份全球亿万富豪榜&#xff08;World’s…

怎么看网站域名有没有收录 收录情况怎么样 网站收录查询

对于网站收录的概念&#xff0c;互联网中或者搜索引擎中已经有大量的相关定义。网站收录&#xff0c;指的是爬虫爬取了网页&#xff0c;并将页面内容数据放入搜索引擎数据库中这一结果。 怎么看网站域名有没有收录?录情况怎么样? 用站长工具查询网站收录的操作步骤&#xff1…

[TIST 2022]No Free Lunch Theorem for Security and Utility in Federated Learning

联邦学习中的安全性和实用性没有免费午餐定理 No Free Lunch Theorem for Security and Utility in Federated Learning 目录摘要简介2 相关文献2.1 隐私测量2.2 联邦学习2.2.1 FL 中的威胁模型。2.2.2 FL 中的保护机制。2.3 隐私-实用权衡3 一般设置和框架3.1 符号3.2 一般设置…

前端如何实现网页变灰功能的?

备注&#xff1a;本文大量摘取前端充电宝公众号相关文章&#xff0c;大家感兴趣可以关注该公众号进行阅读学习 目录 1.引入 2.页面灰色实现方法 3.filter其他属性 A.blur()&#xff1a;模糊 B.brightness()&#xff1a;亮度 C.contrast()&#xff1a;对比度 C.opacity()…

unity---Mesh网格编程(六)

目录 1.模型切割 2.代码 1.模型切割 如图&#xff0c;对3D模型的Mesh网格进行切割&#xff0c;会经过若干个三角面。而切割后&#xff0c;将会产生新的面来组成左右两边的物体。 要记录每个顶点与顶点下标&#xff0c;新的面要顺时针绘制&#xff0c; 2.代码 using System.…

docker+nginx 安装部署修改资源目录配置文件和容器端口信息

查看docker镜像 可以先查看docker下是否存在nginx镜像&#xff0c;使用如下这些命令查看&#xff1a; docker images: 列出所有镜像。docker images nginx: 列出所有nginx镜像&#xff0c;不同版本等等。docker search nginx: 搜索查看所有nginx镜像信息。 拉取安装nginx镜像…

Java8 函数式编程【基础篇】

Java 8是Java在保持向后兼容的前提下首次迈出重要一步&#xff0c;相比之前&#xff0c;不再是只对类库的改良&#xff0c;在编写复杂的集合处理、并行化执行、代码简洁度等方面都有颠覆性的提升。本文将探索和理解函数式编程的含义&#xff0c;以及它在Java 8中的实现。 一、…

RESTful API是什么?看完你整个人都通透了

要弄清楚什么是RESTful API&#xff0c;首先要弄清楚什么是REST&#xff1f; 01 REST REpresentational State Transfer&#xff0c;英语的直译就是“表现层状态转移”。如果看这个概念&#xff0c;估计没几个人能明白是什么意思。那下面就让我来用一句话解释一下什么是RESTf…

低代码搭建质量管理解决方案,为企业管理提速降本

市场竞争的越来越卷&#xff0c;越来越多的制造企业认识到质量管理的重要性。尤其随着全球经济化与信息化的到来&#xff0c;质量管理已经成为企业管理的关键环节。然而与国际上具有先进技术和管理水平的企业相比&#xff0c;我国企业的质量管理较为薄弱&#xff0c;存在着质量…

【MATLAB】羽状图

目录 羽状图 羽状图 h0figure(toolbar,none,...position,[200 150 450 350],...name,实例28);subplot(2,1,1)alpha90:-10:0;rones(size(alpha));malpha*pi/180;nr*10;[u,v]pol2cart(m,n);feather(u,v)title(羽状图)axis([0 20 0 10])subplot(2,1,2)t0:0.5:10;x0.05i;yexp(-x*t…

R语言解释生存分析中危险率和风险率的变化

危险率函数 让我们模拟R中的一些数据&#xff1a; n < - 10000 h < - 0.5 t < - -log&#xff08;runif&#xff08;n&#xff09;&#xff09;/ h 该代码模拟了危险函数的存活时间&#xff0c;即常数。 视频&#xff1a;R语言生存分析原理与晚期肺癌患者分析案…

Ajax学习:jQuery发送ajax请求 通用方法$.ajax

app.all(/jQuery,(requset,response)>{response.setHeader(Access-Control-Allow-Origin,*);const data{name:张三};let strJSON.stringify(data);//需要转换称为json 否则传递的任然是对象response.send(str);//3s之后返回给客户端 }) $(button).eq(2).click(function() {/…

MySQL和Oracle JDBC驱动包下载步骤

MySQL官网&#xff1a;https://www.mysql.com/ 步骤如下&#xff1a; 1.点击DOWNLOADS 2.往下滑&#xff0c;找到MySQL Community&#xff08;GPL&#xff09;Downloands并点击 3.点击Connector/J 4.当前页面展示的是最新版本&#xff0c;要下载历史版本点击Archives 5.选择…

15 【登录鉴权】

15 【登录鉴权-Cookie】 1.什么是认证&#xff08;Authentication&#xff09; 通俗地讲就是验证当前用户的身份&#xff0c;证明“你是你自己”&#xff08;比如&#xff1a;你每天上下班打卡&#xff0c;都需要通过指纹打卡&#xff0c;当你的指纹和系统里录入的指纹相匹配…

细数APDL中的流程控制命令

作者&#xff1a;水哥ANSYS&#xff0c;获授权转载 一、概述 有过其他编程语言经验的同学都知道&#xff0c;流程控制类语言命令在编程中是必须掌握的一门技巧&#xff0c;这类命令能大幅提高我们的编程效率&#xff0c;增加程序可读性。类似地&#xff0c;在APDL中也有很多的…

R语言Poisson回归的拟合优度检验

在这篇文章中&#xff0c;我们将看一下Poisson回归的拟合优度测试与个体计数数据。 最近我们被客户要求撰写关于Poisson回归的研究报告&#xff0c;包括一些图形和统计输出。许多软件包在拟合Poisson回归模型时在输出中提供此测试&#xff0c;或者在拟合此类模型&#xff08;例…

不刷题,PMP考试可以通过吗?

不能&#xff0c;除非你的项目管理经验很厉害&#xff0c;但这么厉害也不需要PMP这个证书了&#xff0c;做一个比喻&#xff0c;学习项目管理知识是读兵书&#xff0c;做题就是“纸上谈兵”&#xff0c;获得PMP证书就是证明你有做指挥的资格&#xff0c;做项目是上战场指挥打仗…

提交代码出现error Empty block statement no-empty,代码却没报错?

开开心心写完代码&#xff0c;commit一下&#xff0c;发现可能控制台报错了&#xff1a; 看了代码却没发现有报错的&#xff0c;后来发现是开了eslint校验&#xff01; 因为存在空的if体&#xff0c;如下&#xff1a; 解决&#xff1a;保证if体不为空即可 参考&#xff1a;…