欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Nacos 记录2 - 入门项目

程序员文章站 2022-05-04 08:50:40
1. 版本依赖Spring Cloud VersionSpring Cloud Alibaba VersionSpring Boot VersionSpring Cloud Hoxton.SR32.2.1.RELEASE2.2.5.RELEASESpring Cloud Hoxton.RELEASE2.2.0.RELEASE2.2.X.RELEASE...

1. 版本依赖

Spring Cloud Version Spring Cloud Alibaba Version Spring Boot Version

Spring Cloud Hoxton.SR3

2.2.1.RELEASE

2.2.5.RELEASE

Spring Cloud Hoxton.RELEASE

2.2.0.RELEASE

2.2.X.RELEASE

Spring Cloud Greenwich

2.1.2.RELEASE

2.1.X.RELEASE

Spring Cloud Finchley

2.0.2.RELEASE

2.0.X.RELEASE

Spring Cloud Edgware

1.5.1.RELEASE

1.5.X.RELEASE

https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E

2. 创建dependecies工程

该工程packaging为pom

<?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.richard123m.temp</groupId>
    <artifactId>nacos-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>server-provider</module>
    </modules>

    <properties>
        <spring.boot.version>2.2.8.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.RELEASE</spring.cloud.version>
        <swagger.version>2.9.2</swagger.version>
    </properties>


    <dependencyManagement>
    <dependencies>
    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>${spring.boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        <version>2.2.0.RELEASE</version>
    </dependency>

    </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <!-- Compiler 插件,设定JDK版本 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <showWarnings>true</showWarnings>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>

    </build>

</project>

3. 服务提供

  • pom.xml
<?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-demo</artifactId>
        <groupId>com.richard123m.temp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>server-provider</artifactId>

    <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- nacos -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

    </dependencies>

</project>
  • application.yml
# 总体配置
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        server-addr: r123m:8848


# 服务本身配置
server:
  port: 8762
  servlet:
    context-path: /nacos-demo

  • 启动类Application
package com.r123m.nacos.demo;

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

/**
 * @author Richard123m
 * @date 2020-07-26
 */
@EnableDiscoveryClient
@SpringBootApplication
public class NacosDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDemoApplication.class,args);
    }
}
  • Controller类
package com.r123m.nacos.demo.controller;

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

/**
 * @author Richard123m
 * @date 2020-07-26
 */
@RestController
public class AbcController {

    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(String msg){
        return  "Hi! Msg =>"+msg;
    }


}
  • 测试接口

Nacos 记录2 - 入门项目

4. 服务消费

  • pom.xml
<?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-demo</artifactId>
        <groupId>com.richard123m.temp</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>nacos-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- nacos -->
        <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>

    </dependencies>

</project>
  • application.yml
# 本地配置
spring:
  application:
    name: nacos-consumer-feign
  cloud:
    nacos:
      discovery:
        server-addr: r123m:8848

server:
  port: 8763

  • 启动类
package com.r123m.nacos.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 Richard123m
 * @date 2020-07-26
 */
@EnableFeignClients
@EnableDiscoveryClient
@SpringBootApplication
public class NacosConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class,args);
    }


}
  • 配置服务提供的接口
package com.r123m.nacos.consumer.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Richard123m
 * @date 2020-07-26
 */
@FeignClient(value = "nacos-provider")
public interface RemoteService {

    //接口参数要加上@RequestParam,否则会报405 
    @RequestMapping(value = "/nacos-demo/hi",method = RequestMethod.GET)
    String sayHi(@RequestParam("msg") String msg);
}
  • 配置消费端接口
package com.r123m.nacos.consumer.controller;

import com.r123m.nacos.consumer.service.RemoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Richard123m
 * @date 2020-07-26
 */
@RestController
public class ConsumerController {

    @Autowired
    private RemoteService remoteService;

    @RequestMapping(value = "/feign/sayHi",method = RequestMethod.GET)
    public String sayHi(String msg){
         return remoteService.sayHi(msg+" from feign");
    }

}
  • 查看Nacos页面,服务提供者、消费者均在

Nacos 记录2 - 入门项目

  • 测试服务,表明成功调用了服务提供接口

Nacos 记录2 - 入门项目

 

本文地址:https://blog.csdn.net/c123m/article/details/107598353

相关标签: Spring