Spring Cloud Alibaba 01_使用 nacos 实现服务治理
<?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.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.blu</groupId>
<artifactId>springcloudalibabademo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloudalibabademo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR3</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
服务注册
2.2.1.RELEASE 版本的 spring-cloud-alibaba 对应的 nacos 版本为 1.2.1
下载nacos 1.2.1:https://github.com/alibaba/nacos/releases/tag/1.2.1
解压启动后,访问nacos(初始用户名和密码都是nacos):http://localhost:8848/nacos/index.html
创建 spring Boot 子模块 provider
将子模块 provider 的 parent 标签修改为父工程信息
添加 nacos 的依赖,版本(2.2.1.RELEASE)与父工程的 spring-cloud-alibaba 版本一致:
<parent>
<groupId>com.blu</groupId>
<artifactId>springcloudalibabademo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<?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>com.blu</groupId>
<artifactId>springcloudalibabademo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.blu</groupId>
<artifactId>provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
编写 application.yml 配置文件:
spring:
cloud:
nacos:
discovery:
server-addr: localhost:8848
application:
name: provider
server:
port: 8080
ProviderController
package com.blu.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProviderController {
@Value("${server.port}")
private String port;
@GetMapping("/index")
public String index(){
return this.port;
}
}
启动,服务注册成功:
修改端口号,启动3个实例
如果出现以下窗口,在Run/Debug Configurations 中勾选 Allow parallel on 即可启动多个实例:
实例数变为了3:
点击详情可查看和配置集群信息:
服务发现
创建 spring Boot 子模块 consumer
修改 parent 标签并添加 nacos 的依赖
<?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>com.blu</groupId>
<artifactId>springcloudalibabademo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.blu</groupId>
<artifactId>consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
将端口设置为 8100 :
server:
port: 8100
ConsumerConfiguration(注入RestTemplate):
package com.blu.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ConsumerConfiguration {
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
ConsumerController :
package com.blu.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
@RestController
public class ConsumerController {
@Autowired
private DiscoveryClient discoveryClient;
//RestTemplate不会自动装载,不能直接用,需要在配置类中配置
@Autowired
private RestTemplate restTemplate;
@GetMapping("/instances")
public List<ServiceInstance> instances(){
List<ServiceInstance> provider = discoveryClient.getInstances("provider");
return provider;
}
@GetMapping("index")
public String index(){
//调用provider服务的index方法
//1. 找到provider实例
List<ServiceInstance> list = discoveryClient.getInstances("provider");
//2. 产生随机数0,1,2
int index = ThreadLocalRandom.current().nextInt(list.size());
//3. 根据随机数0,1,2随机获取服务的3个实例
ServiceInstance serviceInstance = list.get(index);
//4. 获取服务的uri: localhost:8080
String uri = serviceInstance.getUri().toString();
//5. 拼接url: localhost:8080/index
String url = uri + "/index";
//使用RestTemplate调用
String result = restTemplate.getForObject(url, String.class);
return "调用的端口为:"+ result;
}
}
启动访问:http://localhost:8100/instances
访问:http://localhost:8100/index
这里访问的服务实例是随机的
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blucoding.blog.csdn.net/article/details/109162166
内容来源于网络,如有侵权,请联系作者删除!