目录
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最基础的实现啦~