首页 > 编程语言 > 详细

SpringCloud

时间:2019-10-17 21:07:49      阅读:92      评论:0      收藏:0      [点我收藏+]

      Spring Cloud,基于Spring Boot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件,除了基于NetFlix的开源组件做高度抽象封装对象之外,还有一些选型中立的组件。Spring Boot构建多个微服务之后,要实现多个微服务之间的互调互通,就要使用Spring Cloud进行管理协调服务间的互调互通,从而实现分布式微服务架构。(Spring Cloud通过RESTful API实现服务间通信,Dubbo通过RPC(远程过程调用)实现服务之间的通信)。

1.Spring Cloud与Spring Boot之间的关系

      Spring Boot可以脱离Spring Cloud单独使用,Spring Cloud必须依赖Spring Cloud。Spring Cloud是关注全局的微服务协调整理管理框架。

开发  Spring Boot采用 2.0.7版本,Spring Cloud采用Finchley.SR2版本.

//构建服务提供者

<?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>microservice-cloud-01</artifactId>
<groupId>com.mengxuegu.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../microservice-cloud-01/pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-cloud-03-provider-product-8001</artifactId>
<dependencies>
<dependency>
<groupId>com.mengxuegu.springcloud</groupId>
<artifactId>microservice-cloud-02-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--springboot web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis 启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
</dependencies>
</project

  //在 src/main/resources下新建application.yml文件,配置如下:(分为开发环境、测试环境、默认环境)

server:   

  port: 8001

mybatis:
   config-location: classpath:mybatis/mybatis.cfg.xml # mybatis配置文件所在路径
   type-aliases-package: com.mengxuegu.springcloud.entities # 所有Entity别名类所在包
   mapper-locations: classpath:mybatis/mapper/**/*.xml # mapper映射文件
spring:
 application:
   name: microservice-product #这个很重要,这在以后的服务与服务之间相互调用一般都是根据这个name
   datasource:
   type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
   driver-class-name: com.mysql.cj.jdbc.Driver # mysql驱动包
   url: jdbc:mysql://127.0.0.1:3306/springcloud_db01?serverTimezone=GMT%2B8 # 数据库名称
   username: root
   password: root
   dbcp2:
     min-idle: 5 # 数据库连接池的最小维持连接数
     initial-size: 5 # 初始化连接数
     max-total: 5 # 最大连接数
     max-wait-millis: 150

//创数据库。建mapper接口

在使用@mapper注解时无效,service层,dao层注解都添加了,出现的问题应该是缺少jar包,该jar包依赖为,或者可能是jar版本号不对

<!--缺少此jar包,导致@Mapper注解无效-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
  mapper.xml 的位置出错则会出现Invalid bound statement (not found): cn.hzr0523.dao.UserMapper.insertUserInfo错误

@Mapper

public interface ProductMapper{

   Product findById(Long Pid);

   List<Product>finfAll();

   boolean addProduct(Product product);

}

在当前包的resource下新建mabatis文件夹,放mybatis.config.xml,该xml主要进行对mybatis的配置,如驼峰命名等。

第一种:mybatis的配置文件: mybatis-config.xml,其中包
括数据库连接信息,类型别名等等
特点:
名字一般是固定的
位置是src下面
示例:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<typeAlias alias="Student" type="com.XX.pojo.Student" />
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<!--
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
-->
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="username" value="test" />
<property name="password" value="test" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/briup/pojo/StudentMapper.xml" />
</mappers>
</configuration>

在当前包的resource下新建mapper文件夹,放mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.xx.xx.springcloud.ProductMapper">

  <select id="findById",resultType="Product",parameterType="Long">

    select pid, product_name, db_source from product where pid=#{pid};

  </select>

  <select id="findAll", resultType="Product">

    select pid, product_name, db_source from product;

</select>

<insert id="addProduct", parameterType="Product">

   insert into product(product_name,db_source)values(#{ProductName},DATABASE());

</insert>

</mapper>

//创建服务层

  public interface ProductService{

   boolean add(Product product);

   Product get(Long id);

   List<Product> list();

}

//构建服务接口实现类

public class ProductServiceImpl implements ProductService{

  @Autowired

    private ProductMappe mapper;

    public boolean add(Product product){

     return mapper.addProduct(product);

}

    public get(Long id){

    return mapper.findProductById(id);

}

    public List<Product> list(){

    return mapper.list();

}

}

//创建控制层-微服务提供者 REST

  @RestController

 public class ProductController{

 @Autowired

  private ProductService productservice;

  //value表示请求路径,method表示请求方式@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写

    //  @GetMapping("/getUserById/{id}")
    //public User getUser(@PathVariable("id") Long userId){
   //return userService.selectUserById(userId);
   //  }

   //@RequestBody表示参数为对象类型  ,如果是单个或者多个参数,则在Service中的方法参数加上@RequestParam,在spring3里向Rest注册时时使用@PathVariable可以将URL

//中占位符参数绑定到控制器处理方法中

  @RequestMapping(value = "/product/add", method = "RequestMethod.POST")

   public boolean add(@RequestBody Product product){

    return productservice.add(product);

  }

 @RequestMapping(value = "/product/get/{id}" method="RequestMethod.GET")

  public Product get(@PathVariable("id") Long id){

   return productservice.get(id);

}

  @RequestMapping(value = "/product/list" method="RequestMethod.POST")

  public List<Product> list(){

    return productservice.list();

}

}

@MapperScan("com.mengxuegu.springcloud.mapper") //扫描所有Mapper接口
@SpringBootApplication
public class ProductProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(MicroserviceProductProvider_8001.class, args);
}

SpringCloud

原文:https://www.cnblogs.com/zhangwan/p/11692766.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!