JAVA全系列 教程
3762个小节阅读:7094.5k
目录
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
前台用户在访问首页的时候是可以查看广告的,但前台项目无法使用后台项目的接口,因为后台项目接口需要管理员登录才能使用,且前台项目访问量大,我们需要专门编写一个api模块方便前台用户访问。
创建名为shopping_category_customer_api
的SpringBoot工程,添加相关依赖。
xxxxxxxxxx
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping_common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!-- dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-nacos</artifactId>
</dependency>
<!-- nacos -->
<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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
设置该工程的父工程为shopping
。
xxxxxxxxxx
<parent>
<groupId>com.itbaizhan</groupId>
<artifactId>shopping</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
给shopping
工程设置子模块
xxxxxxxxxx
<!-- 子模块 -->
<modules>
<!-- 网站用户操作广告暴露的api -->
<module>shopping_category_customer_api</module>
</modules>
在Nacos配置中心编写配置文件shopping_category_customer_api-dev.yaml
xxxxxxxxxx
# 端口号
server:
port: 8002
# 日志格式
logging:
pattern:
console: '%d{HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n'
# Nacos
spring:
application:
name: shopping_category_customer_api
cloud:
nacos:
discovery:
server-addr: 192.168.0.99:8848
dubbo:
application:
#项目名字
name: shopping_category_customer_api
protocol:
name: dubbo
port: -1
registry:
# 注册地址
address: nacos://192.168.0.99:8848
编写配置文件bootstrap.yml
,连接配置中心
xxxxxxxxxx
spring:
profiles:
# 环境
active: dev
cloud:
nacos:
# 注册中心
config:
server-addr: 192.168.0.99:8848 #注册中心地址
namespace: 5455dc7a-b343-4866-a973-96d5cc1d9312 #命名空间
file-extension: yaml #文件名后缀
prefix: shopping_category_customer_api
编写启动类,忽略数据源自动配置,向Nacos注册服务
xxxxxxxxxx
@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
@EnableDiscoveryClient //向注册中心注册该服务
@RefreshScope // 配置动态刷新
public class ShoppingCategoryCustomerApiApplication {
public static void main(String[] args) {
SpringApplication.run(ShoppingCategoryCustomerApiApplication.class, args);
}
}
编写前台用户广告控制器
xxxxxxxxxx
/**
* 广告
*/
@RestController
@RequestMapping("/user/category")
public class CategoryController {
@DubboReference
private CategoryService categoryService;
/**
* 查询全部启用广告
*
* @return 查询结果
*/
@GetMapping("/all")
public BaseResult<List<Category>> findAll() {
List<Category> categories = categoryService.findAll();
return BaseResult.ok(categories);
}
}
测试前台查询广告接口