Spring Cloud Alibaba 02:Nacos服務治理
準備工作
建立父工程
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.cmdset 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服務