spring cloud Eureka注册中心的使用 01

本文详细介绍了如何在Spring Cloud中实现服务注册中心(Eureka Server)与服务提供者(Eureka Client)的配置,并演示了远程调用和微服务间通信的过程。通过实例展示了如何启动Eureka Server、配置application.yml、创建RestTemplate以及验证服务交互。
摘要由CSDN通过智能技术生成

   项目整体图 

1.导入pom

(服务注册中心和服务提供端都要导)

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.启动类开启注解

服务注册中心

@EnableEurekaServer

服务提供端

@EnableEurekaClient

3.配置application.yml

#服务中心
spring:
  application:
    name: eureka

server:
  port: 10086
#eureka地址信息 集群使用
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka
#服务发现
server:
  port: 8080

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

#Driud数据源
spring:
  application:
    name: order
  datasource:
    druid:
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/cloud
      username: root
      password: 123456

eureka:
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://127.0.0.1:10086/eureka

4.启动测试

先启动eureka

 在启动服务发现端

 注册成功

5.服务之间的通讯

5.1 先在启动类注册RestTempalte

 //远程调用的注册的
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

 5.2 在服务层远程调用

  //远程调用user
        String url="http://user/user/"+order.getUserId();
        User user = restTemplate.getForObject(url, User.class);
        //给order订单设置user对象
        order.setUser(user);

访问验证结果 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值