pom.xml
<!--服务注册/发现--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!--配置中心来做配置管理--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency>
1、Spring Cloud Alibaba -Nacos [作为注册中心]
1)、spring-cloud-alibaba:https://github.com/alibaba/spring-cloud-alibaba
2)、 nacos文档:Nacos 快速开始
3)、下载 Nacos Server
4)、启动Nacos Server
a、双击bin中的startup.cmd文件
b、访问 http://localhost:8848/nacos/
c、使用默认的nacos/nacos进行登录
5)、将微服务注册到 nacos 中
a 、修改pom.xml,引入Nacos Discovery Starter
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency>
b、在application.yml配置文件中配置nacos地址
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
c、使用 @EnableDiscoveryClient开启服务注册发现功能
@SpringBootApplication@EnableDiscoveryClientpublic class ProviderApplication {public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
d、启动应用,查看nacos服务列表是否已经注册服务
6)、feign的远程调用
a、引入open-feign
b、开启@EnableFeignClients(basePackages = "xxx.xxx.xxx")
c、编写接口,进行远程调用
/** * 这是一个声明式的远程调用 */@FeignClient("stores") public interface StoreClient { @RequestMapping(method = RequestMethod.GET, value = "/stores") List<Store> getStores(); @RequestMapping(method = RequestMethod.POST, value = "/stores/{storeId}", consumes = "application/json") Store update(@PathVariable("storeId") Long storeId, Store store); }