springcloud系列之eurake注册中心

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangxing52077/article/details/81335021

1.场景还原

    在分布式横行的互联网时代,springcloud占据了分布式半壁江山;笔者就eurake注册中心开篇展开对springcloud系列教程的讲解,希望能给需要的小伙伴些许指导

2.实现方案

①搭建springcloud环境,配置pom依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

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

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

</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

②resources下application.yml配置

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:8072/eureka/
  server:
    enable-self-preservation: false #自我保护机制
    eviction-interval-timer-in-ms: 30000  #及时踢出已关停的节点

③启动类配置

@SpringBootApplication
@EnableEurekaServer
public class MicroServiceRegisterApplication {

   public static void main(String[] args) {
      SpringApplication.run(MicroServiceRegisterApplication.class, args);
   }
}

④配置eurake客户端

这里以笔者订单服务为例

1.订单服务的yml配置

eureka:
 client:
  healthcheck:
    enabled: true
  serviceUrl:
    defaultZone: http://localhost:8072/eureka/
 instance:
  prefer-ip-address: true
  lease-expiration-duration-in-seconds: 30  #续约到期时间
  lease-renewal-interval-in-seconds: 10     #续约更新时间间隔

2.订单服务的启动类配置

@SpringBootApplication
@ComponentScan("com.yivi")
@MapperScan({"com.yivi.yiviproj.microservicedispatch.dao","com.yivi.yivisender.dispatchdata.dao","com.yivi.yivisender.dispatchcalengine.dao"})
@EnableTransactionManagement
@EnableCaching
@EnableEurekaClient
public class MicroServiceDispatchApplication{

   public static void main(String[] args) {
      SpringApplication.run(MicroServiceDispatchApplication.class, args);
   }

   @Bean
   @LoadBalanced
      //开启负载均衡
   RestTemplate restTemplate() {
      return new RestTemplate();
   }

}

这里订单服务就注册到eurake注册中心去了

3.测试效果

好了,eurake服务就搭建成功了;我是张星,欢迎加入博主技术交流群,群号:526601468

猜你喜欢

转载自blog.csdn.net/zhangxing52077/article/details/81335021
今日推荐