nacos微服务生产者,消费者,远程调用
Posted 苏格拉的底
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nacos微服务生产者,消费者,远程调用相关的知识,希望对你有一定的参考价值。
nacos微服务生产者,消费者,远程调用
微服务生产者
运行NacosProviderApp后,可以在nacos的服务列表查看到生产的服务名
ProviderController、
package com.itheima.nacos.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator
* @version 1.0
**/
@RestController
public class ProviderController
private static final Logger LOG = LoggerFactory.getLogger(ProviderController.class);
@GetMapping("/service")
public String service()
LOG.info("provider invoke");
return "provider invoke";
NacosProviderApp、
package com.itheima.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author Administrator
* @version 1.0
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosProviderApp
public static void main(String[] args)
SpringApplication.run(NacosProviderApp.class,args);
System.out.println("NacosProviderApp启动成功");
application.yml、
server:
port: $port:56010 #启动端口
spring:
application:
name: quickstart-provider
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
logging:
level:
root: info
org.springframework: info
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>nacos-discovery</artifactId>
<groupId>com.itheima.nacos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>quickstart-provider</artifactId>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
</project>
服务消费者
ProviderClient、
package com.itheima.nacos.client;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
/**
* Created by Administrator.
*/
@FeignClient(value = "quickstart-provider")//调用哪个服务
public interface ProviderClient
@GetMapping("/service")//调用哪个服务方法
public String service();
ConsumerController\\
package com.itheima.nacos.controller;
import com.itheima.nacos.client.ProviderClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Administrator
* @version 1.0
**/
@RestController
public class ConsumerController
//动态代理对象,内部远程调用服务生产者
@Autowired
ProviderClient providerClient;
@GetMapping("/service")
public String service()
//远程调用
String providerResult = providerClient.service();
// 返回生产者的串
return "consumer invoke" + "|" + providerResult;
NacosConsumerApp\\
package com.itheima.nacos;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author Administrator
* @version 1.0
**/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class NacosConsumerApp
public static void main(String[] args)
SpringApplication.run(NacosConsumerApp.class,args);
System.out.println("NacosConsumerApp启动成功");
application.yml、
server:
port: 56020 #启动端口 命令行注入
spring:
application:
name: quickstart-consumer
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
# namespace: c67e4a97-a698-4d6d-9bb1-cfac5f5b51c4
# cluster-name: DEFAULT
消费方也是暴露一个服务,也会在服务列表生产一个服务名
服务远程调用
http://localhost:56020/service
成功调用服务生产者的地址
网关配置
新建一个nacos
api-gateway.yaml、NACOS_MICROSERVICE_GROUP
zuul:
routes:
application1:
stringPrefix: false
path:/application/**
项目地址demo:https://gitee.com/zouxiongnb/nacos-discovery
以上是关于nacos微服务生产者,消费者,远程调用的主要内容,如果未能解决你的问题,请参考以下文章