SpringCloud01:SpringCloud介绍、服务提供者、服务消费者

SpringCloud和SpringBoot的关系

SpringBoot专注于快速、方便地开发单个个体微服务,SpringCloud关注全局的治理框架,它将SpringBoot开发的一个个单体微服务整合并管理起来,为各个微服务之间提供:管理配置、服务发现、断路器、路由、微代理、事件总线、全局锁、决策竞选、分布式会话等等集成服务

SpringCloud

SpringCloud和Dubbo的最大区别:SpringCloud抛弃了Dubbo的RPC通信,采用的是基于HTTP的REST方式

自学网站

Spring Cloud Netfix:Spring Cloud Netflix【spring cloud中文版】--spring cloud中文文档

中文API文档:Spring Cloud Dalston 中文文档 参考手册 中文版

SpringCloud中国社区:Spring Cloud中国社区

SpringCloud中文网:Spring Cloud中文网-官方文档中文版

SpringCloud版本选择

版本说明

SpringCloud01:SpringCloud介绍、服务提供者、服务消费者_第1张图片

 实际开发版本关系

SpringCloud01:SpringCloud介绍、服务提供者、服务消费者_第2张图片

创建父工程

创建一个maven工程,去掉src目录

SpringCloud01:SpringCloud介绍、服务提供者、服务消费者_第3张图片

配置pom.xml



    4.0.0

    org.example
    SpringCloud
    1.0-SNAPSHOT

    
    pom

    
        UTF-8
        1.8
        1.8
        4.12
        1.2.17
        1.16.18
    

    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Greenwich.SR1
                pom
                import
            
            
            
                org.springframework.boot
                spring-boot-dependencies
                2.1.4.RELEASE
                pom
                import
            
            
            
                mysql
                mysql-connector-java
                5.1.47
            
            
                com.alibaba
                druid
                1.1.10
            
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                1.3.2
            
            
            
                ch.qos.logback
                logback-core
                1.2.3
            
            
                junit
                junit
                ${junit.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
            
                org.projectlombok
                lombok
                ${lombok.version}
            
        
    

服务提供者

一、创建子module1(springcloud-api)

1、创建数据库

create database db01
use db01

create table dept(
	deptno int primary key not null auto_increment,
	dname varchar(20),
	db_source varchar(20)
)

insert into dept(dname,db_source) values('开发部',DATABASE());
insert into dept(dname,db_source) values('销售部',DATABASE());
insert into dept(dname,db_source) values('运维部',DATABASE());
insert into dept(dname,db_source) values('测试部',DATABASE());
insert into dept(dname,db_source) values('财务部',DATABASE());

2、创建实体类

@Data
@NoArgsConstructor
@Accessors(chain = true) //链式写法
public class Dept implements Serializable {
    private Integer deptno;
    private String dname;
    //表示这个数据是存在哪个数据库的字段
    //微服务:一个服务对应一个数据库,同一个信息可能存在不同的数据库
    private String dbSource;

    public Dept(String dname) {
        this.dname = dname;
    }
}

二、创建子模块2(springcloud-provider-dept-8081)

1、pom.xml



    
        SpringCloud
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    springcloud-provider-dept-8001

    
        
        
            org.example
            springcloud-api
            1.0-SNAPSHOT
        
        
        
            junit
            junit
            test
        
        
            mysql
            mysql-connector-java
        
        
            com.alibaba
            druid
        
        
            ch.qos.logback
            logback-core
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
        
            org.springframework.boot
            spring-boot-test
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-jetty
        
        
        
            org.springframework.boot
            spring-boot-devtools
        
        
            org.example
            springcloud-api
            1.0-SNAPSHOT
            compile
        
    

2、Dao

①DeptDao

@Mapper
@Repository
public interface DeptDao {
    public Integer addDept(Dept dept);
    public Dept queryById(Integer deptno);
    public List queryAll();
}

②DeptMapper.xml




    
        
        
        
    

   
       insert into dept(dname, db_source) values(#{dname}, DATABASE())
   

    

    

3、application.xml

server:
  port: 8001

#mybatis配置
mybatis:
  type-aliases-package: com.springcloud.pojo
  mapper-locations: classpath:mybatis/mapper/*.xml

#spring配置
spring:
  application:
    name: springcloud-provider-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    drive-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://localhost:3306/db01?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: root
    password: 666666QIU

4、service

①DeptService

public interface DeptService {
    public Integer addDept(Dept dept);
    public Dept queryById(Integer deptno);
    public List queryAll();
}

②DeptServiceImpl

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDao deptDao;

    @Override
    public Integer addDept(Dept dept) {
        return deptDao.addDept(dept);
    }

    @Override
    public Dept queryById(Integer deptno) {
        return deptDao.queryById(deptno);
    }

    @Override
    public List queryAll() {
        return deptDao.queryAll();
    }
}

5、controller

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @PostMapping("/dept/add")
    public Integer addDept(Dept dept){
        return deptService.addDept(dept);
    }

    @GetMapping("/dept/get/{deptno}")
    public Dept queryById(@PathVariable("deptno") Integer deptno){
        return deptService.queryById(deptno);
    }

    @GetMapping("/dept/list")
    public List queryAll(){
        return deptService.queryAll();
    }
}

6、主启动类

//启动类
@SpringBootApplication
public class DeptProvider_8001 {
    public static void main(String[] args){
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}

服务消费者

三、创建子模块3(springcloud-consumer-dept-80)

1、pom.xml



    
        SpringCloud
        org.example
        1.0-SNAPSHOT
    
    4.0.0

    springcloud-consumer-dept-80

    
    
        
            org.example
            springcloud-api
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-devtools
        
    


2、 application.xml

server:
  port: 8080

3、ConfigBean(用来注册RestTemplate的Bean)

@Configuration
public class ConfigBean {
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4、controller

@RestController
public class DeptConsumerController {
    //理解:消费者,不应该有service
    @Autowired
    private RestTemplate restTemplate;  //提供多种便捷访问远程http服务的方法,简单的Restful服务模板

    private static final String REST_URL_PREFIX = "http://localhost:8001";

    @RequestMapping("/consumer/dept/add")
    public Integer addDept(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add", dept,Integer.class);
    }

    @RequestMapping("/consumer/dept/get/{deptno}")
    public Dept queryById(@PathVariable("deptno") Integer deptno){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + deptno, Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    public List list(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }
}

你可能感兴趣的:(spring,cloud,spring,boot,java,intellij-idea)