SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)

1、Nacos

https://github.com/alibaba/nacos/releases
SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第1张图片
SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第2张图片

http://10.17.36.27:8848/nacos

SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第3张图片
SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第4张图片

2、简单例子演示

2.1 父级Maven项目



    4.0.0
    com.cntaiping.tpa
    alibaba
    1.0-SNAPSHOT
    pom

    
        UTF-8
        UTF-8
        1.8
        Greenwich.SR1
    

    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.5.RELEASE
        
    

    
        nacos-provider
        nacos-consumer
    

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

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

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            repackage
                        
                    
                
            
        
    


2.2 构建nacos-provider模块

(1)pom.xml



    4.0.0
    com.cntaiping.tpa
    nacos-provider
    0.0.1-SNAPSHOT
    jar
    nacos-provider
    Demo project for Spring Boot

    
        com.cntaiping.tpa
        alibaba
        1.0-SNAPSHOT
    

    
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            0.9.0.RELEASE
        
    



(2)application.properties

server.port= 8701
spring.application.name= nacos-provider
spring.cloud.nacos.discovery.server-addr= 127.0.0.1:8848

(3)Application类

package com.cntaiping.tpa.nacosprovider;

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

@SpringBootApplication
@EnableDiscoveryClient
public class NacosProviderApplication {

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

}

(4)控制器

package com.cntaiping.tpa.nacosprovider.comtroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    private Logger logger= LoggerFactory.getLogger(getClass());

    @GetMapping("/hello")
    public String hello(@RequestParam(value = "name",defaultValue = "hadron",required = false)String name){
        logger.info("响应服务消费请求....");
        return "Hello "+name;
    }
}

2.3 构建nacos-consumer模块

(1) pom.xml



    4.0.0
    com.cntaiping.tpa
    nacos-consumer
    0.0.1-SNAPSHOT
    jar
    nacos-consumer
    Demo project for Spring Boot

    
        com.cntaiping.tpa
        alibaba
        1.0-SNAPSHOT
    

    
        
            org.springframework.cloud
            spring-cloud-starter-alibaba-nacos-discovery
            0.9.0.RELEASE
        
        
            org.springframework.cloud
            spring-cloud-starter-openfeign
        
    



(2)application.properties

server.port= 8702
spring.application.name= nacos-consumer
spring.cloud.nacos.discovery.server-addr= 127.0.0.1:8848

(3)Application类

package com.cntaiping.tpa.nacosconsumer;

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
public class NacosConsumerApplication {

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

}

(4)服务消费类

package com.cntaiping.tpa.nacosconsumer.service;

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

@FeignClient("nacos-provider")
public interface SchedualService {
    @GetMapping("/hello")
    String hello(@RequestParam(value = "name", defaultValue = "hadron", required = false) String name);
}

package com.cntaiping.tpa.nacosconsumer.controller;

import com.cntaiping.tpa.nacosconsumer.service.SchedualService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @Autowired
    private SchedualService schedualService;

    @GetMapping("/hi")
    public String hiFeign(){
        return schedualService.hello("feign");
    }

}

2.4 运行效果

SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第5张图片
http://localhost:8702/hi
SpringCloud 2.x学习笔记:18、使用Nacos作为服务注册发现组件(Greenwich版本)_第6张图片

你可能感兴趣的:(SpringCloud,2.x学习笔记)