前言:一段时间来忙忙碌碌,回头发现很多技术已经更新了很多,很长一段时间都在忙其他的事情,以至于没有太多关注于微服务更新,今天更新了下版本以便于后期使用,突然发现很多东西已经被删除或直接改变了,…。
版本:
springboot:2.7.6
springcloud:2021.0.5
spring-cloud-alibaba-dependencies:2021.0.1.0
nacos:2.1.2
为了更为真实的模拟开发环境,这里将分为以下模块:
1.父工程(管理版本)
2.公共模块:Common-API
3.服务提供者:NacosClientProvider8001,NacosClientProvider8001(为了测试负载均衡这里准备两个模块,当让准备一个模块也可以,使用idea虚拟一个模块也可以)
4.服务消费者:NacosClientConsumer80
(一)父工程
此模块只为了版本控制,所以只有pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qwy</groupId>
<artifactId>springcloud</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>Common-API</module>
<module>NacosClientProvider8001</module>
<module>NacosClientProvider8002</module>
<module>NacosClientConsumer80</module>
</modules>
<!--表明是父工程-->
<packaging>pom</packaging>
<!-- 统一管理jar包版本 -->
<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>
<mysql.version>5.1.47</mysql.version>
<druid.version>1.1.16</druid.version>
<mybatis.spring.boot.version>2.2.2</mybatis.spring.boot.version>
<mybatis.plus.boot.version>3.5.2</mybatis.plus.boot.version>
</properties>
<!-- 子模块继承之后,提供作用:锁定版本+子modlue不用写groupId和version -->
<dependencyManagement>
<dependencies>
<!-- <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
<version>2.7.6</version>
</dependency>-->
<!--spring boot 2.7.6-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.7.6</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud 2021.0.5-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2021.0.1.0-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2021.0.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatis.plus.boot.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.spring.boot.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
<optional>true</optional>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
此配置中引入对了albaba的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>2021.0.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
(二)公共模块Common-API
1.pom.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>Common-API</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.1.0</version>
</dependency>
</dependencies>
</project>
2.配置公共的bean
此类主要为了一般对应数据库中的字段
a.Users.java
package com.qwy.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @Author:齐
* @Package:com.qwy.bean
* @Project:springcloud1
* @name:Users
* @Date:2022/12/11 2:33
* @Filename:Users
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "tb_user")
public class Users {
@TableId(type = IdType.AUTO)
private Long id;
private String username;
@TableField(exist = false)
private Integer age;
@TableField(exist = false)
private Date birthday;
private String address;
}
此类主要为了一般对应数据库中的字段
b.Order.java
package com.qwy.bean;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
/**
* @Author:齐
* @Package:com.qwy.bean
* @Project:springcloud1
* @name:Orders
* @Date:2022/12/11 3:52
* @Filename:Orders
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("tb_order")
public class Orders {
@TableId(type = IdType.AUTO)
private Long id;
@TableField(exist = false)
private Date createTime;
@TableField(value = "user_id")
private Long usersId;
private String name;
private Long price;
private Integer num;
}
此类主要为了对POJO进行扩展,以便以后封装条件或对数据库查询出的数据进行综合封装等作用
c.ExtendsOrder.java
package com.qwy.bean;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @Author:齐
* @Package:com.qwy.bean
* @Project:springcloud1
* @name:ExtendsOrders
* @Date:2022/12/11 4:06
* @Filename:ExtendsOrders
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExtendsOrders extends Orders {
@TableField(exist = false)
private Users users;
}
d.Code.java
此类主要封装以后Controler响应的状态码,以便和前端程序员进行友好的交互
package com.qwy.bean;
public class Code {
public static final Integer SAVE_OK=20021;
public static final Integer DELETE_OK=20022;
public static final Integer UPDATE_OK=20023;
public static final Integer SELECT_OK=20024;
public static final Integer SAVE_ERR=20011;
public static final Integer DELETE_ERR=20012;
public static final Integer UPDATE_ERR=20013;
public static final Integer SELECT_ERR=20014;
public static final Integer SYSTEM_ERR=20031;
public static final Integer BUSINESS_ERR=20032;
public static final Integer UNKNOWN_ERR=20033;
}
e.CommonResult.java
此类主要封装以后Controller返回的数据。
package com.qwy.bean;
/**
* @Author:齐
* @Package:com.qwy.bean
* @Project:springcloud1
* @name:CommonResult
* @Date:2022/12/11 2:36
* @Filename:CommonResult
*/
public class CommonResult<T> {
private Integer code;
private T data;
private String message;
public CommonResult() {
}
public CommonResult(Integer code, T data) {
this.code = code;
this.data = data;
}
public CommonResult(Integer code,T data, String message) {
this.code = code;
this.data = data;
this.message = message;
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
3.公共的异常处理类
a.BusinessException.java
此类主要处理用户业务异常处理。
package com.qwy.exception;
/**
* 定义业务异常处理类
*/
public class BusinessException extends RuntimeException {
private Integer code;
public BusinessException(Integer code) {
this.code = code;
}
public BusinessException(Integer code, String message) {
super(message);
this.code=code;
}
public BusinessException(Integer code, String message, Throwable cause) {
super(message, cause);
this.code=code;
}
}
b.SystemException.java
此类主要处理一些系统异常
package com.qwy.exception;
/**
* 定义系统异常处理类
*/
public class SystemException extends RuntimeException {
private Integer code;
public SystemException( Integer code) {
this.code = code;
}
public SystemException(Integer code, String message) {
super(message);
this.code=code;
}
public SystemException(Integer code,String message, Throwable cause) {
super(message, cause);
this.code=code;
}
}
c.ExceptionResultAdvice.java
此次处理Controller处理异常的返回。
package com.qwy.exception;
import com.qwy.bean.Code;
import com.qwy.bean.CommonResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice//该注解表明该类为异常处理类
public class ExceptionResultAdvice {
@ExceptionHandler({SystemException.class})
public CommonResult doSystemException(SystemException sx){
return new CommonResult(Code.SYSTEM_ERR,null,sx.getMessage());
}
@ExceptionHandler({BusinessException.class})
public CommonResult doBusinessException(BusinessException bx){
return new CommonResult(Code.SYSTEM_ERR,null,bx.getMessage());
}
@ExceptionHandler({Exception.class})
public CommonResult doException(Exception e){
return new CommonResult(Code.SYSTEM_ERR,null,e.getMessage());
}
}
(三)服务提供者ConsulClientProvider8001
此模块模拟服务的提供者
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ConsulClientProvider8001</artifactId>
<dependencies>
<!--SpringCloud consul-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--自己的jar-->
<dependency>
<groupId>com.qwy</groupId>
<artifactId>Common-API</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
注意引入对nacos的注册发现依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-discovery</artifactId>
</dependency>
2.application.yml
server:
port: 8001
spring:
application:
name: consul-client-provider
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/mydb
username: root
password: admin
cloud:
consul:
host: localhost
port: 8500
discovery:
service-name: ${spring.application.name}
3.主启动类
package com.qwy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author:齐
* @Package:com.qwy
* @Project:springcloud1
* @name:ConsulClientProvider8001
* @Date:2022/12/12 16:27
* @Filename:ConsulClientProvider8001
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ConsulClientProvider8001 {
public static void main(String[] args) {
SpringApplication.run(ConsulClientProvider8001.class,args);
}
}
3.持久层、服务层、控制层
a.持久层
package com.qwy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qwy.bean.Users;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author:齐
* @Package:com.qwy.mapper
* @Project:springcloud1
* @name:UsersMapper
* @Date:2022/12/11 21:49
* @Filename:UsersMapper
*/
@Mapper
public interface UsersMapper extends BaseMapper<Users> {
}
b. 服务层
接口
package com.qwy.service;
import com.qwy.bean.Users;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author:齐
* @Package:com.qwy.service
* @Project:springcloud1
* @name:OrderService
* @Date:2022/12/11 21:47
* @Filename:OrderService
*/
@Transactional
public interface UsersService {
boolean save(Users users);
Users findById(Long id);
}
实现类
package com.qwy.service.impl;
import com.qwy.bean.Users;
import com.qwy.mapper.UsersMapper;
import com.qwy.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author:齐
* @Package:com.qwy.service.impl
* @Project:springcloud1
* @name:UsersServiceImpl
* @Date:2022/12/11 21:50
* @Filename:UsersServiceImpl
*/
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersMapper usersMapper;
@Override
public boolean save(Users users) {
int insert = usersMapper.insert(users);
return insert>0?true:false;
}
@Override
public Users findById(Long id) {
return usersMapper.selectById(id);
}
}
c.业务控制器
package com.qwy.controller;
import com.qwy.bean.Code;
import com.qwy.bean.CommonResult;
import com.qwy.bean.Users;
import com.qwy.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
/**
* @Author:齐
* @Package:com.qwy.controller
* @Project:springcloud1
* @name:UsersController
* @Date:2022/12/11 21:51
* @Filename:UsersController
*/
@RestController
@RequestMapping("/users")
public class UsersController {
@Autowired
private UsersService usersService;
@Value("${server.port}")
private String port;
@PostMapping
public CommonResult save(@RequestBody Users users){
boolean save = usersService.save(users);
return new CommonResult(save? Code.SAVE_OK:Code.SAVE_ERR,save);
}
@GetMapping("/{id}")
public CommonResult findById(@PathVariable("id") Long id){
System.out.println("port:"+port);
Users users = usersService.findById(id);
Integer code = users != null ? Code.SELECT_OK : Code.SELECT_ERR;
String message = users != null ? "" : "查询失败,请稍后查询";
return new CommonResult(code,users,message);
}
}
(四)服务提供者ConsulClientProvider8002
此模块参照服务提供者ConsulClientProvider8001
(五)服务消费者ConsulClientConsumer80
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>springcloud</artifactId>
<groupId>com.qwy</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>NacosClientConsumer80</artifactId>
<dependencies>
<!--nacos-client-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- 引入自己定义的api通用包,可以使用Payment支付Entity -->
<dependency>
<groupId>com.qwy</groupId>
<artifactId>Common-API</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>2.0.20</version>
</dependency>
<!--注意新版必须加入负载均衡-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
</dependencies>
</project>
注意这里为了支持负载均衡,必须加入负载均衡的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
springcoud已经彻底放弃了ribbon,有了新的负载均衡的支持。
2.application.yml
server:
port: 80
spring:
application:
name: nacos-client-consumer
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: org.gjt.mm.mysql.Driver
url: jdbc:mysql://localhost:3306/mydb2
username: root
password: admin
cloud:
nacos:
server-addr: localhost:8848
# discovery:
# username: nacos-client-provider
3.主启动类
package com.qwy;
import com.qwy.config.ApplicationConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* @Author:齐
* @Package:com.qwy
* @Project:springcloud1
* @name:NacosClientConsumer80
* @Date:2022/12/13 21:54
* @Filename:NacosClientConsumer80
*/
@SpringBootApplication
@EnableDiscoveryClient
public class NacosClientConsumer80 {
public static void main(String[] args) {
SpringApplication.run(NacosClientConsumer80.class,args);
}
}
4. ApplicationConfig.java
此类为了配置Spring的配置类
package com.qwy.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.web.client.RestTemplate;
/**
* @Author:齐
* @Package:com.qwy.config
* @Project:springcloud1
* @name:ApplicationConfig
* @Date:2022/12/11 4:04
* @Filename:ApplicationConfig
*/
@Configuration
public class ApplicationConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate(){
return new RestTemplate();
}
}
5. 持久层、服务层、控制层
a.持久层
package com.qwy.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qwy.bean.Orders;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author:齐
* @Package:com.qwy.mapper
* @Project:springcloud1
* @name:OrderMapper
* @Date:2022/12/11 3:51
* @Filename:OrderMapper
*/
@Mapper
public interface OrderMapper extends BaseMapper<Orders> {
}
b.服务接口
package com.qwy.service;
import com.qwy.bean.Orders;
import org.springframework.transaction.annotation.Transactional;
/**
* @Author:齐
* @Package:com.qwy.service
* @Project:springcloud1
* @name:OrdersService
* @Date:2022/12/11 4:00
* @Filename:OrdersService
*/
@Transactional
public interface OrdersService {
boolean save(Orders orders);
Orders findById(Long id);
}
c.服务实现类
package com.qwy.service.impl;
import com.qwy.bean.Orders;
import com.qwy.mapper.OrderMapper;
import com.qwy.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Author:齐
* @Package:com.qwy.service.impl
* @Project:springcloud1
* @name:OrdersServiceImpl
* @Date:2022/12/11 4:02
* @Filename:OrdersServiceImpl
*/
@Service
public class OrdersServiceImpl implements OrdersService {
@Autowired
private OrderMapper orderMapper;
@Override
public boolean save(Orders orders) {
int insert = orderMapper.insert(orders);
return insert>0?true:false;
}
@Override
public Orders findById(Long id) {
Orders orders = orderMapper.selectById(id);
return orders;
}
}
d.控制器
package com.qwy.controller;
import com.alibaba.fastjson2.JSONObject;
import com.qwy.bean.*;
import com.qwy.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
/**
* @Author:齐
* @Package:com.qwy.controller
* @Project:springcloud1
* @name:OrderController
* @Date:2022/12/11 4:15
* @Filename:OrderController
*/
@RestController
@RequestMapping("/orders")
public class OrderController {
private final String CLIENT_PROVIDER_URL="http://nacos-client-provider";
@Autowired
private OrdersService ordersService;
@Autowired
private RestTemplate restTemplate;
@PostMapping
public CommonResult save(@RequestBody Orders orders){
boolean save = ordersService.save(orders);
return new CommonResult(save? Code.SAVE_OK:Code.SAVE_ERR,save);
}
@GetMapping("/{id}")
public CommonResult findOrderAndUserById(@PathVariable("id") Long id){
Orders orders = ordersService.findById(id);
Long usersId = orders.getUsersId();
CommonResult commonResult = restTemplate.getForObject(CLIENT_PROVIDER_URL+"/users/"+usersId, CommonResult.class, usersId);
String josn = JSONObject.toJSONString(commonResult.getData());
Users users = JSONObject.parseObject(josn, Users.class);
ExtendsOrders extendsOrders= new ExtendsOrders();
extendsOrders.setUsers(users);
extendsOrders.setCreateTime(orders.getCreateTime());
extendsOrders.setId(orders.getId());
extendsOrders.setName(orders.getName());
Integer code = orders != null ? Code.SELECT_OK : Code.SELECT_ERR;
String message = orders != null ? "" : "查询失败";
return new CommonResult(code,extendsOrders,message);
}
}
(六)图示说明
1.启动nacos(因为nacos默认需要集群,这里使用单节点模式)
startup.cmd -m standalone
2.启动服务提供者和消费者
NacosClientProvider8001,
NacosClientProvider8002,
NacosClientConsumer80
3.浏览器输入
http://localhost:8848/nacos
出现如图所示:
4.查看对应关系
a.服务提供者与Nacos
b.服务消费者与Nacos
c.服务消费者调用在Nacos中注册的服务提供者的名称
结语:持久不断的学习,才能注入新的血液。写此文章主要为了学习技术的更新的部分。另Nacos的功能不仅仅是服务的注册与发现。因为篇幅问题,这里不再探讨其他的功能,后期补充。最后创作不易,希望对大家有帮助,大家浏览之余留下个小小的赞,本人将不胜感激。