- 个人主页: 程序员爱摸鱼
- 版权: 本文由【程序员爱摸鱼】原创、在CSDN首发、需要转载请联系博主
- 如果文章对你有帮助、欢迎关注+点赞+收藏(一键三连)哦
- 想寻找共同成长的小伙伴,可以互粉哦
前言:
问题:“商品管理系统”需要使用“用户管理系统”某个功能,怎么办?
如图:"商品管理系统"调用"用户管理系统"的过程就是"远程调用"。此时"商品管理系统"相当于模拟"浏览器"
那么我们该怎么选择呢?
==微服务==,基于Spring Cloud的微服务,更加强调的是独立、自治、灵活,一般都会采用基于Http
的Rest风格服务。
首先我们先来创建一个项目来担当父项目
项目名:nacos-parent6_14
添加坐标:
org.springframework.cloud
spring-cloud-build
2.3.5.RELEASE
Hoxton.SR12
2.2.7-SNAPSHOT
3.4.0
1.1.10
2.7.0
0.9.0
2.9.7
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
2.2.7.RELEASE
pom
com.baomidou
mybatis-plus-boot-starter
${mybatis.plus.starter.version}
com.alibaba
druid-spring-boot-starter
${durid.starter.version}
io.springfox
springfox-swagger2
${swagger.version}
io.springfox
springfox-swagger-ui
${swagger.version}
接下来我们创建父项目里面的子项目
项目名:nacos-user
添加依赖
org.springframework.boot
spring-boot-starter-web
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
org.projectlombok
lombok
com.baomidou
mybatis-plus-boot-starter
3.4.0
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-test
com.czxy.
test-mybatis-plus
1.0-SNAPSHOT
test
如图:拷贝BaseResult 和 Swagger2ConfigurationV3
server:
port: 7778
@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "user", keepGlobalPrefix = true)
public class User {
@TableId
private Integer uid;
private String username;
private String password;
private Integer age;
}
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserService userService;
@PostMapping
public BaseResult login(@RequestBody User user){
System.out.println("进入成功");
User login = userService.login(user);
if(login !=null){
return BaseResult.ok("登陆成功",login);
}else {
return BaseResult.error("登陆失败");
}
}
}
@Service
@Transactional
public class UserServiceimpl extends ServiceImpl implements UserService{
@Override
public User login(User user) {
QueryWrapper querywrapper = new QueryWrapper<>();
querywrapper.eq("username",user.getUsername());
querywrapper.eq("password",user.getPassword());
User loginuser = baseMapper.selectOne(querywrapper);
return loginuser;
}
}
@Mapper
public interface UserMapper extends BaseMapper {
}
@SpringBootApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class,args);
}
}
http://localhost:7778/swagger-ui.html
项目名:nacosuser-http
org.springframework.boot
spring-boot-starter-web
org.apache.httpcomponents
httpclient
4.5.6
org.projectlombok
lombok
com.alibaba
fastjson
1.2.49
org.springframework.boot
spring-boot-starter-test
com.alibaba
fastjson
1.2.49
org.springframework
spring-tx
5.3.20
compile
org.springframework.boot
spring-boot-starter-test
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
mysql
mysql-connector-java
server:
port: 7770
# http配置
http:
maxTotal: 300
defaultMaxPerRoute: 50
connectTimeout: 1000
connectionRequestTimeout: 500
socketTimeout: 5000
@RestController
@RequestMapping("/userhttp")
public class UserController {
@Resource
private UserService userService;
@PostMapping
public BaseResult jieshoulogin(@RequestBody User user) throws IOException {
String str = userService.login(user);
return BaseResult.ok(str);
}
}
@Service
@Transactional
public class UserServiceimpl implements UserService {
@Resource
private AdminClient adminClient;
@Override
public String login(User user) throws IOException {
String str = adminClient.AdminClientLogin(user);
return str;
}
}
@Component
public class AdminClient {
@Resource
private HttpClient httpClient;
@Test
public String AdminClientLogin(User user) throws IOException {
System.out.println("进入了");
HttpPost httpPost = new HttpPost("http://localhost:7777/user");
httpPost.setEntity(new StringEntity(JSONObject.toJSONString(user)));
httpPost.setHeader("content-type","application/json;charset=UTF-8");
HttpResponse response=httpClient.execute(httpPost);
String str="";
if(response.getStatusLine().getStatusCode() == 200){
str = EntityUtils.toString(response.getEntity());
System.out.println(str);
}
return str;
}
}
如果数据库对应不上将会登陆失败
想要了解更多吗?没时间解释了,快来点一点!!!
程序员爱摸鱼
————————————————
版权声明:本文为CSDN博主「程序员爱摸鱼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:程序员爱摸鱼的博客_CSDN博客-MyBatis Plus,项目研发,归并排序领域博主