当前位置:网站首页>微服务开发步骤(nacos)
微服务开发步骤(nacos)
2022-07-01 14:20:00 【海蓝时见鲸-】
其他的module中引入了mall-common模块
每个Module分别连接着一个数据库
1.开启nacos服务
2.在mall-commom中引入依赖
<!--1.第一步:服务的注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
3.在服务中(非common模块)的application.yml
文件中配置nacos的地址
spring:
#配置Nacos Server地址并且要写application.name
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
application:
name: mall-member
#其他的服务分别配置name: mall-coupon、mall-member、mall-order、mall-product、mall-ware
4.在给个Module中的启动类上配置注解@EnableDiscoveryClient
@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(MallMemberApplication.class, args);
}
}
通过以上步骤就可以在nacos看到注册进去的模块
========================================================================
接下来就要使用远程调用用来从一个服务调用另一个服务中的方法:Fegin声明式远程调用
说明:mall-member服务想要调用mall-coupon服务,那就在mall-member服务中加东西
5.在mall-member的pom.xml问价中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
6.在mall-coupon中写一个方法准备让mall-member调用
@RequestMapping("/member/list")
public R membercoupons() {
CouponEntity couponEntity = new CouponEntity();
couponEntity.setCouponName("满100减10");
return R.ok().put("coupons", Arrays.asList(couponEntity));
}
7.编写一个接口,告诉SpringCloud这个接口需要调用远程服务
在mall-member模块中写一个接口
@FeignClient("mall-coupon") //调用的服务名
public interface CouponFeginService {
//用哪个接口写哪个
@RequestMapping("/coupon/coupon/member/list") //这里写的是在mall-coupon中接口的完整请求路径
public R membercoupons();
}
8.开启远程调用的功能
@EnableFeignClients(basePackages = {
"com.eternal.mall.member.fegin"})
@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(MallMemberApplication.class, args);
}
}
9.开始使用
以上是使用nacos作为注册中心使用
===================================================
以下使用nacos作为配置中心管理各个服务的配置信息
10.引入Nacos Config Starter
因为每个模块都可能使用,所以直接放在mall-common模块中
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
11.在需要使用的各个模块下的/src/main/resources/bootstrap.properties
配置文件中配置Nacos Config元数据
spring.application.name=mall-coupon #根据模块名改变
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
12.给配置中心添加一个当前服务.properties的配置文件
默认规则:应用名.properties
可以添加任何配置数据
13.获取nacos中建立的配置文件数据
边栏推荐
- What class loading mechanisms does the JVM have?
- 玩转MongoDB—搭建MongoDB集群
- Halo effect - who says that those with light on their heads are heroes
- Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
- Websocket (simple experience version)
- [NLP] pre training model - gpt1
- C language course design topic
- 我们该如何保护自己的密码?
- Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
- 建立自己的网站(21)
猜你喜欢
那个很努力的学生,高考失败了……别慌!你还有一次逆袭机会!
So programmers make so much money doing private work? It's really delicious
如何看待国企纷纷卸载微软Office改用金山WPS?
Websocket (simple experience version)
Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine
2022-2-15 learning xiangniuke project - Section 1 filtering sensitive words
643. Maximum average number of subarrays I
博文推荐 | 深入研究 Pulsar 中的消息分块
The integration of computing and Internet enables the transformation of the industry, and the mobile cloud lights up a new roadmap for the future of digital intelligence
Fundamentals of C language
随机推荐
sqlilabs less9
Research Report on the development trend and competitive strategy of the global facial wrinkle removal and beauty instrument industry
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
Journal MySQL
Research Report on the development trend and competitive strategy of the global commercial glassware industry
Research Report on the development trend and competitive strategy of the global indexable milling cutter industry
sqlilabs less10
Research Report on development trend and competitive strategy of global vibration polishing machine industry
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
【NLP】预训练模型——GPT1
Introduction to distributed transactions (Seata)
Summary of leetcode's dynamic programming 5
Effet halo - qui dit qu'il y a de la lumière sur la tête est un héros
Research Report on the development trend and competitive strategy of the global powder filling machine industry
C 语言基础
Oracle-数据库对象的使用
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system
博文推荐 | 深入研究 Pulsar 中的消息分块
日志中打印统计信息的方案