org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
cn.bdqn
springcloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
server:
port: 7001
eureka:
instance:
hostname: localhost #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-register: false
server-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@EnableEurekaServer
@SpringBootApplication
public class EurakeServer7001Application {
public static void main(String[] args) {
SpringApplication.run(EurakeSever7001Application.class,args);
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注册
@SpringBootApplication
@EnableEurekaClient
public class PaymentApplication {
public static void main(String[] args) {
SpringApplication.run(PaymentApplication.class,args);
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
server-url:
defaultZone: http://localhost:7001/eureka
instance:
prefer-ip-address: true #使用ip地址注册
@SpringBootApplication
@EnableEurekaClient
public class OrderApplication {
public static void main(String[] args) {
SpringApplication.run(OrderApplication.class,args);
}
}
2.3.4修改主启动类 标注为Eureka客户端
// private static final String PAYMENT_URL="http://localhost:8001";
@Autowired
private RestTemplate restTemplate;
@Autowired
private DiscoveryClient discoveryClient;
//根据id查询
@GetMapping("/consumer/payment/get/{id}")
public ResponseResult queryById(@PathVariable("id") Integer id){
List serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.getForObject("http://"+instance.getHost()+instance.getPort()+"/payment/get/"+id,ResponseResult.class);
// ResponseResult rs =restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,ResponseResult.class);
return rs;
}
//创建订单
@GetMapping("/consumer/payment/save")
public ResponseResult save(Payment payment){
// ResponseResult rs =restTemplate.postForObject(PAYMENT_URL+"/payment/save", payment,ResponseResult.class);
List serviceInstances = discoveryClient.getInstances("SPRINGCLOUD-PAYMENT-PROVIDER-SERVICE");
ServiceInstance instance = serviceInstances.get(0);
ResponseResult rs = restTemplate.postForObject("http://"+instance.getHost()+instance.getPort()+"/payment/save",payment,ResponseResult.class);
return rs;
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
cn.bdqn
springcloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
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/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7002Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7002Application.class,args);
}
}
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
cn.bdqn
springcloud-api-commons
${project.version}
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-test
test
server:
port: 7003
eureka:
instance:
hostname: eureka7003.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-registry: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
@SpringBootApplication
@EnableEurekaServer
public class EurekaServer7003Application {
public static void main(String[] args) {
SpringApplication.run(EurekaServer7003Application.class,args);
}
}
springcloud-eureka-sever-7001
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #eureka服务器端的
client:
#false 表示不向注册中心注册自己
register-with-eureka: false
#false 表示自己端就是注册中心,我的职责就是维护服务实例,并不需要去检索服务
fetch-register: false
service-url:
#设置与Eureka server交互的地址查询服务和注册服务都需要依赖这个地址
# defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
defaultZone: http://eureka7001.com:7002/eureka/,http://eureka7003.com:7002/eureka/
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://eureka7003.com:7003/eureka/
springcloud-eureka-sever003
server:
port: 7003
eureka:
instance:
hostname: eureka7003.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://eureka7002.com:7002/eureka/,http://eureka7001.com:7001/eureka/
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
server:
port: 8001
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
cn.bdqn
springcloud-api-commons
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.0
com.alibaba
druid-spring-boot-starter
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-jdbc
org.springframework.boot
spring-boot-devtools
runtime
true
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-test
test
server:
port: 8002
spring:
application:
name: springcloud-payment-provider-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springcloud_db?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT
username: root
password: xiaoduo456new
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: cn.bdqn.domain
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true 单节点无所谓,集群必须设置true 才能配合ribbon 使用负载均衡
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,
http://eureka7002.com:7002/eureka,
http://eureka7003.com:7003/eureka,
instance:
prefer-ip-address: true #使用ip地址注册
@SpringBootApplication
@EnableEurekaClient
public class Payment8002Application {
public static void main(String[] args) {
SpringApplication.run(Payment8002Application.class,args);
}
}
5.25编写PaymentMapper接口
@Mapper
public interface PaymentMapper {
//保存一个支付流水
public void insert(Payment payment);
//根据id获取具体的支付信息
public Payment selectById(@Param("id") Integer id);
}
insert into t_payment(flow_number) values(#{flowNumber})
public interface PaymentServer {
//保存一个支付流水
public void save(Payment payment);
//根据id获取具体的支付信息
public Payment queryById(Integer id);
}
@Service
public class PaymentServerImpl implements PaymentServer {
@Autowired
private PaymentMapper paymentMapper;
@Override
public Payment queryById(Integer id) {
return paymentMapper.selectById(id);
}
@Override
public void save(Payment payment) {
paymentMapper.insert(payment);
}
}
@RestController
public class PaymentConroller {
@Autowired
private PaymentServerImpl paymentServer;
@GetMapping("/payment/id/{id}")
public ResponseResult queryById(@PathVariable(name="id") Integer id){
Payment payment = paymentServer.queryById(id);
if(payment!=null) {
return new ResponseResult(200,"成功",payment);
}else{
return new ResponseResult(404,"没有对应的记录,查询id"+id,null);
}
}
@PostMapping("/payment/save")
public ResponseResult save(@RequestBody Payment payment){
try {
paymentServer.save(payment);
return new ResponseResult(200,"插入成功",null);
}catch (Exception e){
e.printStackTrace();
return new ResponseResult(200,"插入失败",null);
}
}
}