目录
一、建module
二、修改pom.xml文件
三、编写yml文件
四、编写主启动类
五、编写业务代码
六、启动测试
一、建module
创建一个普通maven模块
二、修改pom.xml文件
cloud
com.shang.cloud
1.0-SNAPSHOT
4.0.0
cloud-consumer-order80
org.springframework.cloud
spring-cloud-starter-zipkin
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
三、编写yml文件
server:
port: 90
其实这里使用80端口会更好,因为http默认访问80端口,这样消费者就不用在访问的时候加上端口号了。但是我的80端口被Nginx占用,来回禁用也挺麻烦,于是就改成了90端口。
四、编写主启动类
package com.shang.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
五、编写业务代码
由于消费者只需要调用支付模块的方法,因此只需要entity和controller即可。
entity直接复用支付模块的就行了。
package com.shang.cloud.controller;
import com.shang.cloud.entities.CommonResult;
import com.shang.cloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult create(Payment payment){
return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment, CommonResult.class);
}
@GetMapping("/consumer/payment/get/{id}")
public CommonResult getPayment(@PathVariable("id") Long id){
return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id, CommonResult.class);
}
}
注解配置applicationContext
package com.shang.cloud.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
六、启动测试
可以看到,通过消费者订单模块,我们也查到了内容。
数据也能成功插入



