在springloud中,使用过的同学想必对eureka不陌生,eureka是springcloud的注册中心,提供服务的注册于发现功能,与此类似,nacos的注册中心封装并集成了类似eureka注册中心的功能,从上一篇nacos的注册中心的界面可以看出来,只不过nacos的注册中心看起来更容易也更加方便集中式管理
模拟一个微服务的场景,有两个微服务,一个作为生产者,另一个为消费者,生产者和消费者均向nacos注册服务,同时消费者可以调用生产者提供的接口服务
3.1 pom依赖
nacos的相关组件更新很快,我们知道springboot和springloud的版本都有一定的版本要求,这里大家可以结合springboot的官网和nacos托管在github上的说明对比一下,选择适合自己的业务的版本,我下面的演示demo是比较新的版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<fastjson.version>1.2.47</fastjson.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>0.9.0.RELEASE</spring-cloud-alibaba.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>
<!--阿里巴巴fastjosn依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--nacos服务发现组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
3.2 配置文件
配置文件比较简单,只需要提供一个服务名称,并且制定nacos的注册中心的地址即可
spring:
application:
name: service-provider
cloud:
nacos:
register-enabled: true
discovery:
server-addr: IP:8848
server:
port: 8081
3.2 测试服务接口
@RestController
public class ProviderController {
private static Logger logger = LoggerFactory.getLogger(ProviderController.class);
//http://localhost:8001/getMsgInfo/hello
@GetMapping("/getMsgInfo/{msg}")
public String getMsgInfo(@PathVariable String msg){
logger.info("生产者服务被调用了....");
return "Hello Nacos :" + msg;
}
}
3.3 启动类
@SpringBootApplication
@EnableDiscoveryClient
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class,args);
}
}
生产端的配置比较简单,然后运行这段程序,去nacos注册中心的控制台上检查一下,可以看到一个服务已经注册上去了
4.1 pom依赖
消费端的配置和生产端的基本类似,多加了一个open-feign的依赖,nacos提供了两种微服务调用的方式,通过http的方式以及feign的方式,这个和原生的springcloud的使用形式类似
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<fastjson.version>1.2.47</fastjson.version>
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
<spring-cloud-alibaba.version>0.9.0.RELEASE</spring-cloud-alibaba.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>
<!--阿里巴巴fastjosn依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<!--nacos服务发现组件-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>0.9.0.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.1.3.RELEASE</version>
</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>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
4.2 配置文件
spring:
application:
name: service-consumer
cloud:
nacos:
register-enabled: true
discovery:
server-addr: IP:8848
server:
port: 8082
4.3 测试controller
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/getMsgInfoFromRemote/{msg}")
public String getMsgInfoFromRemote(@PathVariable String msg){
return restTemplate.getForObject("http://service-provider/getMsgInfo/" + msg, String.class);
}
}
4.5 启动类
我们先使用第一种方式,直接使用restTemplate的形式调用
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApp {
public static void main(String[] args) {
SpringApplication.run(ConsumerApp.class,args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
启动程序,然后调用一下接口,http://localhost:8082/getMsgInfoFromRemote/rest-msg,可以看到消费端也成功注册到了nacos的注册中心上去了
下面我们再使用feign的方式调用一下,使用feign的方式进行调用也很简单,只需要在consumer端加一个接口即可,该接口专门负责对接具体的服务提供者,如下,
@FeignClient(value = "service-provider")
public interface ConsumerService {
@GetMapping("/getMsgInfo/{msg}")
String getMsgInfo(@PathVariable("msg") String msg);
}
注意,接口上必须加上生产者的服务名称
然后将该接口注入到需要使用的地方即可
@RestController
public class ConsumerController {
@Autowired
private RestTemplate restTemplate;
@Autowired
private ConsumerService consumerService;
@GetMapping("/getMsgInfoFromRemote/{msg}")
public String getMsgInfoFromRemote(@PathVariable String msg){
return restTemplate.getForObject("http://service-provider/getMsgInfo/" + msg, String.class);
}
//http://localhost:8082/getMsgFeignFromRemote/
@GetMapping("/getMsgFeignFromRemote/{msg}")
public String getMsgFeignFromRemote(@PathVariable String msg){
return consumerService.getMsgInfo(msg);
}
}
然后我们通过feign的方式调用一下下面的接口,http://localhost:8082/getMsgFeignFromRemote/feign-msg
通过上述内容,我们演示了一下如何使用nacos做服务的注册于发现,更深入的内容大家可以继续研究,希望对看到的同学有用,最后,感谢观看!