spring cloud Eureka注册中心的使用 01

spring cloud Eureka注册中心的使用 01,第1张

   项目整体图 

1.导入pom

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

 
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
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);

访问验证结果 

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/789250.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-05
下一篇 2022-05-05

发表评论

登录后才能评论

评论列表(0条)

保存