栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud Netflix 系列-02.Rest学习环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud Netflix 系列-02.Rest学习环境搭建

一、版本选择?
Release TrainBoot Version
2021.0.x aka Jubilee2.6.x
2020.0.x aka Ilford2.4.x, 2.5.x (Starting with 2020.0.3)
Hoxton2.2.x, 2.3.x (Starting with SR5)
Greenwich2.1.x
Finchley2.0.x
Edgware1.5.x
Dalston1.5.x

注意:Spring Cloud 官方已经停止对 Dalston、Edgware、Finchley 和 Greenwich 的版本更新。

二、创建maven父工程

导入相关依赖



    4.0.0

    com.sunl
    SpringCloud
    1.0-SNAPSHOT

    
    pom
    
    
        UTF-8
        1.8
        1.8
        4.13.2
        1.18.22
        1.2.17
    

    
    
        
            
            
                org.springframework.cloud
                spring-cloud-dependencies
                Hoxton.SR12
                pom
                import
            
            
            
                org.springframework.boot
                spring-boot-dependencies
                2.3.2.RELEASE
                pom
                import
            
            
            
                mysql
                mysql-connector-java
                8.0.28
            
            
                com.alibaba
                druid
                1.2.8
            
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                2.2.2
            
            
            
                ch.qos.logback
                logback-core
                1.2.3
            
            
                junit
                junit
                ${junit.version}
            
            
                log4j
                log4j
                ${log4j.version}
            
            
                org.projectlombok
                lombok
                ${lombok.version}
            
        
    

三、创建新的提供者module 1.导入相关依赖


    
        SpringCloud
        com.sunl
        1.0-SNAPSHOT
    
    4.0.0

    cloud-provider-dept-8001

    
        8
        8
    

    
        
        
            com.sunl
            cloud-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
        
    


2.配置提供者文件
server:
  port: 8001

mybatis:
  type-aliases-package: com.sunl.cloud.entity
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper
@Mapper
@Repository
public interface DeptMapper {
    
    public boolean addDept(Dept dept);

    
    public Dept queryDept(Long id);

    
    public List getAllDept();


}




    
        insert into dept(dept_name,db_source)
        values (#{deptName},DATABASE());
    

    
        select dept_no deptNo, dept_name deptName, db_source dbSource from dept where dept_no = #{deptNo};
    

    

5.编写业务层代码:DeptService.java、DeptServiceImpl.java
package com.sunl.cloud.service;

import com.sunl.cloud.entity.Dept;

import java.util.List;


public interface DeptService {
    
    public boolean addDept(Dept dept);

    
    public Dept queryDept(Long id);

    
    public List getAllDept();
}
package com.sunl.cloud.service.impl;

import com.sunl.cloud.entity.Dept;
import com.sunl.cloud.mapper.DeptMapper;
import com.sunl.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    
    @Override
    public boolean addDept(Dept dept) {
        return deptMapper.addDept(dept);
    }

    
    @Override
    public Dept queryDept(Long id) {
        return deptMapper.queryDept(id);
    }

    
    @Override
    public List getAllDept() {
        return deptMapper.getAllDept();
    }
}
6.编写控制层代码:DeptController.java
package com.sunl.cloud.controller;

import com.sunl.cloud.entity.Dept;
import com.sunl.cloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


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

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

    @GetMapping("/dept/get/{id}")
    public Dept getDept(@PathVariable("id") Long id) {
        Dept dept = deptService.queryDept(id);
        if (dept == null) {
            throw new RuntimeException("Fail");
        }
        return dept;
    }

    @GetMapping("/dept/list")
    public List queryAll() {
        deptService.getAllDept()
        return deptService.getAllDept();
    }
}
7.编写提供者启动类
package com.sunl.cloud;

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


@SpringBootApplication
public class DeptProvider_8001 {
    public static void main(String[] args) {
        SpringApplication.run(DeptProvider_8001.class,args);
    }
}
三、创建新的消费者module 1.编写提供者启动类:ConsumerDept_80.java
package com.sun.cloud;

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


@SpringBootApplication
public class ConsumerDept_80 {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerDept_80.class,args);
    }
}
2.编写控制层代码:DeptConsumerController.java
package com.sun.cloud.controller;

import com.sunl.cloud.entity.Dept;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;


@RestController
public class DeptConsumerController {

    
    @Autowired
    private RestTemplate restTemplate;

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

    @RequestMapping("/consumer/dept/add")
    @ResponseBody
    public boolean addDept(Dept dept){
        //远程只能用  post 方式请求,那么这里也只能通过 post 方式获取
        return Boolean.TRUE.equals(restTemplate.postForObject(REST_URL_PREFIX + "/dept/add", dept, Boolean.class));
    }

    @RequestMapping("/consumer/dept/get/{id}")
    @ResponseBody
    public Dept queryDept(@PathVariable("id") Long id){
        //service不在本项目中,所以要去远程项目获取
        //远程只能用  get 方式请求,那么这里也只能通过 get 方式获取
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/get/" + id, Dept.class);
    }

    @RequestMapping("/consumer/dept/list")
    @ResponseBody
    public List queryAllDept(){
        return restTemplate.getForObject(REST_URL_PREFIX + "/dept/list", List.class);
    }
}
3.ConfigBean配置代码:ConfigBean.java
package com.sun.cloud.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();
    }
}

4.导入相关依赖


    
        SpringCloud
        com.sunl
        1.0-SNAPSHOT
    
    4.0.0

    cloud-consumer-dept-80

    
        8
        8
    

    
    
        
            com.sunl
            cloud-api
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-devtools
        
    


5.配置提供者文件
server:
  port: 80
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/888393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号