目录
见 第十六章 Spring Cloud Alibaba 基础环境搭建 对应版本如下:
| springboot | Spring Cloud | Spring Cloud Alibaba | Nacos | Sentinel | RocketMQ | Seata | Dubbo |
| 2.6.11 | 2021.0.4 | 2021.0.4.0 | 2.0.4 | 1.8.5 | 4.9.4 | 1.5.2 | ~ |
Nacos客户端服务创建的步骤是:创建独立的springboot应用、引入依赖、编写配置、创建主启动类、创建业务类、启动测试。
cloudalibaba-product-server9001
cloudalibaba-product-server9002
cloudalibaba-order-server9000
引入服务提供者 cloudalibaba-product-server9001 与 cloudalibaba-product-server9002 及服务消费者 cloudalibaba-order-server9000 的 Nacos client 依赖。三个的pom文件都相同。唯一不同就是 groupId 与 artifactId。我们以 消费者为案例。
服务消费者引入 Nacos Client 依赖springcloudalibaba com.hwadee.springcloud2022 0.0.1-SNAPSHOT 4.0.0 com.hwadee.springcloud cloudalibaba-order-server9000 com.alibaba.cloud spring-cloud-starter-alibaba-nacos-discovery org.springframework.boot spring-boot-starter-web com.hwadee.springcloud springcloud-api 0.0.1-SNAPSHOT 不同部分
com.hwadee.springcloud
cloudalibaba-order-server9000
com.hwadee.springcloud
cloudalibaba-product-server9001
com.hwadee.springcloud
cloudalibaba-product-server9002
编写服务提供者和服务消费者的配置,在配置文件中需要配置注册服务的名称和注册中心的地址。如:
配置中 spring.cloud.nacos.server-addr 是Nacos server总地址,客户端的注册地址是spring.cloud.nacos.discovery.server-addr,二者区别是spring.cloud.nacos.discovery.server-addr默认值是spring.cloud.nacos.server-addr是Nacos server总地址,因此在配置文件中可以只配置总地址也可以 即 spring.cloud.nacos.server-addr。同理,spring.cloud.nacos.discovery.service默认值也是spring.application.name,因此也可以只配置spring.application.name 。
配置文件分别如下:
server:
port: 9000
spring:
application:
name: ORDER-SERVICE-CUSTOMER
cloud:
#配置 nacos server 注册中心总的地址
nacos:
server-addr: localhost:8848 # 配置nacos server 注册中心地址
server: port: 9001 spring: application: name: PRODUCT-SERVICE-PROVIDER cloud: #配置 nacos server 注册中心总的地址 nacos: server-addr: localhost:8848 # 配置nacos server 注册中心地址
server: port: 9002 spring: application: name: PRODUCT-SERVICE-PROVIDER cloud: #配置 nacos server 注册中心总的地址 nacos: server-addr: localhost:8848 # 配置nacos server 注册中心地址
创建服务提供者和服务消费者的主启动类,分别如下:
- @SpringBootApplication
- public class OrderServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(OrderServerApplication.class, args);
- }
- }
- @SpringBootApplication
- public class ProductServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProductServerApplication.class, args);
- }
- }


消费者 order-service9000 引入 OpenFegin 依赖 ,修改pom文件,如下:
org.springframework.cloud
spring-cloud-starter-openfeign
注意:此处因为由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon,而是使用spring-cloud-loadbalancer,所以要引入spring-cloud-loadbalancer,不引入会报错。
"1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springcloudalibabaartifactId> <groupId>com.hwadee.springcloud2022groupId> <version>0.0.1-SNAPSHOTversion> parent> <modelVersion>4.0.0modelVersion> <groupId>com.hwadee.springcloudgroupId> <artifactId>cloudalibaba-order-server9000artifactId> <dependencies> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-starter-openfeignartifactId> dependency> <dependency> <groupId>org.springframework.cloudgroupId> <artifactId>spring-cloud-loadbalancerartifactId> dependency> <dependency> <groupId>com.alibaba.cloudgroupId> <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId> dependency> <dependency> <groupId>org.springframework.bootgroupId> <artifactId>spring-boot-starter-webartifactId> dependency> <dependency> <groupId>com.hwadee.springcloudgroupId> <artifactId>springcloud-apiartifactId> <version>0.0.1-SNAPSHOTversion> dependency> dependencies> project>
编写消费者 order-service9000 的 OpenFegin 接口,在订单服务中的service目录中创建 调用远端服务的接口 IOrderFeignService ,在接口上使用注解 @FeignClient("PRODUCT-SERVICE-PROVIDER"),表示 controller 调用接口方法时候,使用负载均衡机制,进行寻找服务名为 "PRODUCT-SERVICE-PROVIDER" 的远端服务,接口中的方法和controller中的方法写法类似。代码如下:
- import com.hwadee.springcloud.entity.Product;
- import org.springframework.cloud.openfeign.FeignClient;
- import org.springframework.stereotype.Component;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- @Component // 让 spring 可以识别,不加也行,但是在注入的时候 IDEA 会报错,不会影响运行,但有条红线让自己不舒服
- @FeignClient("PRODUCT-SERVICE-PROVIDER")
- public interface IOrderFeignService {
- @RequestMapping(value = "/product/buy/{id}")
- Product findOrderById(@PathVariable Long id);
- }
编写消费者 order-service9000 和 product-server9001 及 product-server9002的 Controller,分别如下:
- import com.hwadee.springcloud.entity.Product;
- import com.hwadee.springcloud.service.IOrderFeignService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- @RequestMapping("/order")
- public class OrderController {
-
- @Autowired
- IOrderFeignService orderFeignService;
-
- @RequestMapping("/buy/{id}")
- public Product buy(@PathVariable Long id) {
- Product product = orderFeignService.findOrderById(id);
- return product;
- }
- }
- import com.hwadee.springcloud.entity.Product;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.math.BigDecimal;
-
- @RestController
- @RequestMapping("/product")
- public class ProductController {
- //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip
- @Value("${server.port}")
- private String port;
- @Value("${spring.cloud.client.ip-address}")
- private String ip;
-
- @RequestMapping("/buy/{id}")
- public Product findById(@PathVariable Long id) {
- Product product = new Product();
- product.setId(id);
- // 后面需要测试负载均衡,所以返回 ip 地址及端口号
- product.setName("当前访问服务地址:" + ip + ":" + port+" "+"查询商品订单,订单号:"+id);
- product.setPrice(new BigDecimal(10000.0));
- System.out.println(product);
- return product;
- }
- }
编写消费者 order-service9000 的启动类,并增加注解 @EnableFeignClients 来启动 openFeign。代码如下:
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cloud.openfeign.EnableFeignClients;
-
-
- @SpringBootApplication
- @EnableFeignClients // 启动 feign
- public class OrderServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(OrderServerApplication.class, args);
- }
- }




第十七章:Nacos注册中心详解-入门介绍
第十九章:Nacos统一配置中心详解