nacos服务消费者
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nacos服务消费者相关的知识,希望对你有一定的参考价值。
参考技术A 消费者跟提供者配置方式基本差不太多, 使用的时候提provider提供数据,而consumer调用provider,调用的方式有很多中,常用的有三种方式:然在Nacos工程下创建一个子工程命名其pom文件与nacos-provider相同
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n14" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"><?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>
<groupId>com.zw</groupId>
<artifactId>springcloud-alibaba-example</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>nacos-server-consumer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>nacos-server-consumer</name>
<description>消费者</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
</dependencies>
</project></pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="yaml" cid="n16" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">spring:
cloud:
nacos:
discovery:
cluster-name: nacos-server-consumer
server-addr: 119.23.190.71:8848</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication
public static void main(String[] args)
SpringApplication.run(NacosConsumerApplication.class, args);
</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n21" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Configuration
public class RestTemplateConfiguration
@Bean
//RestTemplate在请求时拥有客户端负载均衡的能力
@LoadBalanced
public RestTemplate restTemplate()
return new RestTemplate();
</pre>
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="java" cid="n23" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit; color: rgb(184, 191, 198); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">@Service
public class NacosConsumerServiceImpl implements NacosConsumerService
@Resource
private RestTemplate restTemplate;
@Override
public String getData()
return restTemplate.getForObject(" http://nacos-server-provider/ ", String.class);
</pre>
登录Nacos控制台,在服务列表选项卡中看到我们的两个应用,并可以对其进行简单的监控和管理。
浏览器中访问服务消费者的接口 http://127.0.0.1:9528/ 可以看到成功返回结果
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服务消费者的主要内容,如果未能解决你的问题,请参考以下文章