从零创建微服务(一)

从零创建微服务(一),第1张

从零创建微服务(一) 从零搭建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写死了。这样肯定是不行的。那么该如何解决,下一篇将会介绍。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5696923.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存