首页 > 其他分享 >springcloud之模拟微服务环境

springcloud之模拟微服务环境

时间:2022-11-30 18:35:21浏览次数:62  
标签:product 服务 cn springcloud springframework itcast import org 模拟


创建聚合服务

1-创建父功能new Maven Project

springcloud之模拟微服务环境_spring

修改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">
<modelVersion>4.0.0</modelVersion>

<groupId>cn.test</groupId>
<artifactId>spring_cloud_demo</artifactId>
<version>1.0-SNAPSHOT</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!--省略getter,setter-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.6</version>
<scope>provided</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/libs-snapshot-local</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone-local</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

删掉src目录

springcloud之模拟微服务环境_spring_02

2-父工程项目右键 new module

springcloud之模拟微服务环境_maven_03

springcloud之模拟微服务环境_xml_04

服务提供者,向外界暴露一系列的服务,通过其他工程去调用这个工程.提供一个服务,对外暴露增删改查功能

springcloud之模拟微服务环境_spring_05

修改子工程的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>spring_cloud_demo</artifactId>
<groupId>cn.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>product_service</artifactId>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>
</project>

3-新建数据库表

springcloud之模拟微服务环境_maven_06

springcloud之模拟微服务环境_spring_07

子模块product_service中创建商品实体类,写在src->java->cn.itcast.product.entity包中

package cn.itcast.product.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.math.BigDecimal;

/**
* 商品实体类
*/
@Data //lombok省略toString,getter,setter方法
@Entity //指名这是一个实体Bean
@Table(name = "tb_product") //@Table注释指定了Entity所要映射带数据库表,其中@Table.name()用来指定映射表的表名。
// 如果缺省@Table注释,系统默认采用类名作为映射表的表名。
public class Product {
@Id //标明主键
private Long id;

private String productName;
private Integer status;
private BigDecimal price;
private String productDesc;
private String caption;
private Integer inventory;
}

4-子模块product_service,新建dao包,新建ProductDao接口

springcloud之模拟微服务环境_xml_08

增删改查,分页排序方法已经自动有了

package cn.itcast.product.dao;

import cn.itcast.product.entity.Product;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

/**
* 使用data jpa,接口需要继承两个
* SpringDataJpa的优点:它的Dao层只需要写接口,不需要写实现类,只需要写一个接口继承JpaRepository接口即可,
* 该接口有两个泛型<T,ID>,在Dao层接口里可以什么都不用写。 T:domain实体类类型, ID:domain实体类的主键字段类型
*/
public interface ProductDao extends JpaRepository<Product,Long>, JpaSpecificationExecutor<Product> {
}

5-子模块product_service,新建service包,ProductService接口

package cn.itcast.product.service;

import cn.itcast.product.entity.Product;

public interface ProductService {
/**
* 根据id查询
*/
Product findById(Long id);
/**
* 保存
*/
void save(Product product);
/**
* 更新
*/
void update(Product product);
/**
* 删除
*/
void delete(Long id);
}

6-子模块product_service,在service包中新建impl包,ProductServiceImpl类

package cn.itcast.product.service.impl;

import cn.itcast.product.dao.ProductDao;
import cn.itcast.product.entity.Product;
import cn.itcast.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductDao productDao;

@Override
public Product findById(Long id) {
return productDao.findById(id).get();
}

@Override
public void save(Product product) {
productDao.save(product);
}

@Override
public void update(Product product) {
productDao.save(product);
}

@Override
public void delete(Long id) {
productDao.deleteById(id);
}
}

7-子模块product_service,新建controller包,ProductController类

package cn.itcast.product.controller;

import cn.itcast.product.entity.Product;
import cn.itcast.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/product")
public class ProductController {

@Autowired
private ProductService productService;

@RequestMapping(value = "/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
Product product = productService.findById(id);
return product;
}

@RequestMapping(value = "",method = RequestMethod.POST)
//@RequestBody主要用来接收前端传递给后端的json字符串中的数据的(请求体中的数据的);
//GET方式无请求体,所以使用@RequestBody接收数据时,前端不能使用GET方式提交数据,而是用POST方式进行提交。
public String save(@RequestBody Product product){
productService.save(product);
return "保存成功";
}
}

8-子模块product_service,添加启动类ProductApplication

package cn.itcast.product;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan("cn.itcast.product.entity") //用来扫描和发现指定包及其子包中的Entity定义
public class ProductApplication {
public static void main(String[] args) {
SpringApplication.run(ProductApplication.class,args);
}
}

9-子模块product_service,resources包下新建application.yml

server:
port: 9001
spring:
application:
name: service-product
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 123456
jpa:
database: MySQL
show-sql: true
open-in-view: true

10-运行子模块product_service的启动类ProductApplication

springcloud之模拟微服务环境_maven_09

11-父工程下new module新建order_service模块, maven

添加依赖,修改pom.xml

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
</dependencies>

12-在子模块order_service中,resources包下新建application.yml

server:
port: 9002
spring:
application:
name: service-order
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
username: root
password: 123456
jpa:
database: MySQL
show-sql: true
open-in-view: true

13-在子模块order_service中,java包下新建cn.itcast.order包,新建启动类OrderApplication

package cn.itcast.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EntityScan("cn.itcast.order.entity") //用来扫描和发现指定包及其子包中的Entity定义
public class OrderApplication {

/**
* 使用spring提供的RestTemplate发送http请求到商品服务
* 1-创建RestTemplate对象交给容器管理
* 2-在使用的时候,调用其方法完成操作(getXX,postXXX)
*/

@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}

public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}

14-将Service-Product中的entity包复制一份到Service-Order中

package cn.itcast.order.entity;

import lombok.Data;
import java.math.BigDecimal;

/**
* 商品实体类
*/
@Data //lombok省略toString,getter,setter方法
public class Product {
private Long id;
private String productName;
private Integer status;
private BigDecimal price;
private String productDesc;
private String caption;
private Integer inventory;
}

15-在order-service子模块中,新建controller包,OrderController类

package cn.itcast.order.controller;

import cn.itcast.order.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

//注入restTemplate
@Autowired
private RestTemplate restTemplate;

/**
*
* @param id 商品id
* @return
* 通过订单系统,调用商品服务根据id查询商品信息
* 1-需要配置商品服务
* 2-需要调用商品服务
*/
@RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
public Product findById(@PathVariable Long id){
Product product = null;
//如何调用远程服务?
product = restTemplate.getForObject("http://127.0.0.1:9001/product/1",Product.class);
return product;
}
}

16-启动OrderApplication启动类

springcloud之模拟微服务环境_maven_10

模拟微服务中存在的问题

springcloud之模拟微服务环境_xml_11

标签:product,服务,cn,springcloud,springframework,itcast,import,org,模拟
From: https://blog.51cto.com/u_12528551/5900115

相关文章

  • springcloud之注册中心概述
        微服务的注册中心:记录了服务和服务地址的映射关系.在分布式架构中,服务会注册到这里,当服务需要调用其他服务时,就在这里找到服务的地址,进行调用.注册中心包......
  • springcloud之Eureka概述
    Eureka是Netflix开发的服务发现框架,SpringCloud将它集成在自己的子项目spring-cloud-netflix中,实现SpringCloud的服务发现功能.Eureka的基础架构由3个角色组成:1-EurekaS......
  • springcloud之使用eureka例子
    搭建EurekaServer1-创建工程 eureka_server子模块2-导入坐标eureka_server的pom.xml<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/P......
  • springcloud之使用eureka例子&实现高可用
    1-准备2(.../n)个EurekaServer,需要相互注册  1号server:9000  首先,将eureka_server子模块的resources中的application.yml修改如下server:port:9000#端口#配......
  • springcloud之eureka server启动原理
    /org/springframework/cloud/spring-cloud-netflix-eureka-server/2.1.0.RELEASE/spring-cloud-netflix-eureka-server-2.1.0.RELEASE.jar!/META-INF/spring.factories中 ......
  • springcloud之eureka client的启动流程
    eurekaclien向eurekaserver准备的web接口发送http请求,完成服务注册,获取...org/springframework/cloud/spring-cloud-netflix-eureka-client/2.1.0.RELEASE/spring-cloud......
  • springcloud之负载均衡策略
    Ribbon内置了多种负载均衡策略,内部负责负载均衡的顶级接口为com.netflix.loadbalanced.IRule  服务消费者,application.yml中添加#修改ribbon的负载均衡策略服务名-......
  • 微服务减少jar包体积
    <build><finalName>${project.artifactId}</finalName><plugins><!--打包jar--><plugin><groupId>org.apa......
  • nodejs 服务器监听的主机名称导致的本地前端代理错误
    问题前端本地开发服务器一直是正常的,直到某一天启动时出现这个错误:代理服务的配置如下:(http://localhost:8000跑着一个本地启动的nodejs服务器。)const{createProxyMid......
  • the--openssl升级引起的openstack服务异常问题
    1.问题查看openstack服务,各个服务报错提示:  PAMunabletodlopen(/lib64/security/pam_tally.so):/lib64/security/pam_tally.so:cannotopensharedobjectfile......