Nacos 学习笔记:Spring-Cloud-Alibaba 整合 Nacos

Nacos 同时被 2 个专栏收录
6 篇文章 0 订阅

父工程 nacos-spring-cloud-alibaba-demo

添加依赖

\nacos-spring-cloud-alibaba-demo\pom.xml
子项目要使用Springboot,但又要继承父项目,所以在父项目中import相关依赖,代替继承操作。

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.jerry</groupId>
    <artifactId>nacos-spring-cloud-alibaba-demo</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>

    <name>${artifactId}</name>
    <description>学习 nacos 配置管理</description>

    <modules>
        <module>nacos-spring-cloud-alibaba-provider</module>
        <module>nacos-spring-cloud-alibaba-consumer</module>
    </modules>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!--修改 Language level-->
        <maven.compiler.source>1.8</maven.compiler.source>
        <!--修改 Java Compiler-->
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <dependencyManagement>
        <dependencies>
            <!-- spring-cloud-dependencies -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-cloud-alibab-dependencies -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- spring-boot-dependencies -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

创建生产者 nacos-spring-cloud-alibaba-provider

添加依赖

\.\nacos-spring-cloud-alibaba-provider\pom.xml

依赖说明添加与否
web服务生产者消费者都要对外提供 restful 接口两个都添加
服务注册发现生产者消费者都要注册到nacos两个都添加
配置管理针对本demo只有生产 者读了配置只有生产者添加
<?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>nacos-spring-cloud-alibaba-demo</artifactId>
        <groupId>com.jerry</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-spring-cloud-alibaba-provider</artifactId>

    <dependencies>
        <!--web服务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--配置管理-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
    </dependencies>
</project>

添加配置

本地添加 SpringCloud 配置文件 bootstrap.yml

\.\nacos-spring-cloud-alibaba-provider\src\main\resources\bootstrap.yml

server:
  port: 8085

spring:
  application:
    name: nacos-alibaba-provider
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml

线上添加 SpringBoot 配置文件 nacos-alibaba-provider.yml

jerry:
  name: 这里是 consumer-service-alibaba

在这里插入图片描述

添加启动类

com.jerry.nacos.discovery.provider.NacosProviderApplication

package com.jerry.nacos.discovery.provider;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @author jerryjin
 */
@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosProviderApplication.class, args);
    }
}

添加控制器

com.jerry.nacos.discovery.provider.controller.ProviderController

package com.jerry.nacos.discovery.provider.controller;

import com.alibaba.nacos.api.annotation.NacosProperties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 * @author jerryjin
 */
@RestController
@RefreshScope
public class ProviderController {
    @Value("${jerry.name:666}")
    private String name;
    @GetMapping("/hi")
    public String hi(){
        return name + " " + LocalDateTime.now().toString();
    }
}

在这里插入图片描述

创建消费者 nacos-spring-cloud-alibaba-consumer

添加依赖

\.\nacos-spring-cloud-alibaba-consumer\pom.xml

依赖说明添加与否
web服务生产者消费者都要对外提供 restful 接口两个都添加
服务注册发现生产者消费者都要注册到nacos两个都添加
声明式HTTP客户端针对本demo只有消费者使用了 feign 调用别人只有消费者添加
<?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>nacos-spring-cloud-alibaba-demo</artifactId>
        <groupId>com.jerry</groupId>
        <version>1.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-spring-cloud-alibaba-consumer</artifactId>

    <dependencies>
        <!--web服务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--服务注册发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--声明式HTTP客户端-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>
</project>

添加配置

\.\nacos-spring-cloud-alibaba-consumer\src\main\resources\application.yml

server:
  port: 8086

spring:
  application:
    name: nacos-alibaba-consumer
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
logging:
  level:
    root: info
    org.springframework: info

添加启动类

com.jerry.nacos.discovery.consumer.NacosConsumerApplication

package  com.jerry.nacos.discovery.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

/**
 * @author jerryjin
 */
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }
}

添加 FeignClient

消费者通过Feign调用生产者的接口:

  1. 消费者中定义interface
  2. 添加@FeignClient注解,值标识生产者服务名。例如:nacos-alibaba-provider
  3. interface中定义胡方法与生产者的controller对应。

com.jerry.nacos.discovery.consumer.client.ProviderClient

  • @FeignClient(“nacos-alibaba-provider”) // 生产者的服务名 nacos-alibaba-provider
  • 接口对应生产者的控制器。
package com.jerry.nacos.discovery.consumer.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;

/**
 * @author jerryjin
 */
@FeignClient("nacos-alibaba-provider")
public interface ProviderClient {
    @GetMapping("/hi")
    String hi();
}

添加控制器

com.jerry.nacos.discovery.consumer.controller.ConsumerController

  • 注入 FeignClient 直接调用即可。
package com.jerry.nacos.discovery.consumer.controller;

import com.jerry.nacos.discovery.consumer.client.ProviderClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author jerryjin
 */
@RestController
public class ConsumerController {
    @Resource
    ProviderClient providerClient;
    @GetMapping("/hi")
    public String hi(){
        String hi = providerClient.hi();
        return "消费者调用了生产者,生产者说:" + hi;
    }
}

在这里插入图片描述

参考资料

Nacos Spring Cloud 快速开始
官网:配置项详解:Spring Cloud Alibaba Nacos Config
官网:Wiki 配置项详解:Spring Cloud Alibaba Nacos Config

笑虾CSDN Nacos 学习笔记:SpringBoot 整合 Nacos 配置管理、服务发现
笑虾码云 spring-cloud-alibaba-demo 源码

SpringCloud官网:7. Declarative REST Client: Feign

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值