idea搭建maven程序:03-80端口子程序的代码部分(RestTempl)
作者:互联网
目录
一、说明
1.1 80端口
一般用于程序入口,不进行操作,只相当于前台,通过RestTempl跳转到各个子程序
1.2 RestTemplate
是从Spring3.0开始支持的一个HTTP请求工具,它提供了常见的REST请求方案的模版,例如GET请求、POST请求.PUT请求、DELETE请求以及一些通用的请求执行方法exchange 以及execute。
1.3 负载均衡(SOA)
多人访问,达到访问上限,进行等待或分流跨域(RestTemplate)
实现不同工程之间的数据传递
二、80端口子程序的代码部分
总体结构:
2.1 新建
右键总项->new->module->命名cloud-demo-users80
2.2 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>cc_maven_demo</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- 80端口项目的pom-->
<artifactId>cloud_demo-users80</artifactId>
<dependencies>
<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.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>
2.3 application.yml
server:
port: 80
2.4 程序入口 UsersMain80.class
package com.users;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//启动项
@SpringBootApplication
public class UsersMain80 {
public static void main(String[] args) {
SpringApplication.run(UsersMain80.class,args);
}
}
2.5 entities包和实体类
package com.users.entities;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@Data//getset
@AllArgsConstructor//全参构造
@NoArgsConstructor//无参构造
public class TabUsers implements Serializable {//框架要求继承的
private String id;
private String loginname;
private String password;
private String username;
}
2.6 config包和对应类
package com.users.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration//config配置的注解
public class ApplicationContextConfig {
//能够提供多种便捷访问远程Http服务的方法
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
2.7 controller包和对应类
package com.users.controller;
import com.users.entities.TabUsers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@Slf4j
public class UsersController {
public static final String URL = "http://localhost:8001";
@Resource//依赖注入,跨域调用
private RestTemplate rest;
@GetMapping(value="users/tab_users_findall")//调用
public String tab_users_findall(){
//getForObject方法有三个参数,url、传入参数类对象、返回参数类对象
return rest.getForObject(URL+"/tab_users_findall",String.class, TabUsers.class);//String.class仅占位,传什么参,没传所以占位
}
}
三、运行
http://localhost/users/tab_users_findall
运行80端口需要跳转时另一个也得运行
标签:03,users,boot,idea,springframework,class,org,import,子程序 来源: https://blog.csdn.net/weixin_44641804/article/details/121565388