栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring cloud+spring cloud alibaba

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring cloud+spring cloud alibaba

首先,要注意spring boot和spring cloud的版本匹配
微服务模块:

  1. 见模块
  2. 改POM
  3. 写YML
  4. 主启动
  5. 业务类
Eureka

1.服务提供端要向eureka提供心跳
2.分为eureka client和eureka server
eureka client:对eureka进行注册
eureka server:向服务端提供注册服务
3.集群:集群中的每个eureka都要注册其他的eureka,也就是相互注册

搭建Eureka集群

包括eureka7001,eureka7002,cloud-provider-payment8001,cloud-provider-payment8002,cloud-consumer-order80
其中: payment端口提供服务,而order端口使用服务

eureka集群中的每个eureka都要在defaultZone中指向其他的eureka

eureka7001:

使用@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的地址然后再发送请求
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695006.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号