目录
1. SpringCloud
2. Nacos
3. 远程通信
3.1 创建公共子模块 (nacos_commons)
3.1.1 DTO对象
3.2 父项目引入子项目 (nacos_commons) 打成的jar包
3.3 父项目指向子项目 (nacos_commons) 为儿子
3.4 子项目 (nacos_provider)
3.5 子项目 (nacos_consumer)
3.6 启动项目测试进行远程调用服务
就是在互不相干的模块里调用其他模块的接口
本章内容父项目pom文件结构
4.0.0
com.jmh
springcloud01
1.0-SNAPSHOT
pom
nacos_provider
nacos_consumer
nacos_commons
springcloud01 Maven Webapp
http://www.example.com
UTF-8
1.7
1.7
2.4.1
2020.0.0
2021.1
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-starter-web
com.alibaba.cloud
spring-cloud-starter-alibaba-nacos-discovery
org.springframework.cloud
spring-cloud-starter-openfeign
org.springframework.cloud
spring-cloud-loadbalancer
ma.glasnost.orika
orika-core
1.4.6
org.projectlombok
lombok
com.jmh
nacos_commons
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-dependencies
${spring-boot.version}
pom
import
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
com.alibaba.cloud
spring-cloud-alibaba-dependencies
${spring-cloud-alibaba.version}
pom
import
消费者 远程调用 生产者 : 需要网络传输,使用DTO同一封装对象
原理与SpringBoot启动类相同
1.将DTO对象封装到公共DTO模块
2.为需要的项目引入公共DTO模块
注意点
1.不需要继承父模块(重复引用问题)
2.打包方式为jar
3.不需要添加启动类的编译
package com.jmh.nacos_commons.dto;
import lombok.Data;
/**
* @author 蒋明辉
* @data 2022/11/4 17:28
*/
@Data
public class UserDto {
//账号
private String names;
//密码
private String pwd;
//秘钥
private String token;
}
com.jmh
nacos_commons
0.0.1-SNAPSHOT
nacos_commons
Orika是java Bean映射框架,可以实现从一个对象递归拷贝数据至另一个对象。
package com.jmh.nacos_provider.pojo;
import lombok.Data;
/**
* @author 蒋明辉
* @data 2022/11/4 17:28
*/
@Data
public class User {
//账号
private String name;
//密码
private String pwd;
}
package com.jmh.nacos_provider;
import ma.glasnost.orika.MapperFactory;
import ma.glasnost.orika.impl.DefaultMapperFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
@SpringBootApplication
@EnableDiscoveryClient
@SuppressWarnings("all")
public class NacosProviderApplication {
public static void main(String[] args) {
SpringApplication.run(NacosProviderApplication.class, args);
}
@Bean
/*单例模式:原型链*/
@Scope("prototype")
public MapperFactory mapperFactory() {
return new DefaultMapperFactory.Builder().build();
}
}
package com.jmh.nacos_provider.controller;
import com.jmh.nacos_commons.dto.UserDto;
import com.jmh.nacos_provider.pojo.User;
import ma.glasnost.orika.MapperFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* @author 蒋明辉
* @data 2022/11/4 17:29
*/
@RestController
@RequestMapping("/user")
@SuppressWarnings("all")
public class UserController {
@Autowired
private MapperFactory factory;
@RequestMapping("/{name}")
public String getByPath(@PathVariable("name") String name){
System.out.println("name=" + name);
return "我是生产者: yes";
}
@RequestMapping("/param")
public String getByParam(@RequestParam("name") String name,@RequestParam("pwd") String pwd){
System.out.println("name=" + name);
System.out.println("pwd=" + pwd);
return "我是生产者: yes";
}
@RequestMapping("/pojo")
public String getByPojo(@RequestBody UserDto userDto){
//属性值一样
//User u = factory.getMapperFacade().map(userDto, User.class);
//属性值那不一样
factory.classMap(UserDto.class, User.class)
.field("names", "name")
.byDefault().register();
User u = factory.getMapperFacade().map(userDto, User.class);
System.out.println("user=" + u);
return "我是生产者: yes";
}
@RequestMapping("/map")
public String getByMap(@RequestBody Map map){
System.out.println("map=" + map);
return "我是生产者: yes";
}
}
现在为什么来了 现在我在互不相干的模块中怎么去调用呢?用什么方法去调用呢? 生产者提供接口:可以使用以下几个注解接收远程调用的参数值:
- @PathVariable
- @RequestParam
- @RequestBody
- @RestController
package com.jmh.nacos_consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@SuppressWarnings("all")
public class NacosConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(NacosConsumerApplication.class, args);
}
}
package com.jmh.nacos_consumer.service;
import com.jmh.nacos_commons.dto.UserDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* @author 蒋明辉
* @data 2022/11/4 17:29
*/
@FeignClient("nacos-provider")
@SuppressWarnings("all")
public interface IFeignUserService {
@RequestMapping("/user/{name}")
String getByPath(@PathVariable("name") String name);
@RequestMapping("/user/param")
String getByParam(@RequestParam("name") String name,@RequestParam("pwd") String pwd);
@RequestMapping("/user/pojo")
String getByPojo(@RequestBody UserDto user);
@RequestMapping("/user/map")
String getByMap(@RequestBody Map map);
}
package com.jmh.nacos_consumer.controller;
import com.jmh.nacos_commons.dto.UserDto;
import com.jmh.nacos_consumer.service.IFeignUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author 蒋明辉
* @data 2022/11/4 18:27
*/
@RestController
@RequestMapping("/user")
@SuppressWarnings("all")
public class UserController {
@Autowired
private IFeignUserService service;
@RequestMapping("/{name}")
public String test01(@PathVariable("name") String name){
service.getByPath(name);
return "我是消费者: yes";
}
@RequestMapping("/test02")
public String test02(String name,String pwd){
service.getByParam(name,pwd);
return "我是消费者: yes";
}
@RequestMapping("/test03")
public String test03(@RequestBody UserDto user){
service.getByPojo(user);
return "我是消费者: yes";
}
@RequestMapping("/test04")
public String test04(@RequestBody Map map){
service.getByMap(map);
return "我是消费者: yes";
}
@RequestMapping("/test05")
public String test05(String name){
service.getByPath(name);
return "我是消费者: yes";
}
}
1. 测试子项目nacos_consumer test01接口
2. 测试子项目nacos_consumer test02接口
3. 测试子项目nacos_consumer test03接口
4. 测试子项目nacos_consumer test04接口
5. 测试子项目nacos_consumer test04接口