其他的module中引入了mall-common模块
每个Module分别连接着一个数据库
1.开启nacos服务
2.在mall-commom中引入依赖
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><!--1.第一步:服务的注册发现-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency></pre>
3.在服务中(非common模块)的 application.yml
文件中配置nacos的地址
<pre class="prettyprint hljs yaml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">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</pre>
4.在给个Module中的启动类上配置注解 @EnableDiscoveryClient
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(MallMemberApplication.class, args);
}
}</pre>
通过以上步骤就可以在nacos看到注册进去的模块
========================================================================
接下来就要使用远程调用用来从一个服务调用另一个服务中的方法: Fegin声明式远程调用
说明:mall-member服务想要调用mall-coupon服务,那就在mall-member服务中加东西
5.在mall-member的pom.xml问价中引入依赖
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency></pre>
6.在mall-coupon中写一个方法准备让mall-member调用
<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@RequestMapping("/member/list")
public R membercoupons() {
CouponEntity couponEntity = new CouponEntity();
couponEntity.setCouponName("满100减10");
return R.ok().put("coupons", Arrays.asList(couponEntity));
}</pre>
7.编写一个接口,告诉SpringCloud这个接口需要调用远程服务
在mall-member模块中写一个接口
<pre class="prettyprint hljs kotlin" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@FeignClient("mall-coupon") //调用的服务名
public interface CouponFeginService {
//用哪个接口写哪个
@RequestMapping("/coupon/coupon/member/list") //这里写的是在mall-coupon中接口的完整请求路径
public R membercoupons();
}</pre>
8.开启远程调用的功能
<pre class="prettyprint hljs less" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">@EnableFeignClients(basePackages = { "com.eternal.mall.member.fegin"})
@SpringBootApplication
@EnableDiscoveryClient
public class MallMemberApplication {
public static void main(String[] args) {
SpringApplication.run(MallMemberApplication.class, args);
}
}</pre>
9.开始使用
以上是使用nacos作为注册中心使用
===================================================
以下使用nacos作为配置中心管理各个服务的配置信息
10.引入Nacos Config Starter
因为每个模块都可能使用,所以直接放在mall-common模块中
<pre class="prettyprint hljs xml" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;"><dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency></pre>
11.在需要使用的各个模块下的 /src/main/resources/bootstrap.properties
配置文件中配置Nacos Config元数据
<pre class="prettyprint hljs makefile" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto;">spring.application.name=mall-coupon #根据模块名改变
spring.cloud.nacos.config.server-addr=127.0.0.1:8848</pre>
12.给配置中心添加一个当前服务.properties的配置文件
默认规则: 应用名.properties
可以添加任何配置数据