server:
port: 7001
eureka:
instance:
hostname: localhost #服务端的实例名称
client:
#false表示不向服务器注册自己
register-with-eureka: false
#false表示自己就是注册中心 去维护服务实例 不需要去检索服务
fetch-register: false
#设置与Enreka server交互的地址查询服务和注册服务都需要这个地址
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
server:
port: 7001
eureka:
instance:
hostname: eureka7001.com #服务端的实例名称 主机别名 修改
client:
#false表示不向服务器注册自己
register-with-eureka: false
#false表示自己就是注册中心 去维护服务实例 不需要去检索服务
fetch-register: false
#设置与Enreka server交互的地址 查询服务和注册服务都需要这个地址 修改
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
server:
port: 7002
eureka:
instance:
hostname: eureka7002.com #服务端的实例名称 主机别名
client:
#false表示不向服务器注册自己
register-with-eureka: false
#false表示自己就是注册中心 去维护服务实例 不需要去检索服务
fetch-register: false
#设置与Enreka server交互的地址查询服务和注册服务都需要这个地址
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
server:
port: 8888
spring:
application:
name: cloud-order-service
eureka:
client:
register-with-eureka: true #自己注册到eureka 默认true
#是否从eureka抓取已有的注册信息 默认为true 单节点无所谓 集群必须为true 才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
#defaultZone: http://localhost:7001/eureka #注册中心地址
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
server:
port: 8001
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/db2019?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: 1234
mybatis:
mapper-locations: classpath:mapper/*.xml #配置文件
type-aliases-package: com.lyx.cloud.entities #所有entity别名所在包
eureka:
client:
register-with-eureka: true #自己注册到eureka 默认true
#是否从eureka抓取已有的注册信息 默认为true 单节点无所谓 集群必须为true 才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka #注册中心地址 单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
server:
port: 8002
spring:
application:
name: cloud-payment-service
datasource:
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:mysql://localhost:3306/db2019?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
username: root
password: 1234
mybatis:
mapper-locations: classpath:mapper/*.xml #配置文件
type-aliases-package: com.lyx.cloud.entities #所有entity别名所在包
eureka:
client:
register-with-eureka: true #自己注册到eureka 默认true
#是否从eureka抓取已有的注册信息 默认为true 单节点无所谓 集群必须为true 才能配合ribbon使用负载均衡
fetch-registry: true
service-url:
# defaultZone: http://localhost:7001/eureka #注册中心地址 单机版
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka #集群版
@RestController
@Slf4j
public class OrderController {
@Autowired
private RestTemplate restTemplate;
//private static final String BASE_URL="http://localhost:8001"; 修改前的端口
private static final String BASE_URL="http://CLOUD-PAYMENT-SERVICE"; 修改后 变成在eureka注册的服务实例名称
@PostMapping("/comsumer/payment/create")
public CommonResult<PayMent> create(@RequestBody PayMent payMent){
return restTemplate.postForObject(BASE_URL+"/payment/create",payMent,CommonResult.class);
}
@GetMapping("/comsumer/payment/get/{id}")
public CommonResult<PayMent> getPaymentByid(@PathVariable("id") Long id){
return restTemplate.getForObject(BASE_URL+"/payment/get/"+id,CommonResult.class);
}
}
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
原文:https://www.cnblogs.com/lyx666/p/12749077.html