赞
踩
新建项目springcloud-alibaba,本工程不需要写代码,删除src
导包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> <springboot.version>2.1.0.RELEASE</springboot.version> <spring-cloud-alibaba.version>2.1.0.RELEASE</spring-cloud-alibaba.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring-cloud-alibaba.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
shop-common
导包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.58</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
创建实体类
@Entity(name="shop_user")//实体类跟数据表的对应
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增
private Integer uid;//主键
private String username;//用户名
private String password;//密码
private String telephone;//手机号
}
@Entity(name="shop_product")//实体类跟数据表的对应
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增
private Integer pid;//主键
private String pname;//商品名称
private Double pprice;//商品价格
private Integer stock;//库存
}
@Entity(name="shop_order")//实体类跟数据表的对应 @Data public class Order { @Id @GeneratedValue(strategy = GenerationType.IDENTITY)//数据库自增 private Long oid;//订单id private Integer uid;//用户id private String username;//用户名 private Integer pid;//商品id private String pname;//商品名称 private Double pprice;//商品价格 private Integer number;//购买数量 }
创建新工程shop-user
导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>cn.ming</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
server: port: 8071 spring: application: name: service-user datasource: url: jdbc:mysql://localhost:3306/shop?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true driver-class-name: com.mysql.jdbc.Driver username: root password: root jpa: properties: hibernates: hbm2ddl: auto: update dialect: org.hibernate.dialect.MySQL5InnoDBDialect
启动类
@SpringBootApplication
public class UserApp {
public static void main(String[] args) {
SpringApplication.run(UserApp.class,args);
}
}
controller(略),service(略),dao
public interface UserDao extends JpaRepository<User,Integer> {
}
略
略
在微服务架构中,最常见的场景就是微服务之间的相互调用。我们以电商系统中常见的 用户下单 为例来演示微服务的调用:客户向订单微服务发起一个下单的请求,在进行保存订单之前需要调用商品微服务查询商品的信息。
我们一般把服务的主动调用方称为服务消费者,把服务的被调用方称为服务提供者。
在这种场景下,订单微服务就是一个服务消费者,商品微服务就是一个服务提供者。
商品服务编写查询商品代码(见工程)
启动服务,浏览器访问测试:http://localhost:8081/product/1
订单服务添加下单代码(见工程)
启动服务,浏览器访问测试:http://localhost:8091/order/product/1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。