首先,要注意spring boot和spring cloud的版本匹配
微服务模块:
- 见模块
- 改POM
- 写YML
- 主启动
- 业务类
1.服务提供端要向eureka提供心跳
2.分为eureka client和eureka server
eureka client:对eureka进行注册
eureka server:向服务端提供注册服务
3.集群:集群中的每个eureka都要注册其他的eureka,也就是相互注册
包括eureka7001,eureka7002,cloud-provider-payment8001,cloud-provider-payment8002,cloud-consumer-order80
其中: payment端口提供服务,而order端口使用服务
eureka7001:eureka集群中的每个eureka都要在defaultZone中指向其他的eureka
使用@EnableEurekaServer
yml文件如下:
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://localhost:7002/eureka/ #指向了另一个eureka,如此就构成了集群
spring:
# application:
# name: cloud-payment-service
# zipkin:
# base-url: http://localhost:9411
# sleuth:
# sampler:
# #采样率值介于 0 到 1 之间,1 则表示全部采集
# probability: 1
datasource:
# type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
# driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
payment8001:
使用@EnableEurekaClient
yaml文件:
server:
port: 8001
spring:
application:
name: cloud-payment-service
# zipkin:
# base-url: http://localhost:9411
# sleuth:
# sampler:
# #采样率值介于 0 到 1 之间,1 则表示全部采集
# probability: 1
datasource:
# type: com.alibaba.druid.pool.DruidDataSource # 当前数据源操作类型
# driver-class-name: org.gjt.mm.mysql.Driver # mysql驱动包
url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapperLocations: classpath:mapper/*.xml
type-aliases-package: com.atguigu.springcloud.entities # 所有Entity别名类所在包
eureka:
client:
#表示是否将自己注册进EurekaServer默认为true。
register-with-eureka: true
#是否从EurekaServer抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka
#向两个eureka注册自己
order80:
使用@EnableEurekaClient
yml文件:
server:
port: 80
spring:
application:
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://localhost:7001/eureka,http://localhost:7002/eureka # 集群版
#将自己注册到eureka
注册restTemplate
@Configuration
public class ApplicationContextConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate(){
return new RestTemplate();
}
}
Controller:
@RestController
@Slf4j
public class OrderController {
public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
@Autowired
private RestTemplate restTemplate;
@GetMapping("/consumer/payment/create")
public CommonResult create(Payment 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);
//向eureka找到payment的地址然后再发送请求
}
}



