提交 7b37a3a3 编写于 作者: Lcry's avatar Lcry 🖥

P16-P30:完成服务注册到Eureka和Zk代码

上级 9dd0f007
......@@ -104,4 +104,12 @@ cloud-consumer-order80/cloud-consumer-order80.iml
cloud-consumer-order80/src/test/
cloud-api-commons/cloud-api-commons.iml
cloud-api-commons/target/
cloud-api-commons/src/test/
\ No newline at end of file
cloud-api-commons/src/test/
cloud-eureka-server7001/src/test/
cloud-eureka-server7001/target/
cloud-eureka-server7002/src/test/
cloud-eureka-server7002/target/
cloud-provider-payment8002/src/test/
cloud-provider-payment8002/target/
cloud-consumer-zk-order80/target/
cloud-provider-zk-payment8004/target/
......@@ -13,6 +13,11 @@
<artifactId>cloud-consumer-order80</artifactId>
<dependencies>
<!--引入eureka客户端eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.lcry</groupId>
......
......@@ -2,6 +2,8 @@ package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* OrderMain80
......@@ -10,8 +12,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @date 2020/03/14 11:59
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class, args);
}
}
}
\ No newline at end of file
package com.lcry.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
......@@ -14,6 +15,7 @@ import org.springframework.web.client.RestTemplate;
@Configuration
public class ApplicationContextConfig {
@Bean //相当于Spring中applicationContext.xml中<bean id="" class="">
@LoadBalanced //使用此注解赋予RestTemplate负载均衡的能力
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
......
......@@ -19,7 +19,9 @@ import javax.annotation.Resource;
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://localhost:8001";
// public static final String PAYMENT_URL = "http://localhost:8001";
//集群应用通过服务名调用,注意配置需要加上@LoadBalanced注解启用负载均衡
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Resource
private RestTemplate restTemplate;
......
......@@ -3,4 +3,14 @@ server:
spring:
application:
name: cloud-order-service
\ No newline at end of file
name: cloud-order-service
eureka:
client:
#表示是否将自己注册进EurekaServer,默认为true
register-with-eureka: true
#是否从EurekaServer中抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://localhost:7001/eureka #单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
\ No newline at end of file
<?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>2020SpringCloud</artifactId>
<groupId>com.lcry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- zk版订单服务-->
<artifactId>cloud-consumer-zk-order80</artifactId>
<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!--先排除自带的zookeeper-->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--添加zookeeper3.4.14版本-->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</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>
\ No newline at end of file
package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* ZkOrderMain80
*
* @author lcry
* @date 2020/03/14 16:19
* ZK版本消费方:只是测试,不写任何具体业务
*/
@SpringBootApplication
@EnableDiscoveryClient
public class ZkOrderMain80 {
public static void main(String[] args) {
SpringApplication.run(ZkOrderMain80.class, args);
}
}
package com.lcry.springcloud.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
/**
* ApplicationContextConfig
*
* @author lcry
* @date 2020/03/14 16:21
* 配置类
*/
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
package com.lcry.springcloud.controller;
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;
/**
* OrderZkController
*
* @author lcry
* @date 2020/03/14 16:21
*/
@RestController
@Slf4j
public class OrderController {
public static final String INVOKE_URL = "http://cloud-provider-payment";
@Resource
private RestTemplate restTemplate;
@GetMapping(value = "/consumer/payment/zk")
public String paymentInfo() {
String result = restTemplate.getForObject(INVOKE_URL + "/payment/zk", String.class);
return result;
}
}
#80表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 80
#服务别名——注册到zookeeper注册中心的名称
spring:
application:
name: cloud-consumer-order
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
\ No newline at end of file
<?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>2020SpringCloud</artifactId>
<groupId>com.lcry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- eureka服务端1-->
<artifactId>cloud-eureka-server7001</artifactId>
<!--eureka-server-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--boot web actuator-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* EurekaMain7001
*
* @author lcry
* @date 2020/03/14 13:02
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #Eureka服务端的实例名称
client:
#false表示不向注册中心中注册自己
register-with-eureka: false
#false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server之间交互的地址,查询服务和注册服务都需要依赖这个地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版,指向自己
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
server:
#关闭Eureka自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
\ No newline at end of file
<?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>2020SpringCloud</artifactId>
<groupId>com.lcry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- eureka服务端2-->
<artifactId>cloud-eureka-server7002</artifactId>
<!--eureka-server-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--boot web actuator-->
<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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* EurekaMain7002
*
* @author lcry
* @date 2020/03/14 13:42
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7002 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7002.class, args);
}
}
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #Eureka服务端的实例名称
client:
#false表示不向注册中心中注册自己
register-with-eureka: false
#false表示自己就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka Server之间交互的地址,查询服务和注册服务都需要依赖这个地址
#defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #单机版,指向自己
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
server:
#关闭Eureka自我保护机制,保证不可用服务被及时剔除
enable-self-preservation: false
eviction-interval-timer-in-ms: 2000
\ No newline at end of file
......@@ -13,6 +13,11 @@
<artifactId>cloud-provider-payment8001</artifactId>
<dependencies>
<!--引入eureka客户端eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.lcry</groupId>
......
......@@ -2,6 +2,8 @@ package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* PaymentMain8001
......@@ -10,6 +12,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
* @date 2020/03/13 20:54
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //Discovery服务发现
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
......
......@@ -4,9 +4,12 @@ import com.lcry.springcloud.entities.CommonResult;
import com.lcry.springcloud.entities.Payment;
import com.lcry.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* PaymentController
......@@ -21,6 +24,9 @@ public class PaymentController {
@Resource
private PaymentService paymentService;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping(value = "/payment/create")
public CommonResult create(@RequestBody Payment payment) {
int result = paymentService.create(payment);
......@@ -44,4 +50,22 @@ public class PaymentController {
return new CommonResult(444, "没有对应记录,查询ID:" + id, null);
}
}
@GetMapping(value = "/payment/discovery")
public Object discovery() {
//查询Eureka上面注册了几个服务
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("service:" + service);
}
//查询对应名字下面有几个示例
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId() + "\t" + instance.getHost()
+ "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
}
......@@ -11,6 +11,23 @@ spring:
username: root
password: lcry
eureka:
client:
#表示是否将自己注册进EurekaServer,默认为true
register-with-eureka: true
#是否从EurekaServer中抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://localhost:7001/eureka #单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8001 #修改Status名称
prefer-ip-address: true
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认30)
lease-renewal-interval-in-seconds: 10
#Eureka服务端在收到最后一次心跳后等待的时间上限,单位为秒(默认90),超时剔除服务
lease-expiration-duration-in-seconds: 60
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.lcry.springcloud.entities # 所有Entity别名类所在包
\ No newline at end of file
<?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>2020SpringCloud</artifactId>
<groupId>com.lcry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- 支付模块8001的克隆版-->
<artifactId>cloud-provider-payment8002</artifactId>
<dependencies>
<!--引入eureka客户端eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--引入自己定义的api调用包,可以使用Payment支付Entity-->
<dependency>
<groupId>com.lcry</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.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--jdbc-->
<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.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>
\ No newline at end of file
package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
/**
* PaymentMain8001
*
* @author lcry
* @date 2020/03/13 20:54
*/
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient //Discovery服务发现
public class PaymentMain8002 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8002.class, args);
}
}
package com.lcry.springcloud.controller;
import com.lcry.springcloud.entities.CommonResult;
import com.lcry.springcloud.entities.Payment;
import com.lcry.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
/**
* PaymentController
*
* @author lcry
* @date 2020/03/13 21:39
* 控制器
*/
@RestController
@Slf4j
public class PaymentController {
@Resource
private PaymentService paymentService;
@Resource
private DiscoveryClient discoveryClient;
@PostMapping(value = "/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(444, "插入数据失败", null);
}
}
@GetMapping(value = "/payment/get/{id}")
public CommonResult getPaymentById(@PathVariable("id") Long id) {
Payment payment = paymentService.getPaymentById(id);
log.info("查询结果:" + payment);
if (payment != null) {
return new CommonResult(200, "查询成功", payment);
} else {
return new CommonResult(444, "没有对应记录,查询ID:" + id, null);
}
}
@GetMapping(value = "/payment/discovery")
public Object discovery() {
//查询Eureka上面注册了几个服务
List<String> services = discoveryClient.getServices();
for (String service : services) {
log.info("service:" + service);
}
//查询对应名字下面有几个示例
List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
for (ServiceInstance instance : instances) {
log.info(instance.getServiceId() + "\t" + instance.getHost()
+ "\t" + instance.getPort() + "\t" + instance.getUri());
}
return this.discoveryClient;
}
}
package com.lcry.springcloud.dao;
import com.lcry.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* PaymentDao
*
* @author lcry
* @date 2020/03/13 21:30
* Dao层
*/
@Mapper
public interface PaymentDao {
int create(Payment payment); //增
Payment getPaymentById(@Param("id") Long id); //根据id查询
}
package com.lcry.springcloud.service;
import com.lcry.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Param;
/**
* PaymentService
*
* @author lcry
* @date 2020/03/13 21:28
*/
public interface PaymentService {
int create(Payment payment); //增
Payment getPaymentById(@Param("id") Long id); //根据id查询
}
package com.lcry.springcloud.service.impl;
import com.lcry.springcloud.dao.PaymentDao;
import com.lcry.springcloud.entities.Payment;
import com.lcry.springcloud.service.PaymentService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* PaymentServiceImpl
*
* @author lcry
* @date 2020/03/13 21:29
* service实现类
*/
@Service
public class PaymentServiceImpl implements PaymentService {
@Resource
private PaymentDao paymentDao;
@Override
public int create(Payment payment) {
return paymentDao.create(payment);
}
@Override
public Payment getPaymentById(Long id) {
return paymentDao.getPaymentById(id);
}
}
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2020?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: lcry
eureka:
client:
#表示是否将自己注册进EurekaServer,默认为true
register-with-eureka: true
#是否从EurekaServer中抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
#defaultZone: http://localhost:7001/eureka #单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
instance:
instance-id: payment8002 #修改Status名称
prefer-ip-address: true
#Eureka客户端向服务端发送心跳的时间间隔,单位为秒(默认30)
lease-renewal-interval-in-seconds: 10
#Eureka服务端在收到最后一次心跳后等待的时间上限,单位为秒(默认90),超时剔除服务
lease-expiration-duration-in-seconds: 60
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.lcry.springcloud.entities # 所有Entity别名类所在包
\ No newline at end of file
/*
Navicat Premium Data Transfer
Source Server : 本地mysql
Source Server Type : MySQL
Source Server Version : 50717
Source Host : localhost:3306
Source Schema : db2020
Target Server Type : MySQL
Target Server Version : 50717
File Encoding : 65001
Date: 13/03/2020 22:40:33
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for payment
-- ----------------------------
DROP TABLE IF EXISTS `payment`;
CREATE TABLE `payment` (
`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`serial` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 25 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES (1, 'aaa');
INSERT INTO `payment` VALUES (22, 'bbbb');
INSERT INTO `payment` VALUES (24, '123');
SET FOREIGN_KEY_CHECKS = 1;
<?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.lcry.springcloud.dao.PaymentDao">
<insert id="create" parameterType="Payment" useGeneratedKeys="true" keyProperty="id">
insert into payment(serial) values(#{serial});
</insert>
<resultMap id="BaseResultMap" type="com.lcry.springcloud.entities.Payment">
<id column="id" property="id" jdbcType="BIGINT"/>
<id column="serial" property="serial" jdbcType="VARCHAR"/>
</resultMap>
<select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
select * from payment where id=#{id};
</select>
</mapper>
<?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>2020SpringCloud</artifactId>
<groupId>com.lcry</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- zk版支付服务-->
<artifactId>cloud-provider-zk-payment8004</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>
<!-- SpringBoot整合zookeeper客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
<!-- 先排除自带的zookeeper -->
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 添加本机zookeeper3.4.14版本 -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.14</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package com.lcry.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
/**
* ZkPaymentMain8004
*
* @author lcry
* @date 2020/03/14 16:08
* ZK版本服务方:只是测试,不写任何具体业务
*/
@SpringBootApplication
@EnableDiscoveryClient //该注解用于向使用consul或者zookeeper作为注册中心时注册服务
public class ZkPaymentMain8004 {
public static void main(String[] args) {
SpringApplication.run(ZkPaymentMain8004.class, args);
}
}
package com.lcry.springcloud.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
/**
* PaymentController
*
* @author lcry
* @date 2020/03/14 16:11
*/
@RestController
@Slf4j
public class PaymentController {
@Value("${server.port}")
private String serverPort;
/*
测试注册服务到zk
*/
@RequestMapping(value = "/payment/zk")
public String paymentzk() {
return "SpringCloud with zookeeper:" + serverPort + "\t" +
UUID.randomUUID().toString();
}
}
#8004表示注册到zookeeper服务器的支付服务提供者端口号
server:
port: 8004
#服务别名——注册到zookeeper注册中心的名称
spring:
application:
name: cloud-provider-payment
cloud:
zookeeper:
connect-string: 127.0.0.1:2181
\ No newline at end of file
......@@ -9,8 +9,13 @@
<version>1.0-SNAPSHOT</version>
<modules>
<module>cloud-provider-payment8001</module>
<module>cloud-provider-payment8002</module>
<module>cloud-consumer-order80</module>
<module>cloud-api-commons</module>
<module>cloud-eureka-server7001</module>
<module>cloud-eureka-server7002</module>
<module>cloud-provider-zk-payment8004</module>
<module>cloud-consumer-zk-order80</module>
</modules>
<packaging>pom</packaging>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册