Spring Cloud Alibaba 参考文档
Spring Cloud Alibaba 参考文档
nacos下载Nacos 快速开始
直接进入bin包 运行cmd命令:startup.cmd -m standalone
运行成功后通过http://localhost:8848/nacos进入nacos可视化页面,账号密码默认都是nacos
Nacos服务注册:以消费90、支付9001两个服务Demo为例
maven依赖:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
配置文件application.yml
server:
port: 9001
spring:
application:
name: nacos-pay-provider #以此名入驻服务注册中心
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
启动类、服务端controller
import cn.hutool.core.util.IdUtil;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.sunxiao.cloud.entities.PayDTO;
import com.sunxiao.cloud.util.Result;
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 java.math.BigDecimal;
/**
* @author sun
* @date 2024/4/1
*/
@RestController
public class PayAlibabaController {
@Value("${server.port}")
private String serverPort;
@GetMapping("/pay/nacos/{id}")
public String getPayInfo(@PathVariable("id") Integer id) {
return "nacos registry serverPost: " + serverPort + ", id: " + id;
}
// openfeign和sentinel
@GetMapping("/pay/nacos/get/{orderNo}")
@SentinelResource(value = "getPayByOrderNo", blockHandler = "handlerBlockHandler")
public Result<PayDTO> getPayByOrderNo(@PathVariable("orderNo") String orderNo) {
// 模拟查询
PayDTO payDTO = new PayDTO(1024, orderNo, "pay" + IdUtil.simpleUUID(), 1, BigDecimal.valueOf(9.9));
return Result.success(payDTO);
}
public Result<PayDTO> handlerBlockHandler(String orderNo, BlockException e) {
return Result.fail("服务提供者" + e.getMessage());
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author sunx
* @date 2024/4/1
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Main9001 {
public static void main(String[] args) {
SpringApplication.run(Main9001.class, args);
}
}
调用端Controller及RestTemplateConfig
import com.sunxiao.cloud.apis.PayFeignSentinelApi;
import com.sunxiao.cloud.config.RestTemplateConfig;
import com.sunxiao.cloud.entities.PayDTO;
import com.sunxiao.cloud.util.Result;
import jakarta.annotation.Resource;
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;
@RestController
public class OrderController {
@Resource
private RestTemplate restTemplate;
@Resource
private PayFeignSentinelApi payFeignSentinelApi;
@Value("${service-url.nacos-user-service}")
private String serverURL;
@GetMapping("/order/pay/nacos/{id}")
public String paymentInfo(@PathVariable("id") Integer id) {
return restTemplate.getForObject(serverURL + "/pay/nacos/" + id, String.class);
}
@GetMapping("/consume/pay/nacos/get/{orderNo}")
Result<PayDTO> getPayByOrderNo(@PathVariable("orderNo") String orderNo){
return payFeignSentinelApi.getPayByOrderNo(orderNo);
}
}
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;
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
复制消费服务端:
启动90、9001、9002效果:
nacos服务列表,调用时自动实现负载均衡:
发起访问请求2次:
Nacos服务配置中心
maven依赖
<!--bootstrap bootstrap.yaml-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<!--nacos-config nacos全局配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--nacos-discovery nacos 服务发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
配置文件bootstrap.yml。它的优先级高于application.yml
# nacos配置
spring:
application:
name: nacos-config-client #以此名入驻服务注册中心
cloud:
nacos:
discovery:
server-addr: localhost:8848 #Nacos服务注册中心地址
config:
server-addr: localhost:8848 #Nacos作为配置中心地址
file-extension: yml #指定yaml格式的配置
#group: PROD_GROUP #如果设置了groupid
#namespace: Prod_Namespace #如果设置了namespace
# nacos端配置文件DataId的命名规则是:
# nacos-config-client dev yaml ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yaml
config:
info: test
application.yml
server:
port: 1234
spring:
profiles:
active: dev # 表示开发环境
#active: prod # 表示生产环境
#active: test # 表示测试环境
启动类及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.web.bind.annotation.RestController;
/**
* @author sun
* @date 2024/4/1
*/
@RestController
@RefreshScope // 支持动态刷新功能
public class NacosConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {
return configInfo;
}
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author sunx
* @date 2024/4/1
*/
@SpringBootApplication
@EnableDiscoveryClient
public class Main1234 {
public static void main(String[] args) {
SpringApplication.run(Main1234.class, args);
}
}
nacos新建配置
测试效果: