实现一个场景:

订单微服务:

POM:

<?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> <groupId>com.nb.security</groupId>
<artifactId>nb-order-api</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency> </dependencies> </dependencyManagement> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> </dependencies> <build>
<plugins> <!--指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin> <plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

yml 指定端口:

server:
port: 9060
订单信息 OrderInfo.java:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderInfo { private Long productId; }

价格信息 PriceInfo :

@Data
public class PriceInfo { private Long id; private BigDecimal price;
}

OrderControl:

package com.nb.security.order;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate; @Slf4j
@RestController
@RequestMapping("/orders")
public class OrderController { private RestTemplate restTemplate = new RestTemplate(); @PostMapping
public OrderInfo create(@RequestBody OrderInfo info){
//查询价格
PriceInfo price = restTemplate.getForObject("http://localhost:9080/prices/"+info.getProductId(),PriceInfo.class);
log.info("price is "+price.getPrice());
return info;
} @GetMapping
public OrderInfo getInfo(@PathVariable Long id){
log.info("getInfo: id is "+id);
return new OrderInfo(id);
} }
价格微服务:

POM:

<?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> <groupId>com.nb.security</groupId>
<artifactId>nb-price-api</artifactId>
<version>0.0.1-SNAPSHOT</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency> </dependencies> <build>
<plugins> <!--指定JDK编译版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

yml指定端口

server:
port: 9080
价格信息实体info类:
@Data
public class PriceInfo { private Long productId; private BigDecimal price;
}

价格微服务Controller:

/**
* 价格服务
*/
@Slf4j
@RestController
@RequestMapping("/prices")
public class PriceController { @GetMapping("/{productId}")
public PriceInfo getPrice(@PathVariable Long productId){
log.info("product id is "+productId);
PriceInfo info = new PriceInfo();
info.setProductId(productId);
info.setPrice(new BigDecimal(100));
return info;
}
}

启动两个服务,用postman请求下订单服务:

查看控制台日志,可以看到两个服务都已调通。

 

												

Spring Cloud微服务安全实战_4-3_订单微服务&价格微服务的更多相关文章

  1. Spring Cloud Sleuth超详细实战

    为什么需要Spring Cloud Sleuth 微服务架构是一个分布式架构,它按业务划分服务单元,一个分布式系统往往有很多个服务单元.由于服务单元数量众多,业务的复杂性,如果出现了错误和异常,很难去 ...

  2. 新书上线:《Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统》,欢迎大家买回去垫椅子垫桌脚

    新书上线 大家好,笔者的新书<Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统>已上线,此书内容充实.材质优良,乃家中必备垫桌脚 ...

  3. Spring Cloud Gateway+Nacos,yml+properties两种配置文件方式搭建网关服务

    写在前面 网关的作用不在此赘述,举个最常用的例子,我们搭建了微服务,前端调用各服务接口时,由于各服务接口不一样,如果让前端同事分别调用,前端同事会疯的.而网关就可以解决这个问题,网关屏蔽了各业务服务的 ...

  4. Spring Cloud Gateway限流实战

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  5. Spring Cloud Gateway自定义过滤器实战(观测断路器状态变化)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  6. Spring Cloud Eureka 自我保护机制实战分析

    前些天栈长在Java技术栈微信公众号分享过 Spring Cloud Eureka 的系列文章: Spring Cloud Eureka 自我保护机制 Spring Cloud Eureka 常用配置 ...

  7. Spring Cloud Alibaba Nacos Discovery 实战

    Nacos 作为服务注册中心,可以快速简单的将服务自动注册到 Nacos 服务端,并且能够动态无感知的刷新某个服务实例的服务列表,为分布式系统提供服务注册与发现功能 一.创建服务 1.创建项目 pom ...

  8. Spring Cloud Alibaba Nacos Config 实战

    Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持.使用 Spring Cloud Alibaba Nacos Config,您可 ...

  9. Spring cloud微服务实战——基于OAUTH2.0统一认证授权的微服务基础架构

    https://blog.csdn.net/w1054993544/article/details/78932614

  10. 微服务网关实战——Spring Cloud Gateway

    导读 作为Netflix Zuul的替代者,Spring Cloud Gateway是一款非常实用的微服务网关,在Spring Cloud微服务架构体系中发挥非常大的作用.本文对Spring Clou ...

随机推荐

  1. Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2) F. Bits And Pieces sosdp

    F. Bits And Pieces 题面 You are given an array

  2. sessionStorage 、localStorage 、 cookie 和session之间的区别

    四者的异同 特性 Session   Cookie localStorage sessionStorage 数据的生命期   在一定时间内保存在服务器上.当访问增多,会比较占用你服务器的性能,考虑到减 ...

  3. tomcat正常运行一段时间后,突然访问不了项目了

    前言 我将项目部署在tomcat服务器上,本来都是好好的,输入网站地址就能访问:但是第二天一早去就会发现网站访问提示404,文件无法找到:我就很懵了. 排查 1.我是用的是chrome浏览器,所以尝试 ...

  4. pixijs shader 制作百叶窗效果

    pixijs shader 制作百叶窗效果 直接贴代码了 const app = new PIXI.Application({ transparent: true }); document.body. ...

  5. Java-100天知识进阶-GC算法-知识铺(五)

    知识铺: 致力于打造轻知识点,持续更新每次的知识点较少,阅读不累.不占太多时间,不停的来唤醒你记忆深处的知识点. GC算法 1.标记清除算法 优缺点:不需要额外空间,但是遍历空间花费大,而且会产生大量 ...

  6. Codekicker.BBCode(BBCode 和 HTML 互转的插件)介绍

    项目地址:http://bbcode.codeplex.com/ 项目介绍: Codekicker.BBCode is a stable and performant BBCode-Parser fo ...

  7. 修改VisualStudio的智能提示字体大小

    最近换了一个高分辨率显示器,便觉得VisualStudio的字体过小了,虽然VisualStudio换字体还比较简单,大部分位置的字体基本上很顺利的就换掉了,然而一直找不到智能提示的字体所在位置,放狗 ...

  8. 基于直接缓冲区和非直接缓冲区的javaIO文件操作

    基本概念: 1. 非直接缓冲区:  指的是通过jvm来缓存数据的,应用程序要读取本地数据要经历从本地磁盘到物理内存,然后copy到jvm中,然后再通过流的方式读取到应用程序中,写的操作正好与之相反. ...

  9. 对象数组自定义排序--System.Collections.ArrayList.Sort()

    使用System.Collections.ArrayList.Sort()对象数组自定义排序 其核心为比较器的实现,比较器为一个类,继承了IComparer接口并实现int IComparer.Com ...

  10. html跳转,获取get提交参数

    html跳转到html页面,url后面携带参数,可以通过脚本获取到url?test=value地址后的参数. 1.more.html 携带参数跳转到list.html,get提交参数 2.list.h ...