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

从零创建微服务(一)

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

从零创建微服务(一)

从零搭建springcloud微服务(一)
  1. 新建项目,如下图所示。


选择依赖。

然后next,最后finish完成。

2.新建一个module(user-service)。



一直next。

3.这里我们使用mybatis逆向工程插件。添加插件依赖。

 
                org.mybatis.generator
                mybatis-generator-maven-plugin
                1.3.2
                
                    
                    src/main/resources/generator.xml
                    true
                    true
                
                
                    
                        Generate MyBatis Artifacts
                        
                            generate
                        
                    
                
                
                    
                        org.mybatis.generator
                        mybatis-generator-core
                        1.3.2
                    
                
            

generator.xml一般放在resources目录下(注意修改自己实际的jar包位置和数据库连接信息以及表的名称)。




    
    
    
        
            
            
        
        
        
        
        
        
            
        

        
        
            
            
            
            
        
        
        
            
            
        
        
        
            
            
        
        
        

双击运行逆行工程。

4.application.yaml

server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud-user?useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
  type-aliases-package: com.bzw.domain  #注意修改成自己的持久化类路径
  mapper-locations: classpath:mapper/*Mapper.xml
  configuration:
    map-underscore-to-camel-case: true
logging:
  level:
    cn.itcast: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS

5.service层

​ 接口:

package com.bzw.service;

import com.bzw.domain.TbUser;

public interface UserService {

    TbUser findUserById(Long id);
}

​ 实现类:

package com.bzw.service.impl;

import com.bzw.domain.TbUser;
import com.bzw.mapper.TbUserMapper;
import com.bzw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private TbUserMapper tbUserMapper;

    @Override
    public TbUser findUserById(Long id) {
        return tbUserMapper.selectByPrimaryKey(id);
    }
}

6.controller层

package com.bzw.controller;

import com.bzw.domain.TbUser;
import com.bzw.service.UserService;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("{userId}")
    public TbUser findUserById(@PathVariable("userId") Long id){
        return userService.findUserById(id);
    }
}

启动类:

package com.bzw;

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

@SpringBootApplication
public class UserServiceApplication {

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

}

7.再新建一个module(order-service)。步骤和user-service是一样的(注意修改端口号)。我们想在查询订单信息的同时也把用户信息也一起查出来,在持久化类中加一个User类。

package com.bzw.domain;

public class User {

    private Long id;

    private String username;

    private String address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address == null ? null : address.trim();
    }


}

8.注入RestTemplate,远程调用。

​ 在order-service启动类中添加如下代码:

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

9.注意service层写法。

package com.bzw.service.impl;

import com.bzw.domain.TbOrder;
import com.bzw.domain.User;
import com.bzw.mapper.TbOrderMapper;
import com.bzw.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public TbOrder findOrderById(Long id) {
        TbOrder tbOrder = new TbOrder();
        tbOrder = tbOrderMapper.selectByPrimaryKey(id);
        User user = new User();
        String url = "http://localhost:8081/user/" + tbOrder.getUserId();
        user = restTemplate.getForObject(url,User.class);
        tbOrder.setUser(user);
        return tbOrder;
    }
}

10.结果。

成功。

11.存在问题。

​ 在order-service的service层可以看到我们采用硬编码的方式,把url写死了。这样肯定是不行的。那么该如何解决,下一篇将会介绍。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/696787.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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