Nacos注册中心和服务消费方式

news/2025/2/12 17:55:49/

1.安装配置nacos以及实现服务支持

nacos简介

Nacos 致力于帮助您发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速
实现动态服务发现、服务配置、服务元数据及流量管理。
从上面的介绍就可以看出,nacos的作用就是一个注册中心,用来管理注册上来的各个微服务。
nacos2.0.1版本: https://pan.baidu.com/s/1hXH5Eq3ZR1T1qZ0YOqD5Cg
提取码:8kb0

1.1:切换单体:
startup.cmd -m standalone

或者直接修改startup.cmd文件:set MODE="standalone"

1.2:访问nacos:
打开浏览器输入http://localhost:8848/nacos,即可访问服务, 默认密码是nacos/nacos
1.3:模块依赖:

<!--nacos客户端-->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

1.4:在模块启动类上添加注解:@EnableDiscoveryClient

package com.xujie.shop_product;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class ShopProductApplication {public static void main(String[] args) {SpringApplication.run(ShopProductApplication.class, args);}}

启动后查看nacos:
注:nacos中的服务名不能出现"_",这是nacos的格则(我出过这个错0.0)
在这里插入图片描述

实现服务调用的负载均衡

Ribbon实现负载均衡

package com.xujie.shop_order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableDiscoveryClient
@SpringBootApplication
public class ShopOrderApplication {public static void main(String[] args) {SpringApplication.run(ShopOrderApplication.class, args);}@Bean@LoadBalanced  //使用ribbon方式实现负载均衡public RestTemplate restTemplate(){return new RestTemplate();}
}
 log.info("客户下单,这时候要调用商品微服务查询商品信息");//定义微服务的服务名称,对应的是商品服务的双节点(8080和8081)String serverName="shop-product";log.info("远程调用地址为:{}","http://"+serverName+"/product/getOne/"+pid);Product product =restTemplate.getForObject("http://"+serverName+"/product/getOne/"+pid,Product.class);

在这里插入图片描述

DiscoveryClient实现负载均衡

package com.xujie.shop_order.controller;import com.alibaba.fastjson.JSON;
import com.xujie.common.model.Order;
import com.xujie.common.model.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;
import java.util.Random;/*** @author 许缘* @company xxx公司* @create 2022-12-06  17:25*/
@Slf4j
@RestController
@RequestMapping("/order")
public class ordercontroller {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;@RequestMapping("/test/{pid}")public Order createOrder(@PathVariable("pid") Integer pid){log.info("客户下单,这时候要调用商品微服务查询商品信息");//定义微服务的服务名称,对应的是商品服务的双节点(8080和8081)String serverName="shop-product";//获取当前微服务所对应的实例数List<ServiceInstance> instances = discoveryClient.getInstances(serverName);//使用随机挑选的方式获取服务实例int index = new Random().nextInt(instances.size());//根据挑选的服务下标获取具体的服务实例(ip+端口)ServiceInstance instance = instances.get(index);//拼接生产者的服务的地址String url="http://"+instance.getHost()+":"+instance.getPort();log.info("远程调用地址为:{}",url+"/product/getOne/"+pid);Product product =restTemplate.getForObject(url+"/product/getOne/"+pid,Product.class);log.info("查询结果:"+ JSON.toJSONString(product));//创建订单Order order = new Order();order.setOid(System.currentTimeMillis());order.setUid(12);order.setUsername("张三");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(product.getStock());return order;}
}

在这里插入图片描述

基于Feign实现服务调用

什么是Feign

Feign是Spring Cloud提供的一个声明式的伪Http客户端, 它使得调用远程服务就像调用本地服务
一样简单, 只需要创建一个接口并添加一个注解即可。
Nacos很好的兼容了Feign, Feign默认集成了 Ribbon, 所以在Nacos下使用Fegin默认就实现了负载均衡的效果。

Feign的使用

pom.xml依赖:

<!--fegin组件-->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

添加注解:@EnableFeignClients启用远程调用

package com.xujie.shop_order;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@EnableFeignClients //启用远程调用
@EnableDiscoveryClient
@SpringBootApplication
public class ShopOrderApplication {public static void main(String[] args) {SpringApplication.run(ShopOrderApplication.class, args);}@Bean//@LoadBalanced  //使用ribbon方式实现负载均衡public RestTemplate restTemplate(){return new RestTemplate();}
}

接口:

package com.xujie.shop_order.service;import com.xujie.common.model.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;/*** @author 许缘* @company xxx公司* @create 2022-12-07  14:39* @FeignClient:指定微服务的服务名称* shop-product === http://193.168.1.144:8081/*/
@FeignClient("shop-product") //调用是哪个服务
public interface IProductService {/***  http://193.168.1.144:8081/product/getOne/{pid}* @param pid* @return*/@RequestMapping("/product/getOne/{pid}")Product getOne(@PathVariable("pid") Integer pid);}
package com.xujie.shop_order.controller;import com.alibaba.fastjson.JSON;
import com.xujie.common.model.Order;
import com.xujie.common.model.Product;
import com.xujie.shop_order.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import java.util.List;
import java.util.Random;/*** @author 许缘* @company xxx公司* @create 2022-12-06  17:25*/
@Slf4j
@RestController
@RequestMapping("/order")
public class ordercontroller {@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate DiscoveryClient discoveryClient;/*** 相当于本地调用*/@Autowiredprivate IProductService productService;@RequestMapping("/test/{pid}")public Order createOrder(@PathVariable("pid") Integer pid){log.info("客户下单,这时候要调用商品微服务查询商品信息");Product product = productService.getOne(pid);log.info("查询结果:"+ JSON.toJSONString(product));//创建订单Order order = new Order();order.setOid(System.currentTimeMillis());order.setUid(12);order.setUsername("张三");order.setPid(product.getPid());order.setPname(product.getPname());order.setPprice(product.getPprice());order.setNumber(product.getStock());return order;}
}

Feign参数传递

package com.xujie.shop_product.controller;import com.xujie.common.model.Product;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;@Slf4j
@RestController
@RequestMapping("/feign")
public class FeignDemoController {@RequestMapping("/findByParameter")public String findByParameter(String name,Double price){log.info("服务提供者日志:{}",name);return "hello:"+name;}@RequestMapping("/findByParameter2")public String findByParameter2(@RequestParam("name") String name,@RequestParam("price") Double price){log.info("服务提供者日志:{},{}",name,price);return "hello:"+name+price;}@RequestMapping("/findByPathVariable")public String findByPathVariable(@PathVariable String name){log.info("服务提供者日志:{}",name);return "hello:"+name;}@RequestMapping("/findByRequestBody")public Product findByRequestBody(@RequestBody Product product){log.info("服务提供者日志:{}",product.getPname());return product;}
}

消费者:

package com.xujie.shop_order.service;import com.xujie.common.model.Product;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;/*** @author 许缘* @company xxx公司* @create 2022-12-07  15:14* 在这边必须按照生产者的传参要求传参* 调用生产者的方法时必须要加注解*/
@FeignClient("shop-product")
public interface IFeignService {@RequestMapping("/feign/findByParameter")String findByParameter(@RequestParam("name") String name,@RequestParam("price") Double price);@RequestMapping("/feign/findByParameter2")String findByParameter2(@RequestParam("name") String name,@RequestParam("price") Double price);@RequestMapping("/feign/findByPathVariable/{name}")String findByPathVariable(@PathVariable("name") String name);@RequestMapping("/feign/findByRequestBody")Product findByRequestBody(@RequestBody Product product);}

controller可以不按照生产者的要求传参:

package com.xujie.shop_order.controller;import com.alibaba.fastjson.JSON;
import com.xujie.common.model.Order;
import com.xujie.common.model.Product;
import com.xujie.shop_order.service.IFeignService;
import com.xujie.shop_order.service.IProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;import java.util.List;
import java.util.Random;/*** @author 许缘* @company xxx公司* @create 2022-12-06  17:25*/
@Slf4j
@RestController
@RequestMapping("/order")
public class ordercontroller {@Autowiredprivate IFeignService feignService;@RequestMapping("/findByParameter")public String findByParameter(String name,Double price){return feignService.findByParameter(name,price);}@RequestMapping("/findByParameter2")public String findByParameter2(String name, Double price){return feignService.findByParameter2(name, price);}@RequestMapping("/findByPathVariable")public String findByPathVariable(String name){return feignService.findByPathVariable(name);}@RequestMapping("/findByRequestBody")public Product findByRequestBody(Product product){return feignService.findByRequestBody(product);}
}

http://www.ppmy.cn/news/2364.html

相关文章

【Linux学习】进程概念(下)

&#x1f431;作者&#xff1a;一只大喵咪1201 &#x1f431;专栏&#xff1a;《Linux学习》 &#x1f525;格言&#xff1a;你只管努力&#xff0c;剩下的交给时间&#xff01; 进程概念&#xff08;下&#xff09;&#x1f35f;进程优先级&#x1f35f;进程切换&#x1f35f…

【图】认识与表达

文章目录一、图的基本构成二、图的表达方式1&#xff09;邻接矩阵2&#xff09;邻接表3&#xff09;数组4&#xff09;综合一、图的基本构成 地图上有很多的建筑&#xff0c;每个建筑之间有着四通八达的道路连接着&#xff0c;如果想要使用数据结构来表示建筑和建筑之间的道路…

java 如何运行SpringBoot jar包中的指定main函数

测试类包含了主函数&#xff0c;被一起打到了jar包中。但是如果执行&#xff1a; java -jar test.jar 那么会执行web服务的主函数。 我们如何指定执行测试类中的主函数呢&#xff1f; 一开始是想到用&#xff1a; java -cp test.jar com.my.TestClass 但是提示无法找到主函数…

阿里十年技术沉淀|深度解析百PB级数据总线技术

云原生场景下数据总线需求场景及挑战 数据总线简介 数据总线作为大数据架构下的流量中枢&#xff0c;在不同的大数据组件之间承载着数据桥梁的作用。通过数据总线&#xff0c;可以实时接入来自服务器、K8s、APP、Web、IoT/移动端等产生的各类异构数据&#xff0c;进行统一数据…

(七)Vue之事件处理

文章目录事件的基本使用事件修饰符按键修饰符preventstoponcecaptureselfpassive按键修饰符系统修饰键自定义别名exact 修饰符鼠标按钮修饰符Vue学习目录 上一篇&#xff1a;&#xff08;六&#xff09;Vue之数据代理 事件的基本使用 事件的基本使用&#xff1a; 绑定&…

手机软件测试用例设计

实例讲解手机软件测试用例设计 实例讲解手机软件测试用例设计,测试伴随在整个手机软件开发的各个阶段中&#xff0c;测试质量的高低直接关系到手机软件的可用性&#xff0c;友好性&#xff0c;可靠性。可以说&#xff0c;测试环节是手机软件开发的重要环节&#xff0c;是整个开…

wait与notify的使用

专栏链接:多线程相关知识详解 wait是Object类里面的方法,而Object类是所有类的父类,所以所有的类都可以使用wait方法 wait里面包含着3个操作: ①释放当前锁 ②进入阻塞等待 ③其他线程调用notify的时候,可以将其唤醒并尝试重新获取锁 public class Demo2 {public static void …

企业为什么要做知识管理?如何进行知识管理?

今天将和大家聊一聊如何通过5大步骤&#xff0c;帮助企业进行知识管理与知识沉淀。 近年来&#xff0c;随着建设的深入&#xff0c;IT不仅成为企业运营的基础&#xff0c;而且在ERP、CRM、OA等信息系统内沉淀的大量知识成为了企业创新的知识源泉&#xff0c;于是知识管理逐渐提…