Spring cloud 使用-eureka作为注册中心

Spring cloud

Spring Cloud 为开发者提供了工具来快速构建分布式系统中的一些常见模式(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式 会话,集群状态)。 分布式系统的协调导致了样板模式,使用 Spring Cloud 开发人员可以快速建立实现这些模式的服务和应用程序。 它们在任何分布式环境中都能很好地工作,包括开发人员自己的笔记本电脑、裸机数据中心和 Cloud Foundry 等托管平台。Spring cloud官网

使用 eureka 作为服务注册中心

eureka 模块

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

eureka启动类

@SpringBootApplication
@EnableEurekaServer
public class EruekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(EruekaApplication.class);
    }
}

配置properties.yml

server:
  port: 8761
eureka:
  instance:
    hostname: 127.0.0.1
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  server:
    enable-self-preservation: false
spring:
  application:
    name: erueka

eureka的具体文档可以通过Spring Cloud Netflix查阅

服务提供者

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>

配置 properties.yml

server:
  port: 18081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/cloud_consumer?useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  application:
    name: consumerservice
mybatis:
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
logging:
  level:
    cn.geoary: debug
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

启动类,注意这里添加了@EnableEurekaClient注解

@MapperScan("cn.geoary.mapper")
@SpringBootApplication
@EnableEurekaClient
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class);
    }
}

提供者提供了一个get请求 http://127.0.0.1:18080/consumer/{id} 的请求方法,用来通过主键查询患者信息。

服务消费者

消费者的配置与提供者类似,现在只把区别展示下。
调用请求方式,通过RestTemplate,注意,这个需要添加注解@LoadBalanced

@Bean
@LoadBalanced
public RestTemplate restTemplate(){
    return new RestTemplate();
}

调用代码段:

private final String CONSUMER_SERVICE = "consumerservice";

String url = "http://" + CONSUMER_SERVICE + "/consumer/" + consumerId;
// String url = "http://127.0.0.1:18081/consumer/" + consumerId;
TConsumer tConsumer = restTemplate.getForObject(url, TConsumer.class);

这样我们就把之前需要通过写死的ip:port去调用请求的方式调成了 通过微服务名的方式来调用了。
通过 eureka 控制台可以查看注册的微服务,也就是这里的CONSUMERSERVICEDRUGSETVICE了。

posted @   Geoary  阅读(62)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示