SpringCloud学习笔记尚硅谷周阳2020版 下半场SpringCloud Alibaba

本文介绍了SpringCloud Alibaba的学习笔记,重点讲解了Nacos作为服务注册中心和配置中心的配置与使用,包括Nacos的下载安装、服务提供者和消费者注册、服务配置中心的配置。此外,还探讨了Sentinel的流量控制规则,如QPS、线程数等,并展示了Sentinel在服务熔断中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringCloud学习笔记

代码 gitee仓库地址

https://gitee.com/lu-yi1104/spring-cloud_-study.git/

上半场 (一)地址

https://blog.youkuaiyun.com/weixin_43691773/article/details/109167048

文章目录

SpringCloud Alibaba 简介

在这里插入图片描述
在这里插入图片描述

官网地址

https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
依赖 父工程已经导入了
在这里插入图片描述
在这里插入图片描述

Spring Cloud Alibaba Nacos服务注册中心和配置中心

简介

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

下载地址

https://github.com/alibaba/Nacos

中文地址
https://nacos.io/zh-cn/

nacos下载和安装

在这里插入图片描述
下载解压
修改startup.cmd MODE 由cluster改为单机版
在这里插入图片描述

启动
在这里插入图片描述
访问 localhost:8848/nacos 出现界面代表成功 默认账号密码 nacos/nacos
在这里插入图片描述

Nacos作为服务注册中心

Nacos之服务提供者注册

新建模块 9001
跟着官网来
https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_nacos_discovery
pom文件
在父工程pom文件加入 springcloud alibaba的依赖

  <!--spring cloud alibaba 2.1.0.RELEASE-->
            <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>

9001 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>com.luyi.cloud</artifactId>
        <groupId>com.luyi.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-provider-payment9001</artifactId>

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

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

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>

</project>

yml文件

server:
 port: 9001

spring:
  application:
    name: nacos-provider
  cloud:
   nacos:
    discovery:
     server-addr: 127.0.0.1:8848 #配置Nacos地址

# 暴露要监控的
management:
  endpoints:
   web:
    exposure:
      include: '*'


主启动类

package com.luyi.sprinhcloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author 卢意
 * @create 2020-11-06 11:25
 */
@EnableDiscoveryClient
@SpringBootApplication
public class PaymentMain9001 {
   
	public static void main(String[] args) {
   
		SpringApplication.run(PaymentMain9001.class, args);
	}
}

controller层

package com.luyi.sprinhcloud.alibaba.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 卢意
 * @create 2020-11-06 11:27
 */
@RestController
public class PaymentController {
   
	@Value("${server.port}")
	private String serverPort;

	@GetMapping(value = "/payment/nacos/{id}")
	public String getPyment(@PathVariable("id") Integer id){
   
		return "nacos register ,serverPort: "+serverPort+"\t id: "+id;
	}
}

测试 首先启动nacos注册中心
再启动9001
在这里插入图片描述
为了演示nacos的负载均衡、参照9001侯建9002 (也可以直接拷贝9001的虚拟端口映射)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
启动
在这里插入图片描述

在这里插入图片描述

Nacos之服务消费者注册

新建order83模块

pom文件与上面相同

yml文件

server:
  port: 83

Spring:
  application:
    name: nacos-order-consumer

  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848

# 消费者将要去访问的微服务名称(注册成功进nacos的微服务提供者)
service-url:
  nacos-user-service: http://nacos-payment-privider

主启动类

package com.luyi.springcloud.alibaba;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author 卢意
 * @create 2020-11-06 11:56
 */
@EnableDiscoveryClient
@SpringBootApplication
public class OrderNacosMain83 {
   
	public static void main(String[] args) {
   
		SpringApplication.run(OrderNacosMain83.class, args);
	}
}

新建上下文配置类

package com.luyi.springcloud.alibaba.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @author 卢意
 * @create 2020-11-06 13:34
 */
@Configuration
public class ApplicationContextConfig {
   
	@Bean
	@LoadBalanced// resttemplate 结合ribbon做负载均衡的时候 要加入注解
	public RestTemplate getRestTemplate(){
   
		return new RestTemplate();
	}
}


controller类

package com.luyi.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author 卢意
 * @create 2020-11-06 13:37
 */
@RestController
@Slf4j
public class OrderNacosController {
   
	@Resource
	private RestTemplate restTemplate;

	@Value("${service-url.nacos-user-service}")
	private String serverUrl;

	@GetMapping(value = "/consumer/payment/nacos/{id}")
	public String paymeentInfo(@PathVariable("id") Long id){
   
		return restTemplate.getForObject(serverUrl+"/payment/nacos/"+id,String.class );
	}
}

启动83
在这里插入图片描述
访问成功 并带有负载均衡效果
在这里插入图片描述
在这里插入图片描述

Nacos之自带负载均衡

在这里插入图片描述

Nacos 服务中心对比提升

在这里插入图片描述
Nacos支持Cp+Ap的切换

在这里插入图片描述
在这里插入图片描述

Nacos之服务配置中心–基础配置

新建配置中心 3377
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>com.luyi.cloud</artifactId>
        <groupId>com.luyi.cloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloudalibaba-config-nacos-client3377</artifactId>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--nacos-config-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!-- nacos-discovery -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
    </dependencies>
</project>

两个yml文件
在这里插入图片描述
bootstrap文件

# nacos 配置
server:
  port: 3377

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 # Nacos服务注册中心地址
      config:
          server-addr: localhost:8848 # Nacos作为服务中心的地址
          file-extension: yaml # 指定Yaml格式的配置  3377就可以去配置中心读指定后缀名是yaml的文件

# ${
   spring.application.name}-${
   spring.profile.active}.${
   Spring.cloud.nacos.config.file.extension}

application.yml

spring:
  profiles:
    active: dev # 表示开发环境

主启动类

package com.luyi.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author 卢意
 * @create 2020-11-06 17:04
 */
@EnableDiscoveryClient
@SpringBootApplication
public class NacosClientConfigMain3377 {
   
	public static void main(String[] args) {
   
		SpringApplication.run(NacosClientConfigMain3377.class, args);
	}
}

重复投入

package com.luyi.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值