当前位置:网站首页>ss-4.1-1个eurekaServer+1个providerPayment+1个consumerOrder
ss-4.1-1个eurekaServer+1个providerPayment+1个consumerOrder
2022-08-03 05:09:00 【lhorse003】
一个eureka + 一个生产者 +1个消费者案例
eurekaServer
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ EurekaMain7001.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/EurekaMain7001.java:13:1
│ │ │
│ │ └─resources
│ │ application.yml
│ │
│ └─test
│ └─java
pom.xml
<dependencies>
<!--eureka-server-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>
EurekaMain7001.java
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
application.yml
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务端的实例名称
client:
register-with-eureka: false #false表示不向注册中心注册自己
fetch-registry: false #false表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
cloud-provider-payment8001
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─springCloud
│ │ │ │ PaymentMain8001.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/springCloud/PaymentMain8001.java:13:1
│ │ │ │
│ │ │ ├─controller
│ │ │ │ PaymentController.java
│ │ │ │
│ │ │ ├─dao
│ │ │ │ PaymentDao.java
│ │ │ │
│ │ │ └─service
│ │ │ │ PaymentService.java
│ │ │ │
│ │ │ └─impl
│ │ │ PaymentServiceImpl.java
│ │ │
│ │ └─resources
│ │ │ application.yml
│ │ │
│ │ └─mapper
│ │ PaymentMapper.xml
│ │
│ └─test
│ └─java
pom.xml
<dependencies>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.cmk.springcoloud</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<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>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
PaymentMain8001.java
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
PaymentController.java
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@PostMapping("/payment/create")
public CommonResult create(@RequestBody Payment payment){
int result = paymentService.create(payment);
log.info("****插入结果"+result);
if (result>0){
return new CommonResult(200,"插入数据库成功",result);
}else{
return new CommonResult(400,"插入数据库失败",null);
}
}
@GetMapping("/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") long id){
Payment result = paymentService.getPaymentById(id);
log.info("****查询结果"+result+"456");
if (result !=null){
return new CommonResult(200,"查询数据库成功",result);
}else{
return new CommonResult(400,"查询数据库失败",null);
}
}
}
PaymentDao.java
@Mapper
public interface PaymentDao {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
service
PaymentService.java
@Mapper
public interface PaymentService {
int create(Payment payment);
Payment getPaymentById(@Param("id") long id);
}
impl > PaymentServiceImpl.java
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
public int create(Payment payment){
return paymentDao.create(payment);
}
public Payment getPaymentById(@Param("id") long id){
return paymentDao.getPaymentById(id);
}
}
resources
application.yml
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.cmk.springCloud.entities
eureka:
client:
#表示是否将自己注册进 EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
mapper > PaymentMapper.xml
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.cmk.springCloud.service.PaymentService">
<insert id="create" parameterType="com.cmk.springCloud.entities.Payment">
insert into payment(serial) values (#{serial});
</insert>
<select id="getPaymentById" parameterType="long" resultMap="BaseResultMap">
select * from payment where id=#{id}
</select>
<resultMap id="BaseResultMap" type="com.cmk.springCloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
</mapper>
com-consumer-order80
│ pom.xml
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─cmk
│ │ │ └─consumerOrder80
│ │ │ │ OrderMain80.java //jetbrains://idea/navigate/reference?project=zy2020&path=com/cmk/consumerOrder80/OrderMain80.java:13:1
│ │ │ │
│ │ │ ├─config
│ │ │ │ ApplicationContextConfig.java
│ │ │ │
│ │ │ └─controller
│ │ │ OrderController.java
│ │ │
│ │ └─resources
│ │ application.yml
│ │
│ └─test
│ └─java
OrderMain80.java
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
ApplicationContextConfig.java
@Configuration
public class ApplicationContextConfig {
@Bean
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
OrderController.java
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
@Resource
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult<Payment> create(Payment payment){
System.out.println(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);
}
}
application.yml
server:
port: 80
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
边栏推荐
猜你喜欢
随机推荐
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
Interface testing framework combat (3) | JSON request and response assertion
在树莓派上搭建属于自己的网页(2)
【Harmony OS】【FAQ】鸿蒙问题合集1
Interface Test Framework Practice (4) | Get Schema Assertion
Peptides mediated PEG DSPE of phospholipids, targeted functional materials - PEG - RGD/TAT/NGR/APRPG
接口管理工具YApi怎么用?颜值高、易管理、超好用
阿里云对象存储oss私有桶生成链接
接口测试框架实战(四)| 搞定 Schema 断言
Concepts and Methods of Exploratory Testing
MOSN 反向通道详解
Flink state
Shell条件语句判断
获取Ip工具类
User password verification
Exception(异常) 和 Error(错误)区别解析
js中的闭包
How to use the interface management tool YApi?Beautiful, easy to manage, super easy to use
shell脚本循环语句
BIOTIN ALKYNE CAS: 773888-45-2 Price, Supplier