spring cloud nacos服务搭建

      基于nacos搭建的父项目、spring cloud nacos provider(服务提供者)、spring cloud nacos consumer(服务消费者)结构:

spring cloud nacos服务搭建_第1张图片

1. 创建一个maven父项目。

        maven父项目包含springcloudnacos-provider(服务提供者)、springcloudnacos-consumer(服务消费者)两个子模块。父模块下的src可以删除。

        maven父项目的pom.xml:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent

        2.2.4.RELEASE
         
    
    com.example
    springcloudnacos
    0.0.1-SNAPSHOT
    springcloudnacos
    Demo project for Spring Boot
    pom
    
        1.8
        2.2.4.RELEASE
        Hoxton.RELEASE
        2.2.0.RELEASE
    

    
        springcloudnacos-provider
        springcloudnacos-consumer
    

    
        
            org.springframework.boot
            spring-boot-starter
        

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

    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies

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

                ${spring-boot-dependencies.version}
                pom
                import
            
            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies

                ${spring-cloud-alibaba.version}
                pom
                import
            
        
    

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

            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


2. 服务提供者:springcloudnacos-provider

(1)spring cloud nacos provider项目的pom.xml



    4.0.0






    
        com.example
        springcloudnacos
        0.0.1-SNAPSHOT
    

    com.example
    springcloudnacos-provider
    0.0.1-SNAPSHOT
    springcloudnacos-provider
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

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

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

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

    
        

            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    




(2)spring cloud nacos provider服务的启动类

package com.example.springcloudnacosprovider;

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

@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudnacosProviderApplication {

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

}

(3)spring cloud nacos provider服务的yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: nacos-provider
server:
  port: 8090

(4)spring cloud nacos provider服务的测试类

package com.example.springcloudnacosprovider.controller;

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

@RestController
public class NacosProviderController {

    @RequestMapping("/testNacosProvider")
    public String testNacosProvider(){
        return "this is my nacos provider!";
    }


}

(5)spring cloud nacos provider服务测试

spring cloud nacos服务搭建_第2张图片

(6)spring cloud nacos provider服务在nacos上的注册(nacos服务注册中心)

3. 服务消费者:springcloudnacos-consumer

(1)spring cloud nacos consumer服务的pom.xml



    4.0.0






    
        com.example
        springcloudnacos
        0.0.1-SNAPSHOT
    

    com.example
    springcloudnacos-consumer
    0.0.1-SNAPSHOT
    springcloudnacos-consumer
    Demo project for Spring Boot
    
        1.8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

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

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

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

        
            org.springframework.cloud
            spring-cloud-loadbalancer
        

    

    
        
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
    


(2)spring cloud nacos consumer服务的启动类

package com.example.springcloudnacosconsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class SpringcloudnacosConsumerApplication {

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

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return  new RestTemplate();
    }

}

(3)spring cloud nacos consumer服务的yml配置

spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
  application:
    name: nacos-consumer
server:
  port: 8091

(4)spring cloud nacos consumer服务的测试类

package com.example.springcloudnacosconsumer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class NacosConsumerController {

    private RestTemplate restTemplate;

    @Autowired
    public NacosConsumerController(RestTemplate restTemplate){

        this.restTemplate = restTemplate;

    }

    @RequestMapping("/testNacosConsumer")
    public String testNacosConsumer(){

        return restTemplate.getForObject("http://nacos-provider/testNacosProvider",String.class);

    }

}

(5)spring cloud nacos consumer服务测试

spring cloud nacos服务搭建_第3张图片

(6)spring cloud nacos consumer服务在nacos服务器上的注册(nacos服务注册中心)

你可能感兴趣的:(java进阶之路,spring,cloud,java,spring)