然后新建一个Module,命名为nacos-provider-demo,Java代码的包名为ltd.newbee.cloud。在该Module的pom.xml配置文件中增加parent标签,与上层Maven建立好关系。之后,在这个子模块的 pom.xml 文件中加入Nacos的依赖项 spring-cloud-starter-alibaba-nacos-discovery。最终,子节点nacos-provider-demo的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>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>nacos-provider-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-provider-demo</name>
<description>Spring Cloud Alibaba Provider Demo</description>
<parent>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
首先把该Spring Boot项目的端口号设置为8091。之后创建ltd.newbee.cloud.api包,在api包中新建NewBeeCloudGoodsAPI类,代码如下:
package ltd.newbee.cloud.api;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class NewBeeCloudGoodsAPI {
@Value("${server.port}")
private String applicationServerPort;// 读取当前应用的启动端口
@GetMapping("/goodsServiceTest")
public String goodsServiceTest() {
// 返回信息给调用端
return "this is goodsService from port:" + applicationServerPort;
}
}
启动类命名为ProviderApplication,代码如下:
package ltd.newbee.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
配置文件中添加Nacos配置参数
完成基础的服务编码后,接下来就要把这个服务注册到Nacos中去。过程也非常简单,只需要在application.properties文件中添加几个Nacos的配置项即可。
# 项目启动端口
server.port=8091
# 应用名称
spring.application.name=newbee-cloud-goods-service
# 注册中心Nacos的访问地址
spring.cloud.nacos.discovery.server-addr=localhost:8848
# 登录名(默认username,可自行修改)
spring.cloud.nacos.username=nacos
# 密码(默认password,可自行修改)
spring.cloud.nacos.password=nacos
编写服务消费端的代码
新建一个Module,命名为nacos-consumer-demo,Java代码的包名为ltd.newbee.cloud。在该Module的pom.xml配置文件中增加parent标签,与上层Maven建立好关系。之后,在这个子模块的 pom.xml 文件中加入Nacos的依赖项 spring-cloud-starter-alibaba-nacos-discovery。最终,子节点nacos-consumer-demo的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>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>nacos-consumer-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-consumer-demo</name>
<description>Spring Cloud Alibaba Nacos Consumer Demo</description>
<parent>
<groupId>ltd.newbee.cloud</groupId>
<artifactId>spring-cloud-alibaba-nacos-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project>
接着在nacos-consumer-demo中进行简单的功能编码,首先把该Spring Boot项目的端口号设置为8093。之后创建ltd.newbee.cloud.api包,在api包中新建ConsumerTestController类,代码如下:
package ltd.newbee.cloud.api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class ConsumerTestController {
// 测试方法,暂未通过Nacos调用下级服务
@GetMapping("/nacosRegTest")
public String nacosRegTest() {
return "nacosRegTest";
}
}
启动类命名为ConsumerApplication,代码如下:
package ltd.newbee.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
将服务注册至Nacos
完成基础的编码后,接下来就要把nacos-consumer-demo注册到Nacos中去。在application.properties配置文件添加Nacos配置项即可,代码如下所示
# 项目启动端口
server.port=8093
# 应用名称
spring.application.name=newbee-cloud-consumer-service
# 服务中心Nacos地址
spring.cloud.nacos.discovery.server-addr=47.114.141.185:17748
# 登录名(默认为nacos,可自行修改)
spring.cloud.nacos.username=nacos
# 登录密码(默认为nacos,可自行修改)
spring.cloud.nacos.password=nacos
注册至Nacos成功后,接下来就要编写服务通信相关的代码了。
编写服务通信代码
借助RestTemplate工具实现Nacos间的服务通信。
在newbee-cloud-consumer-service项目中新建config包,并新建RestTemplate的配置类,代码如下:
package ltd.newbee.cloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.Charset;
@Configuration
public class RestTemplateConfig {
@LoadBalanced //负载均衡
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
RestTemplate restTemplate = new RestTemplate(factory);
// UTF-8编码设置
restTemplate.getMessageConverters().set(1,
new StringHttpMessageConverter(Charset.forName("UTF-8")));
return restTemplate;
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
// 超时时间 10秒
factory.setReadTimeout(10 * 1000);
// 超时时间 5秒
factory.setConnectTimeout(5 * 1000);
return factory;
}
}
不仅仅需要添加@LoadBalanced注解,消费端在发起调用时需要添加负载均衡模块,所以需要在nacos-consumer-demo中引入如下依赖:
<!-- 负载均衡模块 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
ConsumerTestController类中新增如下代码:
private final String SERVICE_URL = "http://newbee-cloud-goods-service";
// 通过Nacos调用下级服务
@GetMapping("/consumerTest")
public String consumerTest() {
return restTemplate.getForObject(SERVICE_URL + "/goodsServiceTest", String.class);
}
上述代码中,SERVICE_URL变量的写法变成了“http://”+服务名称,而不是“http://”+IP+端口号。不过,由于服务中心的存在,这样也能够完成对newbee-cloud-goods-service的调用。启动newbee-cloud-consumer-service项目,打开浏览器并输入如下地址:
http://localhost:8093/consumerTest