文章目录
- 1.简单代码
- 1.1依赖
- 1.2配置文件配置
- 1.2.1项目yml配置文件
- 1.2.2nacos配置文件
- 1.2controller
- 1.3 service
- 2.sentinel下载运行
- 3.运行验证
nacos的安装部署
SpringCloudAlibaba整合nacos
1.简单代码
1.1依赖
SpringBoot 2.3.12.RELEASE
SpringCloudAlibaba 2.2.8.RELEASE
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</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-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
1.2配置文件配置
删除项目的.properties文件改为bootstrap.yml文件
1.2.1项目yml配置文件
spring:
application:
name: springcloud-sentinel
cloud:
nacos:
config:
server-addr: 127.0.0.1:8848
namespace: config
file-extension: yaml
group: DEFAULT_GROUP
discovery:
server-addr: 127.0.0.1:8848
bootstrap:
enabled: true
# profiles:
# active: dev
1.2.2nacos配置文件
server:
port: 8085
spring:
application.name: springcloud-sentinel
cloud:
sentinel:
transport.dashboard: localhost:8080
datasource:
ds.nacos:
username: nacos
password: nacos
ruleType: flow
groupId: DEFAULT_GROUP
dataId: ${spring.application.name}
server-addr : localhost:8848
1.2controller
@RequestMapping("/test")
@RestController
public class HelloController {
@Autowired
private TestService service;
@GetMapping("/hello/{name}")
public String hello(@PathVariable String name){
return service.sayHello(name);
}
}
1.3 service
@Service
public class TestService {
@SentinelResource(value = "sayHello")
public String sayHello(String name) {
return "Hello, " + name;
}
}
2.sentinel下载运行
下载jar包地址
https://github.com/alibaba/Sentinel/releases
jar -jar sentinel-dashboard-1.8.1.jar
访问http://localhost:8080
用户名/密码 sentinel/sentinel
3.运行验证
运行成功