<dependencies>
<!-- springboot web starter 用来监听端口-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 如果在子工程/模块指定了 version,则以指定为准 -->
</dependency>
<!--
1. starter-actuator 是 springboot 程序的监控系统,可以实现健康检查,info 信息
等
2. 访问 http://localhost:10000/actuator 可以看到相关链接, 还可以做相关设置. -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web
-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
server:
port: 80 # 监听80端口
spring:
application:
name: member-service-consumer-80
package com.sun.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Description:
*
* @Author sun
* @Create 2024/3/24 13:58
* @Version 1.0
*/
@SpringBootApplication
public class MemberConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(MemberConsumerApplication.class, args);
}
}
server:
port: 81 # 监听81端口
spring:
application:
name: member-service-consumer-81
package com.sun.springcloud.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* Description:
*
* @Author sun
* @Create 2024/3/24 14:21
* @Version 1.0
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
//Serializable 加上,后面可能使用
public class Member implements Serializable {
private Long id;
private String name;
private String pwd;
private String mobile;
private String email;
private Integer gender;
}
package com.sun.springcloud.util;
/**
* Description:
*
* @Author sun
* @Create 2024/3/24 14:21
* @Version 1.0
*/
public class Result<T> {
private String code;
private String msg;
private T data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public Result() {
}
public Result(T data) {
this.data = data;
}
public static Result success() {
Result result = new Result<>();
result.setCode("200");
result.setMsg("success");
return result;
}
public static <T> Result<T> success(T data) {
Result<T> result = new Result<>(data);
result.setCode("200");
result.setMsg("success");
return result;
}
public static <T> Result<T> success(String msg, T data) {
Result<T> result = new Result<>(data);
result.setCode("200");
result.setMsg(msg);
return result;
}
public static Result error(String code, String msg) {
Result result = new Result();
result.setCode(code);
result.setMsg(msg);
return result;
}
public static <T> Result<T> error(String code, String msg, T data) {
Result<T> result = new Result<>(data);
result.setCode(code);
result.setMsg(msg);
return result;
}
}
package com.sun.springcloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* Description: 配置类
*
* @Author sun
* @Create 2024/3/24 14:32
* @Version 1.0
*/
@Configuration
public class CustomizationBean {
/**
* 注入RestTemplate的bean对象
* @return
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.sun.springcloud.controller;
import com.sun.springcloud.entity.Member;
import com.sun.springcloud.util.Result;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
/**
* Description: 作为会员中心微服务对member表操作的网关
*
* @Author sun
* @Create 2024/3/24 14:42
* @Version 1.0
*/
@RestController
public class MemberConsumerController {
/*
访问会员中心微服务的前缀
*/
public static final String MEMBER_SERVICE_PROVIDER_URL = "http://localhost:10001";
/*
注入微服务之间通讯的RestTemplate的bean对象
*/
@Resource
private RestTemplate restTemplate;
/**
* 向会员中心微服务的save接口发送请求,携带member对象,接受返回的结果Result并以json的格式返回给浏览器
*
* @param member 这里的参数必须是表单类型的,因为没有加@requestBody
* @return
*/
@PostMapping("/member/consumer/save")
public Result save(Member member) {
// 注意:使用restTemplate发送请求时会将member转化为json格式的数据然后再发送请求,所以会员中心微服务的save接口必须加@requestBody注解
return restTemplate.postForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/save", member, Result.class);
}
}
/**
* 接受id为路径参数,向会员中心微服务模块的getMemberById接口发送请求,根据id获取信息
*
* @param id 请求参数
* @return 根据id返回json类型的数据
*/
@GetMapping("/member/consumer/get/{id}") // 这里使用的路径参数
public Result getMemberById(@PathVariable("id") Long id) {
return restTemplate.getForObject(MEMBER_SERVICE_PROVIDER_URL + "/member/get/" + id, Result.class);
}
<component name="RunDashboard">
<option name="configurationTypes">
<set>
<option value="SpringBootApplicationConfigurationType" />
</set>
</option>
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<dependencies>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<!-- optional为true表示两个模块之间依赖不传递,也就是其他模块引入这个共用模块时,不传递这个模块的lombok依赖 -->
<optional>true</optional>
</dependency>
</dependencies>
package
命令主要用于项目的编译和打包,但不会将产物安装到本地仓库,主要用于构建过程的测试和验证。install
命令在执行package
的基础上,进一步将打包后的文件安装到本地Maven仓库,便于其他项目的依赖引用。 <!-- 公共模块的jar包 -->
<dependency>
<groupId>org.example</groupId>
<artifactId>e_commerce_center-common-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
更多【云原生-服务消费微服务】相关视频教程:www.yxfzedu.com