从零开始学习SpringBoot

SpringBoot - Eureka

2018-07-02  本文已影响22人  BzCoder

相关资料:

一.什么是Eureka

Eureka的本意是我发现了,我找到了的意思,它同时也是SpringCloud中的Netflix 出品的用于实现服务注册和发现的工具。首先了解一些概念:

因此Eureka Server可以很好的应对因网络故障导致部分节点失联的情况,而不会像ZK那样如果有一半不可用的情况会导致整个集群不可用而变成瘫痪。

二.实践部分

程序主要分为三部分,在Server一般选择集群式部署,在本文中,单部署和集群部署均有写。

1.Server

Maven:添加eureka-server

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

Config:添加@EnableEurekaServer注释

/**
 * 注册中心
 * @author: BaoZhou
 * @date : 2018/6/29 11:06
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

配置文件:
单Server配置文件

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server
  client:
    #不把自己注册到Eureka上
    register-with-eureka: false
    #不从Eureka获取注册信息
    fetch-registry: false
    service-url:
       default-zone: http://localhost:8761/eureka

集群Server配置文件,主要是要把几个Server之间要互相注册,服务注册时只要在其中一个Server上注册,其他Server会自动同步:

spring:
  application:
    name: eureka
---
spring:
  profiles: eureka1
eureka:
  instance:
    hostname: eureka1
  client:
    service-url:
      default-zone: http://localhost:8762/eureka,http://localhost:8763/eureka
---
spring:
  profiles: eureka2
eureka:
  instance:
    hostname: eureka2
  client:
    service-url:
      default-zone: http://localhost:8761/eureka,http://localhost:8763/eureka
---
spring:
  profiles: eureka3
eureka:
  instance:
    hostname: eureka3
  client:
    service-url:
      default-zone: http://localhost:8761/eureka,http://localhost:8762/eureka

启动服务时,只要将打出来的Jar包依次带参执行即可:

2.Provider

Maven中添加eureka-client

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

Config中添加@EnableDiscoveryClient注释

@EnableDiscoveryClient 
@SpringBootApplication
public class ProviderApplication {

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

配置文件

#多provide配置
---
spring:
  profiles: provider1
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
---
spring:
  profiles: provider2
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8762/eureka/

Controller

/**
 * @author BaoZhou
 * @date 2018/6/29
 */
@RestController
public class TicketController {
    @Autowired
    TicketService ticketService;

    @GetMapping("/ticket")
    public String getTicket() {
        return ticketService.getTicket();
    }
}

启动服务时,只要将打出来的Jar包依次带参执行即可:

3.Client

Maven中添加eureka-client

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

Config中添加@EnableDiscoveryClient注释,并且将RestTemplate加入到容器中,@LoadBalanced可以开启负载均衡,默认是轮询式。

@EnableDiscoveryClient //开启发现服务功能
@SpringBootApplication
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }

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

Controller:调用restTemplate通过Http的方式访问服务,访问的格式为应用的名字+方法的名字

/**
 * @author BaoZhou
 * @date 2018/6/29
 */
@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/buy")
    public String buyTicket(String name) {
        /*application:name + methodName*/
       return name+restTemplate.getForObject("http://PROVIDER/ticket", String.class);
    }
}

配置文件:

server:
  port: 8002
spring:
  application:
   name: comsumer-ticket
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka

三.运行程序

轮询

负载均衡机制让请求在两个Provider上依次执行,同时在三个Server上都可以看到Provider与Consumer服务。

四.Eureka与Dubbo的区别

上一篇 下一篇

猜你喜欢

热点阅读