Spring Cloud Alibaba 微服务生态基础实践

本文介绍了如何使用Spring Cloud Alibaba建立微服务,并将其注册到Nacos。详细步骤包括配置Nacos服务注册,创建微服务消费者进行服务调用,设置服务调用超时时间和Feign日志监控。此外,文章还探讨了Sentinel的安装与应用,通过Sentinel对微服务提供方和调用方进行流量控制,包括设置流控和降级规则,并使用JMeter模拟高并发测试其效果。
摘要由CSDN通过智能技术生成

3.2 建立微服务并向Nacos注册服务

  • 建立微服务工程项目 

  • 勾选Nacos Service Discovery依赖 

  • 打开pom.xml,下载依赖

<?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”>

4.0.0

org.springframework.boot

spring-boot-starter-parent

2.2.9.RELEASE

nacos.democonsumer

sentinel

0.0.1-SNAPSHOT

sentinel

Demo project for Spring Boot

<java.version>1.8</java.version>

<spring-cloud-alibaba.version>2.2.1.RELEASE</spring-cloud-alibaba.version>

org.springframework.boot

spring-boot-starter-web

com.alibaba.cloud

spring-cloud-starter-alibaba-nacos-discovery

org.springframework.boot

spring-boot-starter-test

test

org.junit.vintage

junit-vintage-engine

com.alibaba.cloud

spring-cloud-alibaba-dependencies

${spring-cloud-alibaba.version}

pom

import

org.springframework.boot

spring-boot-maven-plugin

  • 设置项目配置文件:application.yml

端口

server:

port: 8083

spring:

application:

name: goods-service

cloud:

nacos服务注册

nacos:

discovery:

server-addr: 172.16.109.118:8848

  • 通过 Spring Cloud Alibaba原生注解 @EnableDiscoveryClient 开启服务注册发现功能

// 通过 Spring Cloud 原生注解 @EnableDiscoveryClient 开启服务注册发现功能

@EnableDiscoveryClient

@SpringBootApplication

public class DemoServiceApplication {

public static void main(String[] args) {

SpringApplication.run(DemoServiceApplication.class, args);

}

}

  • 建立HTTP接口的商品信息微服务,模拟返回商品列表

/**

  • 商品信息微服务-模拟返回商品列表

*/

@RestController

@RequestMapping(“api/goods”)

public class GoodsService {

public static final Logger logger = LoggerFactory.getLogger(GoodsService.class);

// 返回商品列表

@GetMapping

public List getAllGoods(HttpServletRequest httpServletRequest) {

List goods = new ArrayList<>();

  • 10
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值