从github下载sentinel
这里下载可能非常的慢,可以右键复制链接地址,然后在迅雷里面去下载,非常的快。下载下来以后,上传到linux服务器直接启动。
启动方式一:java -jar sentinel-dashboard-1.8.4.jar需要保证8080端口没有被占用
启动方式二:java -jar -Dserver.port 8081 sentinel-dashboard-1.8.4.jar 自己指定一个端口
启动方式三:nohup java -jar -Dserver.port=9081 sentinel-dashboard-1.8.4.jar >> ./sentinel.log 2>&1 &后台启动,并且日志输出到当前目录的sentinel.log文件中
我采用第三种方式启动nohup java -jar -Dserver.port=9081 sentinel-dashboard-1.8.4.jar >> ./sentinel.log 2>&1 &
浏览器访问 http://192.168.101.41:9081
登录进去:sentinel/sentinel
这样sentinel控制台就算是部署成功了,接下来写程序连接到sentinel中
依赖文件引入pom.xml
org.springframework.cloud spring-cloud-dependencies Hoxton.SR12 pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.5.RELEASE pom import 8 8 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery com.alibaba.cloud spring-cloud-starter-alibaba-nacos-config com.alibaba.cloud spring-cloud-starter-alibaba-sentinel com.alibaba.csp sentinel-datasource-nacos org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-maven-plugin
我使用的nacos作为注册中心和配置中心参见【Spring Cloud Alibaba】nacos注册中心实践以及【Spring Cloud Alibaba】nacos配置中心实践也可以不用,直接将nacos相关的依赖删除即可。
下面是bootstrop.yml的配置
spring:
application:
name: nacos-cluster
cloud:
nacos:
discovery:
server-addr: 192.168.101.41:8858 #nacos作为注册中心地址
config:
server-addr: 192.168.101.41:8858 #nacos作为配置中心地址
file-extension: yml # 指定yaml的格式的配置
sentinel:
transport:
# 配置sentinel dashboard地址
dashboard: 192.168.101.41:9081
# 默认8719端口(与sentinel交换数据),假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
port: 8719
# 当前服务的部署主机地址
client-ip: 192.168.101.39
下面是application.yml的配置
spring:
profiles:
active: dev
server:
port: 8809
management:
endpoints:
jmx:
exposure:
include: '*'
启动程序
@SpringBootApplication
@EnableDiscoveryClient
public class ApplicationNacosConfig {
public static void main(String[] args) {
SpringApplication.run(ApplicationNacosConfig.class, args);
}
}
普通的controller
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LimitController {
@GetMapping("/get/sentinel1")
public String get1() {
return "sentinel test 一";
}
@GetMapping("/get/sentinel2")
public String get2() {
return "sentinel test 二";
}
}
最后结构如下图
启动程序,分别访问/get/sentinel1和/get/sentinel2两个接口(如果只是启动了程序,没有任何接口的访问,在sentinel中将没有这个服务的任何信息,这是一个懒加载模式,只有访问过了,在sentinel中才会有该服务的信息)



