Nacos 记录2 - 入门项目

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



    4.0.0

    com.richard123m.temp
    nacos-demo
    pom
    1.0-SNAPSHOT
    
        server-provider
    

    
        2.2.8.RELEASE
        Hoxton.RELEASE
        2.9.2
    


    
    
    
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring.cloud.version}
        pom
        import
    
    
        org.springframework.boot
        spring-boot-starter-parent
        ${spring.boot.version}
        pom
        import
    

    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
        2.2.0.RELEASE
    

    
    

    
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    true
                    1.8
                    1.8
                
            
        

    

3. 服务提供

  • pom.xml


    
        nacos-demo
        com.richard123m.temp
        1.0-SNAPSHOT
    
    4.0.0

    server-provider

    
    
        org.springframework.boot
        spring-boot-starter-test
        test
    

    
        org.springframework.boot
        spring-boot-starter-web
    

    
    
        com.alibaba.cloud
        spring-cloud-starter-alibaba-nacos-discovery
    

    

  • 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 - 入门项目_第1张图片

4. 服务消费

  • pom.xml


    
        nacos-demo
        com.richard123m.temp
        1.0-SNAPSHOT
    
    4.0.0

    nacos-consumer

    
        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        

        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        

    

  • 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 - 入门项目_第2张图片

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

Nacos 记录2 - 入门项目_第3张图片

项目代码:   https://download.csdn.net/download/c123m/12663321

你可能感兴趣的:(Spring)