Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Lcry
SpringCloud2020
提交
7b37a3a3
S
SpringCloud2020
项目概览
Lcry
/
SpringCloud2020
通知
20
Star
2
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SpringCloud2020
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
7b37a3a3
编写于
3月 14, 2020
作者:
Lcry
🖥
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
P16-P30:完成服务注册到Eureka和Zk代码
上级
9dd0f007
变更
35
隐藏空白更改
内联
并排
Showing
35 changed file
with
850 addition
and
4 deletion
+850
-4
.gitignore
.gitignore
+9
-1
cloud-consumer-order80/pom.xml
cloud-consumer-order80/pom.xml
+5
-0
cloud-consumer-order80/src/main/java/com/lcry/springcloud/OrderMain80.java
...der80/src/main/java/com/lcry/springcloud/OrderMain80.java
+5
-1
cloud-consumer-order80/src/main/java/com/lcry/springcloud/config/ApplicationContextConfig.java
...com/lcry/springcloud/config/ApplicationContextConfig.java
+2
-0
cloud-consumer-order80/src/main/java/com/lcry/springcloud/controller/OrderController.java
...java/com/lcry/springcloud/controller/OrderController.java
+3
-1
cloud-consumer-order80/src/main/resources/application.yml
cloud-consumer-order80/src/main/resources/application.yml
+11
-1
cloud-consumer-zk-order80/pom.xml
cloud-consumer-zk-order80/pom.xml
+56
-0
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/ZkOrderMain80.java
...r80/src/main/java/com/lcry/springcloud/ZkOrderMain80.java
+20
-0
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/config/ApplicationContextConfig.java
...com/lcry/springcloud/config/ApplicationContextConfig.java
+22
-0
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/controller/OrderController.java
...java/com/lcry/springcloud/controller/OrderController.java
+29
-0
cloud-consumer-zk-order80/src/main/resources/application.yml
cloud-consumer-zk-order80/src/main/resources/application.yml
+11
-0
cloud-eureka-server7001/pom.xml
cloud-eureka-server7001/pom.xml
+51
-0
cloud-eureka-server7001/src/main/java/com/lcry/springcloud/EurekaMain7001.java
...01/src/main/java/com/lcry/springcloud/EurekaMain7001.java
+20
-0
cloud-eureka-server7001/src/main/resources/application.yml
cloud-eureka-server7001/src/main/resources/application.yml
+19
-0
cloud-eureka-server7002/pom.xml
cloud-eureka-server7002/pom.xml
+52
-0
cloud-eureka-server7002/src/main/java/com/lcry/springcloud/EurekaMain7002.java
...02/src/main/java/com/lcry/springcloud/EurekaMain7002.java
+19
-0
cloud-eureka-server7002/src/main/resources/application.yml
cloud-eureka-server7002/src/main/resources/application.yml
+19
-0
cloud-provider-payment8001/pom.xml
cloud-provider-payment8001/pom.xml
+5
-0
cloud-provider-payment8001/src/main/java/com/lcry/springcloud/PaymentMain8001.java
...1/src/main/java/com/lcry/springcloud/PaymentMain8001.java
+4
-0
cloud-provider-payment8001/src/main/java/com/lcry/springcloud/controller/PaymentController.java
...va/com/lcry/springcloud/controller/PaymentController.java
+24
-0
cloud-provider-payment8001/src/main/resources/application.yml
...d-provider-payment8001/src/main/resources/application.yml
+17
-0
cloud-provider-payment8002/pom.xml
cloud-provider-payment8002/pom.xml
+73
-0
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/PaymentMain8002.java
...2/src/main/java/com/lcry/springcloud/PaymentMain8002.java
+21
-0
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/controller/PaymentController.java
...va/com/lcry/springcloud/controller/PaymentController.java
+70
-0
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/dao/PaymentDao.java
...02/src/main/java/com/lcry/springcloud/dao/PaymentDao.java
+19
-0
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/service/PaymentService.java
...ain/java/com/lcry/springcloud/service/PaymentService.java
+16
-0
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/service/impl/PaymentServiceImpl.java
...com/lcry/springcloud/service/impl/PaymentServiceImpl.java
+31
-0
cloud-provider-payment8002/src/main/resources/application.yml
...d-provider-payment8002/src/main/resources/application.yml
+33
-0
cloud-provider-payment8002/src/main/resources/db/payment.sql
cloud-provider-payment8002/src/main/resources/db/payment.sql
+37
-0
cloud-provider-payment8002/src/main/resources/mapper/PaymentMapper.xml
...r-payment8002/src/main/resources/mapper/PaymentMapper.xml
+21
-0
cloud-provider-zk-payment8004/pom.xml
cloud-provider-zk-payment8004/pom.xml
+60
-0
cloud-provider-zk-payment8004/src/main/java/com/lcry/springcloud/ZkPaymentMain8004.java
...src/main/java/com/lcry/springcloud/ZkPaymentMain8004.java
+20
-0
cloud-provider-zk-payment8004/src/main/java/com/lcry/springcloud/controller/PaymentController.java
...va/com/lcry/springcloud/controller/PaymentController.java
+30
-0
cloud-provider-zk-payment8004/src/main/resources/application.yml
...rovider-zk-payment8004/src/main/resources/application.yml
+11
-0
pom.xml
pom.xml
+5
-0
未找到文件。
.gitignore
浏览文件 @
7b37a3a3
...
...
@@ -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/
cloud-consumer-order80/pom.xml
浏览文件 @
7b37a3a3
...
...
@@ -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>
...
...
cloud-consumer-order80/src/main/java/com/lcry/springcloud/OrderMain80.java
浏览文件 @
7b37a3a3
...
...
@@ -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
cloud-consumer-order80/src/main/java/com/lcry/springcloud/config/ApplicationContextConfig.java
浏览文件 @
7b37a3a3
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
();
}
...
...
cloud-consumer-order80/src/main/java/com/lcry/springcloud/controller/OrderController.java
浏览文件 @
7b37a3a3
...
...
@@ -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
;
...
...
cloud-consumer-order80/src/main/resources/application.yml
浏览文件 @
7b37a3a3
...
...
@@ -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
cloud-consumer-zk-order80/pom.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/ZkOrderMain80.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/config/ApplicationContextConfig.java
0 → 100644
浏览文件 @
7b37a3a3
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
();
}
}
cloud-consumer-zk-order80/src/main/java/com/lcry/springcloud/controller/OrderController.java
0 → 100644
浏览文件 @
7b37a3a3
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
;
}
}
cloud-consumer-zk-order80/src/main/resources/application.yml
0 → 100644
浏览文件 @
7b37a3a3
#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
cloud-eureka-server7001/pom.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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
cloud-eureka-server7001/src/main/java/com/lcry/springcloud/EurekaMain7001.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-eureka-server7001/src/main/resources/application.yml
0 → 100644
浏览文件 @
7b37a3a3
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
cloud-eureka-server7002/pom.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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
cloud-eureka-server7002/src/main/java/com/lcry/springcloud/EurekaMain7002.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-eureka-server7002/src/main/resources/application.yml
0 → 100644
浏览文件 @
7b37a3a3
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
cloud-provider-payment8001/pom.xml
浏览文件 @
7b37a3a3
...
...
@@ -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>
...
...
cloud-provider-payment8001/src/main/java/com/lcry/springcloud/PaymentMain8001.java
浏览文件 @
7b37a3a3
...
...
@@ -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
);
...
...
cloud-provider-payment8001/src/main/java/com/lcry/springcloud/controller/PaymentController.java
浏览文件 @
7b37a3a3
...
...
@@ -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
;
}
}
cloud-provider-payment8001/src/main/resources/application.yml
浏览文件 @
7b37a3a3
...
...
@@ -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
cloud-provider-payment8002/pom.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/PaymentMain8002.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/controller/PaymentController.java
0 → 100644
浏览文件 @
7b37a3a3
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
;
}
}
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/dao/PaymentDao.java
0 → 100644
浏览文件 @
7b37a3a3
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查询
}
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/service/PaymentService.java
0 → 100644
浏览文件 @
7b37a3a3
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查询
}
cloud-provider-payment8002/src/main/java/com/lcry/springcloud/service/impl/PaymentServiceImpl.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-provider-payment8002/src/main/resources/application.yml
0 → 100644
浏览文件 @
7b37a3a3
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
cloud-provider-payment8002/src/main/resources/db/payment.sql
0 → 100644
浏览文件 @
7b37a3a3
/*
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
;
cloud-provider-payment8002/src/main/resources/mapper/PaymentMapper.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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>
cloud-provider-zk-payment8004/pom.xml
0 → 100644
浏览文件 @
7b37a3a3
<?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
cloud-provider-zk-payment8004/src/main/java/com/lcry/springcloud/ZkPaymentMain8004.java
0 → 100644
浏览文件 @
7b37a3a3
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
);
}
}
cloud-provider-zk-payment8004/src/main/java/com/lcry/springcloud/controller/PaymentController.java
0 → 100644
浏览文件 @
7b37a3a3
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
();
}
}
cloud-provider-zk-payment8004/src/main/resources/application.yml
0 → 100644
浏览文件 @
7b37a3a3
#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
pom.xml
浏览文件 @
7b37a3a3
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录