微服务&springCloud

什么是微服务

  •  

微服务与微服务架构

  •  

微服务优缺点

  • 优点
  • 缺点

微服务技术栈有哪些

  •  

为什么选择springCloud作为微服务架构

  •  

springCloud是什么

  •  

springCloud与springCloud区别

  •  

springCloud与Dubbo区别

  •  

微服务构建

 父工程搭建

  • 搭建一个maven项目,什么都不要勾选  --> spring_cloud
  • 删除src
  • pom
     <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
        </properties>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Dalston.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
                <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <version>1.5.9.RELEASE</version>
                </dependency>
    
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
    
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>5.0.4</version>
                </dependency>
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid</artifactId>
                    <version>1.0.31</version>
                </dependency>
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>1.3.0</version>
                </dependency>
                <dependency>
                    <groupId>ch.qos.logback</groupId>
                    <artifactId>logback-core</artifactId>
                    <version>1.2.3</version>
                </dependency>
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                    <scope>test</scope>
                </dependency>
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
    
                <!--热部署,立马生效-->
                <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>springloaded</artifactId>
                    <version>1.2.0.RELEASE</version>
                </dependency>
                <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-devtools -->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-devtools</artifactId>
                    <version>1.5.9.RELEASE</version>
                </dependency>

API公共模块搭建

  • 搭建模块API(maven)
  • 创建Dept,目的让其他模块公共引用,提高代码复用
  • Dept
    @NoArgsConstructor //无参构造
    @Data //提供getset方法
    @Accessors // 赋值的时候连着赋值
    public class Dept implements Serializable {
    
        private Long deptId;
        private String dName;
        private String dbSource;
    }
  • pom

           //目的是dept的注解起作用
             <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>${lombok.version}</version>
            </dependency>

搭建服务提供者项目

  • 搭建模块cloud_provider(先创建maven,然后在写springboot启动项目,一下的微服务和springboot都会这样操作)
  • pom
      <dependency>
                <groupId>com.springcloud</groupId>
                <artifactId>cloud_api</artifactId>
                <version>${project.version}</version>
            </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>
    
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jetty -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jetty</artifactId>
                <version>2.1.6.RELEASE</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
    
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>
    
            <!-- eureka客户端注册需要引入  -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    
            <!-- eureka 点击服务名称跳转页面的内容监控 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <version>1.5.9.RELEASE</version>
            </dependency>
    
  • 整合mybatis,具体看git

  • DeptController,主要是暴露resultful风格的API,区别于RPC的API
    package com.springcloud.controller;
    
    
    import com.hongdou.entity.Dept;
    import com.springcloud.service.DeptService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.cloud.client.ServiceInstance;
    
    import org.springframework.web.bind.annotation.*;
    
    import java.util.List;
    
    @RestController
    public class DeptController {
    
        @Autowired
        private DeptService deptService;
    
        @RequestMapping(value = "/dept/add", method = RequestMethod.POST)
        public boolean add(@RequestBody Dept dept) {
            return deptService.add(dept);
        }
    
        @RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
        public Dept get(@PathVariable("id") Long id) {
            return deptService.get(id);
        }
    
        @RequestMapping(value = "/dept/list", method = RequestMethod.GET)
        public List<Dept> list() {
            return deptService.list();
        }
    
    
    
    
    }
    
  • properties.yml

    server:
      port: 8081
    
    mybatis:
      config-location: classpath:mybatis/mybatis.cfg.xml #mybatis配置文件所在路径
      type-aliases-package: com.hongdou.entity #所有Entity别名类所在的包
      mapper-locations:
      - classpath:mybatis/mapper/**/*.xml  #mapper 映射的文件
    
    spring:
      application:
        name: cloud-provider
      datasource:
        driver-class-name: org.gjt.mm.mysql.Driver  #mysql驱动包
        url: jdbc:mysql://192.168.3.75:3306/xiaoke  #s数据库名称
        type: com.alibaba.druid.pool.DruidDataSource #当前数据源操作类型
        username: root
        password: 123456
        dbcp2:
          min-idle: 5  #数据库连接池的最小维持连接数
          initial-size: 5 #初始化连接数
          max-total: 5   #最大连接数
          max-wait-millis: 200  #等待连接获取的最大超时时间

搭建服务消费者项目

  • 搭建consumer_dept项目,调用resultful服务方项目
  • application.yml
    server:
      port: 8082
  • controller,这里为了方便直接用contrller层调用了,主要使用RestTemplate调用

    package com.springcloud.controller;
    
    import com.hongdou.entity.Dept;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.client.RestTemplate;
    
    
    import java.util.List;
    
    @RestController
    @RequestMapping("consumer")
    public class DeptController {
    
        private static final String REST_URL_PREFIX = "http://localhost:8081";
    
        @Autowired
        private RestTemplate restTemplate;
    
        @GetMapping("/dept/add")
        public  boolean add(Dept dept){
            return restTemplate.postForObject(REST_URL_PREFIX + "/dept/add/", dept, Boolean.class);
        }
    
        @GetMapping(value = "/dept/get/{id}")
        public Dept get(@PathVariable("id") Long id){
            return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id,Dept.class);
        }
    
        @GetMapping("/dept/list")
        public List<Dept> list(){
            return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
        }
    
        @RequestMapping(value = "/dept/discovery", method = RequestMethod.GET)
        public Object getDiscovery(){
         return restTemplate.getForObject(REST_URL_PREFIX + "/dept/discovery", Object.class);
        }
    
    }
    
  • 配置类

    package com.springcloud.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    @Configuration
    public class ConfigBean {
    
        @Bean
        public RestTemplate getRestTemplate() {
            return new RestTemplate();
        }
    }
    
  • pom

            <dependency>
                <groupId>com.springcloud</groupId>
                <artifactId>cloud_api</artifactId>
                <version>${project.version}</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <!-- Ribbon相关 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-eureka</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-ribbon</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-config</artifactId>
            </dependency>
    
            <!--热部署-->
            <!-- https://mvnrepository.com/artifact/org.springframework/springloaded -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
            </dependency>

git地址:[email protected]:Xiaokeworksveryhard/spring_cloud.git

发布了176 篇原创文章 · 获赞 27 · 访问量 10万+

猜你喜欢

转载自blog.csdn.net/qq_41650354/article/details/104078335