本文使用RestTemplate+Ribbon来测试服务之间的调用,并使用Nacos作为服务注册中心,所以需要用到两个服务,生产者与消费者。
启动Nacos-server
启动Nacos-server作为服务注册中心。
首先下载Nacos-server,这里使用的是V1.1.4。
下载地址:https://github.com/alibaba/nacos/releases/1.1.4
下载解压后进入bin文件夹(目录:nacos\nacos\bin),直接双击执行startup.cmd文件,启动成功如下图:
启动成功后,此时Nacos控制台就可以访问了;
浏览器访问:http://127.0.0.1:8848/nacos/index.html#/login,默认的账号密码为nacos/nacos,控制台页面如下:
至此Nacos-server启动成功。
创建服务提供者
创建聚合工程Nacos作为父工程,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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.kongcheng</groupId>
<artifactId>nacos</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos</name>
<description>nacos</description>
<modules>
<module>nacos-provide</module>
<module>nacos-consumer</module>
</modules>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.0.4.RELEASE</spring-boot.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
<nacos.version>0.2.2.RELEASE</nacos.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${nacos.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
然后点击右键创建子模块Module,命名为【nacos-provide】,其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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>nacos</artifactId>
<groupId>com.kongcheng</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.kongcheng</groupId>
<artifactId>nacos-provide</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-provide</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<version>2.3.0.RELEASE</version>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在主入口【NacosProvideApplication】中加入@EnableDiscoveryClient注解, 开启服务注册发现功能:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient//开启服务注册发现
public class NacosProvideApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProvideApplication.class, args);
}
}
然后创建【TestController】作为测试控制层:
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/helloNacos")
public String helloNacos(){
return "你好,nacos!";
}
}
最后配置文件application.yml进行如下配置
server:
port: 18200
spring:
application:
name: nacos-provide
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos作为服务注册中心地址
创建服务消费者
点击右键创建子模块Module,命名为【nacos-consumer】,其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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>nacos</artifactId>
<groupId>com.kongcheng</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.kongcheng</groupId>
<artifactId>nacos-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-consumer</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<version>2.3.0.RELEASE</version>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
主入口同样添加@EnableDiscoveryClient注解, 开启服务注册发现功能,然后再创建【TestController】作为测试控制层:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class TestController {
@Autowired
private RestTemplate restTemplate;
/**
* 注解开启ribbon负载均衡能力
* @return
*/
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
/**
* RestTemplate+Ribbon进行服务调用
* @return
*/
@GetMapping("/consumer")
public String test1(){
return restTemplate.getForObject("http://nacos-provide/helloNacos",String.class);
}
}
最后配置文件application.yml进行如下配置
server:
port: 18201
spring:
application:
name: nacos-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848 # Nacos作为服务注册中心地址
调用测试
启动完成后,在日志中应该可以看到如下两条信息
2022-02-17 18:17:47.080 INFO 16440 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, nacos-provide 192.168.1.112:18200 register finished
2022-02-17 18:18:14.795 INFO 17756 --- [ main] o.s.c.a.n.registry.NacosServiceRegistry : nacos registry, nacos-consumer 192.168.1.112:18201 register finished
现在登录Nacos控制台,你会发现服务列表中,已经显示了我们刚才创建的两个项目,并可以对其进行简单的监控和管理。
浏览器中访问服务消费者的接口【localhost:18201/consumer】可以看到成功返回结果
至此测试完成。