SpringCloud-Nacos

奋斗吧
奋斗吧
擅长邻域:未填写

标签: SpringCloud-Nacos Java博客 51CTO博客

2023-07-12 18:24:22 111浏览

SpringCloud-Nacos,项目结构如下:spring-cloud-api:提供feign接口spring-cloud-consumer:消费者spring-cloud-provider:生产者父工程引入依赖在pom.xml文件中,主要引入SpringCloudNacosDiscovery。引入spring-cloud-starter-alibaba-nacos-discovery依赖,将Nacos作为注册中心,并实现

项目结构如下:

SpringCloud-Nacos_spring


spring-cloud-api:提供feign接口

spring-cloud-consumer:消费者

spring-cloud-provider:生产者

父工程引入依赖

在pom.xml文件中,主要引入Spring Cloud Nacos Discovery。

引入spring-cloud-starter-alibaba-nacos-discovery依赖,将Nacos作为注册中心,并实现对它的自动配置,它的内部同时也引入了ribbon。

<?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.spring.cloud</groupId>
    <artifactId>spring-cloud-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <modules>
        <module>spring-cloud-consumer</module>
        <module>spring-cloud-provider</module>
        <module>spring-cloud-api</module>
    </modules>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
        <spring-boot.version>2.4.1</spring-boot.version>
        <spring-cloud.version>2020.0.0</spring-cloud.version>
        <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    </properties>


    <!--子项目会继承此版本-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--    子项目会继承其中的依赖-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <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>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

spring-cloud-api:feign接口

代码如下:

package com.spring.api;

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

@FeignClient(value = "provider")    // 添加FeignClient,指定服务ID
public interface ProviderClient {
    /**
     * 声明一个feign的接口,它的实现是服务提供者的controller实现
     */
    @GetMapping("/provider/test")
    String test();
}


spring-cloud-provider:生产者

1、创建application.yml文件,添加Nacos Discovery配置项。配置如下:

重点看spring.cloud.nacos.discovery配置项,它是Nacos Discovery配置项的前缀,对应NacosDiscoveryProperties配置项。

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        service: ${spring.application.name} # 注册到 Nacos 的服务名。默认值为 ${spring.application.name}。
  application:
    name: provider
server:
  port: 8081

2、创建应用启动类,并提供接口。代码如下:

@SpringBootApplication详解,被添加在类上,声明这是一个Spring Boot应用。Spring Cloud是构建在SpringBoot之上的,所以 需要添加。

@EnableDiscoveryClient,开启Spring Cloud的注册发现功能,不过从Spring Boot Edgware版本开始,实际上已经不需要添加@EnableDiscoveryClient详解,只需要引入Spring Cloud注册发现组件,就会自动开启注册发现的版本。

package com.spring.provider;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
@Slf4j
public class ProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(ProviderApplication.class);
        log.info("Provider application is running ......");
    }
}
package com.spring.provider.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("provider")
public class ProviderController {

    @GetMapping("/test")
    public String test(){

        return "test";
    }
}

3、运行生产者的应用服务,服务已经注册到Nacos上。

SpringCloud-Nacos_spring_02

spring-cloud-consumer:消费者

1、创建application.yml文件,添加Nacos Discovery配置项。配置如下:

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        service: ${spring.application.name} # 注册到 Nacos 的服务名。默认值为 ${spring.application.name}。
  application:
    name: consumer
server:
  port: 8082

2、创建应用启动类,并提供接口。代码如下:

package com.spring.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = "com.spring.api")
@Slf4j
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
        log.info("Consumer application is running ......");
    }
}
package com.spring.consumer.controller;

import com.spring.api.ProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ProviderClient client;

    @GetMapping("test")
    public String test(){
        String result = client.test();
        return result;
    }
}

3、消费者pom.xml文件添加loadbalancer依赖。

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-loadbalancer</artifactId>
 </dependency>

4、启动消费者应用程序,服务已经注册到Nacos上。

SpringCloud-Nacos_nacos_03

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

客服QQ 1913284695