栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

nacos提供者和消费者

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

nacos提供者和消费者

前话:
因为这篇博客是紧跟上一篇博客的。然后我也是初学者,有什么问题可以评论区交流。谢谢大家!

服务注册和发现

微服务的服务注册和发现相信都用过Eureka,要自己本地构建一个Eureka微服务,但是整合了Alibaba的Nacos则不用那么复杂,直接启动Alibaba提供的Nacos服务即可,这样让程序员把全部精力放在业务上,下面是一个简单的架构图:

服务提供者 新建maven工程作为项目的主工程(父工程) 1、导入依赖
    
        

            
                org.springframework.boot
                spring-boot-dependencies
                2.6.4
                pom
                import
            

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

            
                org.springframework.cloud
                spring-cloud-dependencies
                2021.0.1
                pom
                import
            

            
                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2.2.0.RELEASE
                pom
                import
            
        
    
2、新建一个子项目nacos-provider 3、导入依赖
    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    
4、编写配置
server:
  port: 9001
spring:
  application:
    name: nacos-provider
  cloud:
    nacos:
      discovery:
        # nacos的服务地址,nacos-server的ip地址:端口号
        server-addr: 127.0.0.1:8848
5、写主启动类
package com.sky;

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


@SpringBootApplication
// 一般springcloud组件导入什么就要开启什么,开启nacos的服务注册
@EnableDiscoveryClient
public class NacosDiscoveryApplication {
   public static void main(String[] args) {
       SpringApplication.run(NacosDiscoveryApplication.class,args);
   }
}

6、编写controller测试(NacosController)
package com.sky.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


 // RestController:返回页面字符串
@RestController
@RequestMapping("/nacos")
public class NacosController {

    @GetMapping("/test/{id}")

    public String test(@PathVariable("id") Integer id){
        return "accept one msg id="+id;
    }

}
7、启动项目访问localhost:9001/nacos/test/1

访问成功
我们再查看nacos控制台

服务消费者 1、导入依赖
    
        
            com.alibaba.cloud
            spring-cloud-starter-alibaba-nacos-discovery
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.cloud
            spring-cloud-starter-netflix-ribbon
        
    
2、编写配置
server:
 port: 9002
spring:
 application:
   name: nacos-consumer
 cloud:
   nacos:
     discovery:
       # nacos的服务地址
       server-addr: 127.0.0.1:8848
3、编写主启动类
package com.sky;

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


@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class,args);
    }
}
4、编写配置类
package com.sky.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;


@Configuration
public class BeanConfig {

    

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}
5、编写controller用restTemplate调用提供者方法
package com.sky.controller;

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


@RestController
@RequestMapping("/nacos")
public class NacosController {

    @Autowired
    private RestTemplate restTemplate;

    // 使用提供者的项目名
    private static final String REST_URL_PREFIX = "http://nacos-provider";
//    private static final String REST_URL_PREFIX = "http://127.0.0.1:9001";

    @GetMapping("/test/{id}")
    public String test(@PathVariable("id") Integer id){
        return restTemplate.getForObject(REST_URL_PREFIX+"/nacos/test/"+id,String.class);
    }
}
6、测试,访问http://localhost:9002/nacos/test/16

访问成功!

7、总结

nacos对比Eureka简直是简单太多了!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/830995.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号