nacos搭建
nacos官方文档 https://nacos.io/zh-cn/docs/quick-start.html
- 下载nacos压缩包 或者源码
- 压缩包地址 https://github.com/alibaba/nacos/releases
- 源码 git clone https://github.com/alibaba/nacos.git
- 启动项目
- 解压项目
- sh startup.sh -m standalone
- 关系服务
- sh shutdown.sh
nacos默认的端口号为8848
如果要修改只需要修改
nacos/conf/application.properties
文件下的server.port就可以了
搭建基于springcloud的dubbo服务
provider
pom文件配置
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
//springboot版本 消费端的版本一致
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
//springcloud 版本 消费端的版本一致
<spring-cloud-alibaba.version>2.2.2.RELEASE</spring-cloud-alibaba.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
//nacos 配置中心
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
//nacos 注册中心
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
//dubboo 依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-dubbo</artifactId>
</dependency>
# 应用名称
spring.application.name=spt-user
# dubbo 协议
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
dubbo.protocol.port=-1
# Dubbo 消费端订阅服务端的应用名,多个服务提供者用逗号分隔
# 这里订阅"自己",会被忽略掉,请根据实际情况添加
dubbo.cloud.subscribed-services=spt-user
# dubbo 服务扫描基准包
dubbo.scan.base-packages=com.spt.user
#dubbo的注册地址如果没有 默认挂在到springcloud 所以用springcloud 就可以不用写了
#dubbo.registry.address=spring-cloud://localhost
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=10.73.98.45:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public
#server.port=8081
spring.profiles.active=public
接口+实现类
//接口类
public interface TestFacade {
ResponseDto<TestVo> test(TestRequest testRequest);
}
//实现类
//dubbo 注解 服务启动的时候回扫描带dubboservice的注解 然后根据我们配置的协议转换成对应的协议头开头的请求地址
@DubboService(protocol = "dubbo")
public class TestFacadeImpl implements TestFacade {
@Autowired
TestBiz testBiz;
//这里就是一个简单的test方法
@Override
public ResponseDto<TestVo> test(TestRequest testRequest) {
ResponseDto<TestVo> responseDto = new ResponseDto<>();
TestVo vo = testBiz.abc(testRequest);
responseDto.setData(vo);
return responseDto;
}
}
consumer
pom文件配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
//作为聚合层对外输出,需要用到controller
<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-dubbo</artifactId>
</dependency>
# 应用名称
# 应用名称
spring.application.name=api-server
# dubbo 协议
dubbo.protocol.id=dubbo
dubbo.protocol.name=dubbo
# dubbo 协议端口( -1 表示自增端口,从 20880 开始)
dubbo.protocol.port=-1
# 这里订阅服务端的application.name
dubbo.cloud.subscribed-services=spt-user
# dubbo 服务扫描基准包
dubbo.scan.base-packages=com.spt.api
#启动的时候不校验服务端是否启动
dubbo.consumer.check=false
# Nacos帮助文档: https://nacos.io/zh-cn/docs/concepts.html
# Nacos认证信息
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
# Nacos 服务发现与注册配置,其中子属性 server-addr 指定 Nacos 服务器主机和端口
spring.cloud.nacos.discovery.server-addr=10.73.98.45:8848
# 注册到 nacos 的指定 namespace,默认为 public
spring.cloud.nacos.discovery.namespace=public
server.port=8082
服务端代码
@RestController
public class TestController {
@DubboReference
TestFacade testFacade;
Logger logger = LoggerFactory.getLogger(TestController.class);
@GetMapping(value = "test")
public String test(Integer flag){
TestRequest testRequest = new TestRequest();
testRequest.setFlag(flag);
ResponseDto<TestVo> responseDto = testFacade.test(testRequest);
logger.info(responseDto.toString());
if(responseDto.isSuccess()){
return "success";
}
return "fail";
}
}
gateway
springcloud alibaba gateway 提供了一个用于在Spring WebFlux之上构建API网关的库。Spring Cloud Gateway旨在提供一种简单而有效的方法来路由到API,并为它们提供跨领域的关注点,例如:安全性,监视/指标和弹性。
pom文件
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
//最新的gateway版本提供了与sentinel合并的依赖包 不影响只是用gateway
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</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-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
</dependencies>
# 应用名称
spring.application.name=spt-gateway
server.port=8900
spring.main.allow-bean-definition-overriding=true
#nacos注册中心配置
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.server-addr=10.73.98.45:8848
spring.cloud.nacos.discovery.namespace=public
spring.cloud.gateway.discovery.locator.lower-case-service-id=true
#是否开始服务注册和发现功能,以为我们是要发现api-server服务,所以开启
spring.cloud.gateway.discovery.locator.enabled=true
//具体的路由规则
//id是随意定义的 sentinel配置限流能用到id
spring.cloud.gateway.routes[0].id=web
//lb 表示挂载到注册中心,会从注册中心找到api-server的服务列表,然后进行路由,默认问轮询
spring.cloud.gateway.routes[0].uri=lb://api-server
//表示test开头的会请求转发到 api-server
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/acb/**
//表示请求转发的时候会去掉最前面的一级目录 ip:port/acb/**
spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
spring.cloud.gateway.routes[1].id=auth
spring.cloud.gateway.routes[1].uri=lb://spt-auth
spring.cloud.gateway.routes[1].predicates[0]=Path=/auth/**
spring.cloud.gateway.routes[1].filters[0]=StripPrefix=0
#spring.cloud.gateway.routes[1].filters[1]=LogFilterFactory=true
#这块是sentinel的配置
spring.cloud.sentinel.transport.dashboard=127.0.0.1:8085
spring.cloud.sentinel.datasource.ds.nacos.server-addr=10.73.98.45:8848
spring.cloud.sentinel.datasource.ds.nacos.data-id=sentinel
spring.cloud.sentinel.datasource.ds.nacos.username=nacos
spring.cloud.sentinel.datasource.ds.nacos.password=nacos
spring.cloud.sentinel.datasource.ds.nacos.group-id=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.data-type=json
spring.cloud.sentinel.datasource.ds.nacos.rule-type=degrade
Golbalfilter 在gateway 中基本都会用到,一般会作为鉴权,请求url,参数日志打印等
@Component("authFilter")
public class Authfilter implements GlobalFilter, Ordered {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
String path = exchange.getRequest().getURI().getPath();
logger.info("path:{}",path);
//过滤不需要拦截的url
if(path.startsWith("/auth/login")){
Mono<Void> filter = chain.filter(exchange);
return filter;
}
//token校验
ServerHttpRequest request = exchange.getRequest();
HttpHeaders header = request.getHeaders();
String token = header.getFirst("token");
System.out.println("token="+token);
ServerHttpResponse response = exchange.getResponse();
if (StringUtils.isBlank(token)) {
System.out.println("token not found");
response.setStatusCode(HttpStatus.UNAUTHORIZED);
return response.setComplete();
}
return chain.filter(exchange);
}
@Override
public int getOrder() {
return 10;
}
}
@Configuration
public class GatewayConfiguration {
@Autowired
private Authfilter authfilter;
}