准备工作
创建父工程
Spring Cloud Alibaba 的环境在父工程中创建,微服务的各个组件作为子工程,继承父工程的环境。
其中pom修改打包方式为pom,完整pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<groupId>com.godfrey</groupId>
<artifactId>spring-cloud-alibaba</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-alibaba</name>
<description>spring-cloud-alibaba for Spring Boot</description>
<!--打包方式-->
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring.cloud.alibaba.version>2.2.3.RELEASE</spring.cloud.alibaba.version>
<spring.cloud.version>Hoxton.SR8</spring.cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<!--Spring Cloud Alibaba-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring.cloud.alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--Spring Cloud Hoxton-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring.cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1、Nacos 服务注册
去到Nacos github下载Nacos,https://github.com/alibaba/nacos
解压,通过cmdstartup.cmd -m standalone
启动单机(非集群)服务或者修改startup.cmd set MODE="cluster"
=》set MODE="standalone"
。启动后访问http://localhost:8848/nacos 账号、密码皆为nacos
Nacos 搭建成功,接下来注册服务。
在父工程路径下创建provide子模块,,让子工程继承父工程的环境依赖,pom.xml 中添加 nacos 组件。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
application.yml 中配置
spring:
application:
name: provider
cloud:
nacos:
discovery:
# 指定nacos server地址
server-addr: localhost:8848
启动类ProviderApplication
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args)
}
}
注意:在Spring Cloud官方文档中,从Edgware开始,已经不再强制在启动类上添加@EnableDiscoveryClient注解了,如果不用Nacos作为服务注册的组件,可以添加autoRegister = false在@EnableDiscoveryClient中
2、Nacos 服务发现与调用
2.1 服务发现
在父工程路径下创建consumer子模块,,让子工程继承父工程的环境依赖,pom.xml 中添加 nacos 发现组件。
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
application.yml中配置
server:
port: 9090
启动类ConsumerApplication
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
通过 discoveryClient 发现注册到 nacos 中的 provider 服务。
@RestController
public class ConsumerController {
private final DiscoveryClient discoveryClient;
@Autowired
ConsumerController(DiscoveryClient discoveryClient){
this.discoveryClient = discoveryClient;
}
@GetMapping("/instances")
public List<ServiceInstance> getInstances() {
return discoveryClient.getInstances("provider");
}
}
1、启动Nacos
2、设置允许启动多个provider服务,通过修改端口号(8080~8082)启动三个provider服务
3、启动consumer服务
2.2 服务调用:
consumer通过RestTemplate调用provider提供的服务
先去provider controller向外提供调用方法
@RestController
public class ProviderController {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index() {
return this.port;
}
}
去consumer模块配置RestTemplate的Bean
@Configuration
public class ConsumerConfig {
@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
controller添加index方法
@RestController
public class ConsumerController {
private final DiscoveryClient discoveryClient;
private final RestTemplate restTemplate;
@Autowired
public ConsumerController(DiscoveryClient discoveryClient, RestTemplate restTemplate) {
this.discoveryClient = discoveryClient;
this.restTemplate = restTemplate;
}
@GetMapping("/instances")
public List<ServiceInstance> getInstances() {
return discoveryClient.getInstances("provider");
}
@GetMapping("/index")
public String index() {
List<ServiceInstance> instances = discoveryClient.getInstances("provider");
//随机访问服务
int index = ThreadLocalRandom.current().nextInt(instances.size());
String url = instances.get(index).getUri() + "/index";
return "consumer随机远程调用provier:" + this.restTemplate.getForObject(url, String.class);
}
}
1、启动Nacos
2、设置允许启动多个provider服务,通过修改端口号(8080~8082)启动三个provider服务
3、启动consumer服务