Springboot+nacos实现分布式项目

目录

nacos服务器

        springboot项目准备

        提供者

        消费者

        测试


nacos服务器

nacos服务器:https://pan.baidu.com/s/1AdUIuroPQmiT4iysrjpbHQ

提取码:xxnn

我是在linux系统里解压使用,解压完成后找到解压目录运行bin目录下的startup.sh即可

单机模式运行:/usr/local/是我的解压目录,替换成自己的即可

bash /usr/local/nacos/bin/startup.sh -m standalone

 使用命令查看已开启的端口

netstat -nltp

或者直接搜索8848端口

netstat -nltp |grep 8848

nacos启动大概需要1分钟时间,趁着这时间去建项目

springboot项目准备

提供者

ProService
package com.example.pro.service;

public interface ProService {
    String list();
}
ProServiceImpl
package com.example.pro.service.impl;

import com.example.pro.service.ProService;
import org.springframework.stereotype.Service;

@Service
public class ProServiceImpl implements ProService {

    @Override
    public String list() {
        return "这里是pro";
    }
}
ProController
package com.example.pro.controller;

import com.example.pro.service.ProService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pro")
public class ProController {

    @Autowired
    ProService ts;

    @GetMapping
    String query(String msg){
        return ts.list()+msg;
    }
}

消费者

建包流程以及依赖和提供者一样,这里就不再展示

完成后在启动类上加@EnableFeignClients注解

package com.example.cons;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
public class ConsApplication {

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

}

 ConsService

package com.example.cons.service;

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

@FeignClient(value = "pro")//指定消费者服务名称
public interface ConsService {

    @GetMapping("/pro")//请求方法标注以及请求api
    String query(@RequestParam("msg") String msg);//传参使用注解
}

ConsController

package com.example.cons.controller;

import com.example.cons.service.ConsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/cons")
@RestController
public class ConsController {

    @Autowired
    ConsService ts;

    @GetMapping
    String query(String msg){
        return ts.query(msg);
    }
}

OK,先启动提供者,再启动消费者

测试

打开postman

这就是springboot+nacos最基础的实现啦~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心心丶念念

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值