赞
踩
准备工作 一个查看 一个添加
添加如下
@RestController @RequestMapping("/order") public class OrderController1 { @Autowired private OrderService orderService; //该类没有交予spring管理 @Autowired private RestTemplate restTemplate; @GetMapping("/buy/{pid}/{num}") public String getById(@PathVariable Integer pid,@PathVariable Integer num){ System.out.println("=============================购买开始================================="); Order order = new Order(); order.setNumber(num); // 用户信息可以根据token从redis中获取 order.setUid(1); order.setUsername("超哥"); //需要设置订单中的商品信息 //远程调用商品微服务 http协议restFul风格调用 适合微服务 TPC协议 RPC风格适合 SOA分布式 Product product = restTemplate.getForObject("http://localhost:8081/product/getById/" + pid, Product.class); System.out.println("=================远程查询的商品结构=============="+product); order.setPid(product.getPid()); order.setPname(product.getPname()); order.setPprice(product.getPprice()); orderService.saveOrder(order); return "下单成功"; }
1.下载nacos-serve在当前目录下搜索cmd就可以直接打开当前目录下的黑窗口输入startup.cmd -m standalone可以启动单例模式 默认为集群模式
网页搜索路径为localhost:8848/nacos
账号密码默认为nacos
点击服务管理-在服务列表查询信息
引入nacos依赖
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
application.properties
nacos注册中心 ip地址
spring.cloud.nacos.discovery.server-addr=localhost:8848
指定是否把该服务注册到注册中心 默认为true
spring.cloud.nacos.discovery.register-enabled=false
nacos注册名称---不能存在下划线 '_' spring.application.name=springcloud-product
运行即可在注册中心 查看信息
2.
@Autowired private RestTemplate restTemplate;
//根据名称获取对象实例 List<ServiceInstance> instances = discoveryClient.getInstances("springcloud-product"); //获取第一个实例名称 int v = (int) (Math.random() * instances.size()); System.out.println(v); ServiceInstance serviceInstance = instances.get(v); serviceInstance.getHost();//主机ip serviceInstance.getPort();//主机端口号 String path = serviceInstance.getUri().toString();//端口加id Product product = restTemplate.getForObject(path+"/product/getById/" + pid, Product.class);
3.再开一个启动类
测试会随机在两个端口号中出查询结果---人为手写负载均衡
ribbon实现负载均衡 无需在家依赖 上个依赖中已经有了
只需在著启动类中加
//ribbon借助负载均衡的调用 @LoadBalanced
@SpringBootApplication @EnableFeignClients public class OrderApp { public static void main(String[] args) { SpringApplication.run(OrderApp.class,args); } @Bean //ribbon借助负载均衡的调用 @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
@RestController @RequestMapping("/order") public class OrderController3 { @Autowired private OrderService orderService; //该类没有交予spring管理 @Autowired private RestTemplate restTemplate; @GetMapping("/buy/{pid}/{num}") public String getById(@PathVariable Integer pid,@PathVariable Integer num){ System.out.println("=============================购买开始================================="); Order order = new Order(); order.setNumber(num); // 用户信息可以根据token从redis中获取 order.setUid(1); order.setUsername("超哥"); //restTemplate调用时必须使用 /服务提供者的名称/服务提供的资源路径 Product product = restTemplate.getForObject("http://springcloud-product/product/getById/" + pid, Product.class); System.out.println("=================远程查询的商品结构=============="+product); order.setPid(product.getPid()); order.setPname(product.getPname()); order.setPprice(product.getPprice()); orderService.saveOrder(order); return "下单成功"; } }
3.application.properties中加
springcloud-product微服务名称 NFLoadBalancerRuleClassName微服务类
springcloud-product.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RandomRule
会随机访问结果
4.加openfign依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
建一个接口
//被调用的微微服务名称springcloud-product 代理类url= http://springcloud-product/product/getById/{pid} @FeignClient(value = "springcloud-product") public interface PeoductFeign { //微服务接口方法 @GetMapping("/product/getById/{pid}") public Product getById(@PathVariable Integer pid); }
默认不识别
需要在启动类中加
@EnableFeignClients开启注解
然后去调用
@Autowired private PeoductFeign peoductFeign;
拿到product
Product product = peoductFeign.getById(pid);
5.eureka-server配置类
1.加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
2.application.properties中加
server.port=7000 spring.application.name=eureka-server 主机名 eureka.instance.hostname=localhost #是否把eureka服务器注册到注册中心 eureka.client.register-with-eureka=false #是否拉取 eureka.client.fetch-registry=false #服务器地址 eureka.client.service-url.defaultZone=localhost:7000/eureka
建启动类
@SpringBootApplication @EnableEurekaServer//开启eureka注解 public class EurekaServiceApp { public static void main(String[] args) { SpringApplication.run(EurekaServiceApp.class,args); } }、
测试
加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
配置类修改访问地址
eureka.client.service-url.defaultZone=localhost:7000/eureka #??????? spring.application.name=eureka-product
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。