一、SpringCloud Alibaba简介
- Spring官网:https://spring.io/projects/spring-cloud-alibaba
- GitHub:https://github.com/alibaba/spring-cloud-alibaba
- GitHub中文文档:https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md
- Spring Cloud Alibaba参考文档:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
版本对应
https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E
二、Nacos服务注册
官方文档:https://nacos.io/zh-cn/docs/what-is-nacos.html
2.1 与其他注册中心对比
2.2 切换
nacos可以切换 AP(默认) 和 CP ,可使用如下命令切换成CP模式
curl -X PUT '$NACOS_SERVER:8848/nacos/v1/ns/operator/switches?entry=serverMode&value=CP'
2.3 下载
https://github.com/alibaba/nacos/releases
下载压缩包以后解压,进入bin目录,打开dos窗口,执行startup命令启动它
startup.cmd -m standalone
可访问 : http://localhost:8848/nacos/index.html 地址,默认账号密码都是nacos
2.4 注册中心功能
1、创建父工程springcloud_alibaba
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>nacos-provider8000</module>
<module>nacos-provider8001</module>
<module>nacos-conusmer9000</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.5</version>
</parent>
<groupId>com.liming</groupId>
<artifactId>springcloud_alibaba</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<!--版本规定:规定子工程中版本不用写了-->
<dependencyManagement>
<dependencies>
<!--springcloud alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2、服务提供者1(nacos-provider8000)
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_alibaba</artifactId>
<groupId>com.liming</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-provider8000</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8000
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
主启动类
package com.liming.provider8000;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author 黎明
* @date 2023/5/11 20:31
* @version 1.0
*/
@SpringBootApplication
@EnableDiscoveryClient // nacos注册中心客户端
public class NacosProvider8000App {
public static void main(String[] args) {
SpringApplication.run(NacosProvider8000App.class,args);
}
}
controller
package com.liming.provider8000.controller;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* @author 黎明
* @version 1.0
* @date 2023/5/11 20:47
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Value("${server.port}")
int port;
@GetMapping("findById/{id}")
public String findById(@PathVariable("id") int id) {
// 业务逻辑
return "nacos provider.port:" + port + "|id:" + id;
}
}
3、服务提供者2(nacos-provider8001)
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_alibaba</artifactId>
<groupId>com.liming</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-provider8001</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 8001
spring:
application:
name: nacos-provider
cloud:
nacos:
discovery:
server-addr: localhost:8848
主启动类
package com.liming.provider8001;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @author 黎明
* @date 2023/5/11 20:54
* @version 1.0
*/
@SpringBootApplication
@EnableDiscoveryClient // 开启nacos注册中心客户端
public class NacosProvider8001App {
public static void main(String[] args) {
SpringApplication.run(NacosProvider8001App.class,args);
}
}
controller
package com.liming.provider8001.controller;
import org.springframework.beans.factory.annotation.Value;
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;
/**
* @author 黎明
* @version 1.0
* @date 2023/5/11 20:56
*/
@RestController
@RequestMapping("/goods")
public class GoodsController {
@Value("${server.port}")
int port;
@GetMapping("finById/{id}")
public String findById(@PathVariable("id") int id) {
return "nacos provider.port:" + port + "|id:" + id;
}
}
4、服务消费者(nacos-consumer9000)
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud_alibaba</artifactId>
<groupId>com.liming</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>nacos-conusmer9000</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springcloud alibaba nacos-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!--loadbalancer-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<!--openfeign-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
application.yml
server:
port: 9000
spring:
application:
name: nacos-consumer
cloud:
loadbalancer:
ribbon:
enabled: false
nacos:
discovery:
server-addr: localhost:8848
主启动类
package com.liming.consumer9000;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author 黎明
* @date 2023/5/11 21:45
* @version 1.0
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumer9000App {
public static void main(String[] args) {
SpringApplication.run(NacosConsumer9000App.class,args);
}
}
注册RestTemplate
package com.liming.consumer9000.config;
import org.springframework.boot.web.client.RestTemplateBuilder;
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;
/**
* @author 黎明
* @date 2023/5/11 21:46
* @version 1.0
*/
@Configuration
public class RestTemplateConfig {
@Bean
@LoadBalanced //loadbalancer 客户端负载均衡
public RestTemplate restTemplate(RestTemplateBuilder builder){
RestTemplate restTemplate = builder.build();
return restTemplate;
}
}
controller,调用
package com.liming.consumer9000.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;
/**
* @author 黎明
* @date 2023/5/11 21:47
* @version 1.0
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("add/{id}")
public String add(@PathVariable("id") int id){
// 业务逻辑
String url = "http://nacos-provider/goods/findById/"+id;
String result = restTemplate.getForObject(url, String.class);
return result;
}
}
测试:http://localhost:9000/order/add/1
2.5 整合feign
1.在消费者pom中导入
<!-- openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
2.在主启动类上加上@EnableFeignClients
,激活feign
3.新建feign接口
package com.liming.consumer9000.feign;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author 黎明
* @date 2023/5/11 22:11
* @version 1.0
*/
@FeignClient("nacos-provider")
public interface GoodsFeign {
@GetMapping("goods/finById/{id}")
public String findById(@PathVariable("id") int id);
}
4.controller新写方法
package com.liming.consumer9000.controller;
import com.liming.consumer9000.feign.GoodsFeign;
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;
/**
* @author 黎明
* @date 2023/5/11 21:47
* @version 1.0
*/
@RestController
@RequestMapping("/order")
public class OrderController {
@Autowired
private GoodsFeign goodsFeign;
/**
* 使用openfeign远程调用
* @param id
* @return
*/
@GetMapping("add2/{id}")
public String add2(@PathVariable("id") int id){
// 业务逻辑
String result = goodsFeign.findById(id);
return result;
}
}