Spring-Clould-Alibaba-Nacos
概述:
- Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,
- 方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。
- 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,
- 就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。
版本介绍:
![Spring-Clould-Alibaba-Nacos_第1张图片](http://img.e-com-net.com/image/info8/51134a308fbc44bfa137a804ec552432.jpg)
![Spring-Clould-Alibaba-Nacos_第2张图片](http://img.e-com-net.com/image/info8/55863f8a828f47d59558d7c4ff80440b.jpg)
Nacos概述
- 什么是Nacos
- Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,
- 帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。
![Spring-Clould-Alibaba-Nacos_第3张图片](http://img.e-com-net.com/image/info8/a14fe86d3d914b78a815c12ac89f3883.jpg)
- 搭建NacosServer
1.下载 官网地址
2.网盘地址:百度网盘 提取码:rest
3.启动
![Spring-Clould-Alibaba-Nacos_第4张图片](http://img.e-com-net.com/image/info8/183411855bb042e68881159a515e5048.jpg)
双击startup.cmd
![Spring-Clould-Alibaba-Nacos_第5张图片](http://img.e-com-net.com/image/info8/9cf4f52c49214911a7f68a75f08379f5.jpg)
浏览器输入http://localhost:8848/nacos/
![Spring-Clould-Alibaba-Nacos_第6张图片](http://img.e-com-net.com/image/info8/0765318b93ae4805a5767acba3067ebf.jpg)
用户名和密码都是 nacos
![Spring-Clould-Alibaba-Nacos_第7张图片](http://img.e-com-net.com/image/info8/085276ef8ea544e4b087bdf18834550d.jpg)
![Spring-Clould-Alibaba-Nacos_第8张图片](http://img.e-com-net.com/image/info8/30ad0712685a4a7bbf02062dec694033.jpg)
服务注册与发现
1. 创建父工程,在父工程当中导入依赖
![Spring-Clould-Alibaba-Nacos_第9张图片](http://img.e-com-net.com/image/info8/cf771a7b1e9246488dfdb824305f388d.jpg)
![Spring-Clould-Alibaba-Nacos_第10张图片](http://img.e-com-net.com/image/info8/58ede550e9294752b952f24f25389346.jpg)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.3.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
- 创建两个子工程user与goods工程
![Spring-Clould-Alibaba-Nacos_第11张图片](http://img.e-com-net.com/image/info8/44a637203736456cb3657c5b095789dc.jpg)
![Spring-Clould-Alibaba-Nacos_第12张图片](http://img.e-com-net.com/image/info8/da6aff6b18394247abb291627ba4cbf9.jpg)
- 导入依赖
![Spring-Clould-Alibaba-Nacos_第13张图片](http://img.e-com-net.com/image/info8/74af91ddf9b54fe6ae3c358e9753778b.jpg)
- 在启动类添加@EnableDiscoveryClient注解
![Spring-Clould-Alibaba-Nacos_第14张图片](http://img.e-com-net.com/image/info8/ebdc9482a97a43448e738e6a272d8fec.jpg)
- 创建util包和ResponseResult类用于返回信息
![Spring-Clould-Alibaba-Nacos_第15张图片](http://img.e-com-net.com/image/info8/ec961c3733e84e3cb0ea3ce0f89c266b.jpg)
public class ResponseResult extends HashMap {
public static String SUCCESS_CODE = "200";
public static String ERROR_CODE = "500";
public static String DATA_KEY = "data";
public static String MSG_KEY = "msg";
private ResponseResult() {
}
public ResponseResult set(String key, Object object) {
super.put(key, object);
return this;
}
private static ResponseResult newResponseResult() {
return new ResponseResult();
}
public static ResponseResult success() {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, "操作成功");
}
public static ResponseResult success(String msg) {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, msg);
}
public static ResponseResult success(String msg, Object object) {
return ResponseResult.newResponseResult()
.set("code", ResponseResult.SUCCESS_CODE)
.set(ResponseResult.MSG_KEY, msg)
.set(ResponseResult.DATA_KEY, object);
}
public ResponseResult data(Object obj) {
return this.set("data", obj);
}
public static ResponseResult error() {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, "操作失败")
.set("code", ResponseResult.ERROR_CODE);
}
public static ResponseResult error(String msg) {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, msg)
.set("code", ResponseResult.ERROR_CODE);
}
public static ResponseResult error(String msg, Object object) {
return ResponseResult.newResponseResult()
.set(ResponseResult.MSG_KEY, msg)
.set(ResponseResult.DATA_KEY, object)
.set("code", ResponseResult.ERROR_CODE);
}
}
- 创建controller
![Spring-Clould-Alibaba-Nacos_第16张图片](http://img.e-com-net.com/image/info8/0ee717fc1f714726954a29958a8f2abe.jpg)
@RestController
public class goodsController {
@RequestMapping("/getGoods")
public ResponseResult getGoods(){
return ResponseResult.success("操作成功");
}
}
- 在配置文件添加添加配置
![Spring-Clould-Alibaba-Nacos_第17张图片](http://img.e-com-net.com/image/info8/8bdb09faf9c4487c83524ef453e6e054.jpg)
spring:
application:
name: goods-provide
cloud:
nacos:
discovery:
server-addr: localhost:8848 #nacos服务的地址 不要加http
server:
port: 8001
- 启动运行,在nacos服务端查看
![Spring-Clould-Alibaba-Nacos_第18张图片](http://img.e-com-net.com/image/info8/ed1a1eb9a177408a9179acbeec2f6b8e.jpg)
- 使用相同方式 把user注册到nacos上
![Spring-Clould-Alibaba-Nacos_第19张图片](http://img.e-com-net.com/image/info8/7b778bb58f7f4d0383e05ab6f024c829.jpg)
![Spring-Clould-Alibaba-Nacos_第20张图片](http://img.e-com-net.com/image/info8/b36a3330060b40008041c2bde50a13a0.jpg)
- 6.在user工程中通过服务发现调用goods工程
在user的启动类中添加RestTemplate
![Spring-Clould-Alibaba-Nacos_第21张图片](http://img.e-com-net.com/image/info8/c0e14208a1a940f0a25c951784f229a9.jpg)
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
在controller中添加
![Spring-Clould-Alibaba-Nacos_第22张图片](http://img.e-com-net.com/image/info8/d933ae9cdf3d451f8e9bc254b0c6c754.jpg)
@Autowired
public RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/getGoods")
public ResponseResult getGoods() {
List<ServiceInstance> instances = discoveryClient.getInstances("goods-provide");
System.out.println(instances);
ServiceInstance serviceInstance = instances.get(0);
String url = serviceInstance.getUri()+"/getGoods".toString();
return ResponseResult.success("操作成功",
restTemplate.getForObject(url,Object.class));
}
启动访问http://localhost:8000/getGoods
![Spring-Clould-Alibaba-Nacos_第23张图片](http://img.e-com-net.com/image/info8/1426cc6994e24cdeb46229bc9ec5fa69.jpg)