第十八章 Nacos注册中心详解-入门案例及服务通信

目录

一、Spring Cloud Alibaba 基础环境搭建

二、Nacos客户端服务环境搭建

2.1 创建基于Nacos的服务消费者和提供者

2.2 引入 Nacos Client 依赖

2.3 编写配置,服务注册到 Nacos 中心

2.4 创建主启动类

2.5 启动测试

三、服务间通信

1. 服务将通信方式

2. 使用Rest通信方式实现服务通信

3. 使用OpenFegin接口实现负载均衡及通信 

4. 启动测试


一、Spring Cloud Alibaba 基础环境搭建

      见 第十六章 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客户端服务环境搭建

        Nacos客户端服务创建的步骤是:创建独立的springboot应用、引入依赖、编写配置、创建主启动类、创建业务类、启动测试。

2.1 创建基于Nacos的服务消费者和提供者

  •  新建两个服务提供者和一个服务消费者模块,分别为:    

        cloudalibaba-product-server9001

        cloudalibaba-product-server9002

        cloudalibaba-order-server9000

2.2 引入 Nacos Client 依赖

  •  服务提供者和服务消费者引入Nacos客户端依赖

        引入服务提供者 cloudalibaba-product-server9001 与  cloudalibaba-product-server9002 及服务消费者 cloudalibaba-order-server9000 的 Nacos client 依赖。三个的pom文件都相同。唯一不同就是 groupId 与 artifactId。我们以 消费者为案例。

服务消费者引入 Nacos Client 依赖
<?xml version="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>springcloudalibaba</artifactId>
        <groupId>com.hwadee.springcloud2022</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
   
    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>cloudalibaba-order-server9000</artifactId>

    <dependencies>
        <!-- 引入Nacos Client 客户端依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 管理公共api -->
        <dependency>
            <groupId>com.hwadee.springcloud</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

不同部分

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>cloudalibaba-order-server9000</artifactId>

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>cloudalibaba-product-server9001</artifactId>

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>cloudalibaba-product-server9002</artifactId>

2.3 编写配置,服务注册到 Nacos 中心

     编写服务提供者和服务消费者的配置,在配置文件中需要配置注册服务的名称和注册中心的地址。如:

  • 注册服务的名称 spring.application.name=ORDER-SERVICE-CUSTOMER
  • 注册服务的名称 spring.cloud.nacos.discovery.service=${spring.application.name}
  • 注册的地址 spring.cloud.nacos.server-addr=localhost:8848
  • 注册的地址 spring.cloud.nacos.discovery.server-addr=${spring.cloud.nacos.server-addr}

       配置中 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 。

        配置文件分别如下:

  • 服务消费者 order-server 配置文件
server:
  port: 9000

spring:
  application:
    name: ORDER-SERVICE-CUSTOMER
  cloud:
    #配置 nacos server 注册中心总的地址
    nacos:
      server-addr: localhost:8848 # 配置nacos server 注册中心地址
  • 服务提供者 product-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 注册中心地址

2.4 创建主启动类

       创建服务提供者和服务消费者的主启动类,分别如下:

  • OrderServerApplication
@SpringBootApplication
public class OrderServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServerApplication.class, args);
    }
}
  • ProductServerApplication
@SpringBootApplication
public class ProductServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductServerApplication.class, args);
    }
}

2.5 启动测试

三、服务间通信

1. 服务将通信方式

  •   1、Http Rest(推荐)
  •   2、RPC(不推荐)

2. 使用Rest通信方式实现服务通信

  •   1、RestTemplate(不推荐)
  •   2、RestTemplate + Ribbon(不推荐)
  •   3、OpenFeign(推荐)

3. 使用OpenFegin接口实现负载均衡及通信 

  • 引入依赖

       消费者 order-service9000 引入 OpenFegin 依赖 ,修改pom文件,如下:

  <!-- openfeign相关 开始 -->
        <!--告诉微服务注册中心我是微服务的client端 openfeign 需要和Nacos整合-->
        <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
<!-- openfeign相关 结束 -->

注意:此处因为由于SpringCloud Feign在Hoxton.M2 RELEASED版本之后不再使用Ribbon,而是使用spring-cloud-loadbalancer,所以要引入spring-cloud-loadbalancer,不引入会报错。

<?xml version="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>springcloudalibaba</artifactId>
        <groupId>com.hwadee.springcloud2022</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>cloudalibaba-order-server9000</artifactId>

    <dependencies>
        <!-- openfeign相关 开始 -->
        <!--告诉微服务注册中心我是微服务的client端 openfeign 需要和eureak整合-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <!-- openfeign相关 结束 -->
   
        <!-- 引入Nacos Client 客户端依赖 -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 管理公共api -->
        <dependency>
            <groupId>com.hwadee.springcloud</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>
  • 编写消费者 OpenFegin 接口

        编写消费者 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);
}
  • 创建controller

       编写消费者 order-service9000 和 product-server9001 及 product-server9002的 Controller,分别如下:

  • OrderController
  • 在订单服务中创建 OrderController ,在 OrderController 中注入 IOrderFeignService  接口,通过 IOrderFeignService 接口方法 调用远端服务,代码如下:
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;
    }
}
  • ProductController
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);
    }
}

4. 启动测试

  • 启动PRODUCTER-SERVICE
  • 启动ORDER-SERVICE
  • 浏览器查看  http://localhost:9001/product/buy/1 、  http://localhost:9002/product/buy/1 、  http://localhost:9000/order/buy/1 

第十七章:Nacos注册中心详解-入门介绍

第十九章:Nacos统一配置中心详解

猜你喜欢

转载自blog.csdn.net/qq_41946216/article/details/127568411
今日推荐