一、前言
参考该篇文章《Nacos作为注册中心(一) 使用Spring Cloud开发消费者对提供者的调用》发现,接口文件是在消费者的工程里,这样项目结构不清晰也不规范,不利于后期的迭代与维护。
下面把接口定义提出来,单独作为一个模块。
二、项目结构
项目的结构如下,有3个模块:
- nacos-api-interface 接口的定义,
- nacos-api-provide 接口的实现,服务的提供者
- nacos-api-consumer 服务的消费者,调用远程服务器上的nacos-api-provide
三、创建IDEA项目
3.1 创建Maven父工程
然后把src目录删除掉,这是父项目,没有代码。
3.2 模块nacos-api-interface
3.2.1 创建Module
可以删除掉该模块的resources和test目录
3.2.2 修改pom.xml
# 增加openfeign的库
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
</dependencies>
3.2.3 创建IEchoService.java
package com.anron.common.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @Author: Anron
* @Date: 2020/8/6 17:12
*/
@FeignClient(name = "service-provider")
@RequestMapping(value = "/api")
public interface IEchoService {
@RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
String echo(@PathVariable("str") String str);
}
3.3 模块nacos-api-provider
3.3.1 创建工程
3.3.2 修改ProviderApplication.java
package com.anron.provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {
public static void main(String[] args) {
SpringApplication.run(ProviderApplication.class, args);
}
}
3.3.3 创建EchoController.java
package com.anron.provider.controller;
import com.anron.common.service.IEchoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Anron
* @Date: 2020/8/6 17:16
*/
@RestController
public class EchoController implements IEchoService {
@Autowired
private Environment environment;
Logger log = LoggerFactory.getLogger(this.getClass());
@Override
public String echo(@PathVariable("str") String string) {
String port = environment.getProperty("server.port");
log.info("port=" + port + ", param="+ string);
return string;
}
}
3.3.4 修改application.properties
spring.application.name=service-provider
server.port=18081
# Nacos Server的安装地址和端口号
spring.cloud.nacos.discovery.server-addr=192.168.1.17:8848
3.3.5 修改父项目的pom.xml
# 在<modules></modules>中增加
<module>nacos-api-provider</module>
3.4 模块nacos-api-consumer
3.4.1 创建工程
3.4.2 修改ConsumerApplication.java
package com.anron.consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.anron.*"})
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
注意:@EnableFeignClients默认是扫描当前package下的@FeignClient注解,如果需要扫描外部JAR包里的@FeignClient注解,需要更改basePackages的值,如果这里的配置不正确,程序运行时会提示找不到interface实例后的Bean,提示信息如下
Field echoService in com.anron.consumer.controller.TestController required a bean of type 'IEchoService' that could not be found.
3.4.3 创建TestController.java
package com.anron.consumer.controller;
import com.anron.common.service.IEchoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author: Anron
* @Date: 2020/8/6 17:38
*/
@RestController
public class TestController {
@Autowired
private IEchoService echoService;
@RequestMapping(value = "/echo-feign/{str}", method = RequestMethod.GET)
public String feign(@PathVariable String str) {
return echoService.echo(str);
}
}
3.4.4 修改application.properties
spring.application.name=service-consumer
server.port=18082
spring.cloud.nacos.discovery.server-addr=192.168.1.17:8848
3.4.5 修改父项目的pom.xml
# 在<modules></modules>中增加
<module>nacos-api-consumer</module>
四、测试
先确保Nacos server已启动,然后开启2个服务提供者,分别运行于18001和18002接口
java -jar nacos-api-provider-0.0.1-SNAPSHOT.jar --server.port=18001
java -jar nacos-api-provider-0.0.1-SNAPSHOT.jar --server.port=18002
调用provider中的restful接口进行测试
curl http://127.0.0.1:18001/api/echo/1
curl http://127.0.0.1:18002/api/echo/1
开启1个服务消费者,运行于18082接口
java -jar nacos-api-consumer-0.0.1-SNAPSHOT.jar
调用consumer的接口进行多次测试
curl http://127.0.0.1:18082/echo-feign/1
curl http://127.0.0.1:18082/echo-feign/2
curl http://127.0.0.1:18082/echo-feign/3
curl http://127.0.0.1:18082/echo-feign/4