当前位置:   article > 正文

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

nacos springboot resttemplate

父工程 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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

创建生产者 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

添加配置

本地添加 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

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

jerry:
  name: 这里是 consumer-service-alibaba
  • 1
  • 2

在这里插入图片描述

添加启动类

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

添加控制器

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();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

在这里插入图片描述

创建消费者 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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

添加配置

\.\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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

添加启动类

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);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

添加 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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

添加控制器

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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

在这里插入图片描述

参考资料

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/458322
推荐阅读
相关标签
  

闽ICP备14008679号