Eureka(1)--->使用eureka

1、Eureka是什么,有何作用?

      eureka是一个组件,用来实现服务的注册与发现,我们在微服务的体系中,会有一个服务中心,这个服务中心储存着所有微服务的信息,例如某台服务的ip、端口等信息,当我们进行远程调用的时候可以从服务中心去获取到服务提供者的信息。eureka 就是用来作为服务注册中心的。

     eureka 架构图:

              

 

2、使用eureka单机模式

      eureka有两种使用方式,第一种就是单机版本,使用案例如下;

      2.1、案例目录:

               

               eureka-server 用来作为服务中心的服务端,负责存储服务信息;

               eureka-client、consumer 用于模拟eurekaClient,负责从eurekaServer上获取服务提供者provider的信息。

               provider 用于提供服务,启动的时候使用eurekaClient将其信息 注册到eurekaServer上。

        2.2、eureka-server项目核心代码列举:

                pom.xml :  

    <parent>
        <artifactId>spring-cloud-discovere-erueka</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server</artifactId>
    <packaging>jar</packaging>

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

            启动类:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerDiscoveryApplication {

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

}

             配置文件application.properties:

# 应用名称
spring.application.name=eureka-server
server.port=9090

#eureka 服务端自我注册
eureka.client.register-with-eureka=false
#关闭拉取服务数据,服务数据本来就在自己上面,不需拉取,但是客户端需要拉取。
eureka.client.fetch-registry=false
#配置服务端地址,在客户端时候 注意 port 是9090
eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka

      2.3、provider项目核心代码列举:

               pom.xml文件:

    <parent>
        <artifactId>spring-cloud-discovere-erueka-standalone</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>
    <packaging>jar</packaging>

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

        <dependency>
            <groupId>com.wzy</groupId>
            <artifactId>provider-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

            启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class ProviderApplication {

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

             配置文件application.properties:

# 应用名称
spring.application.name=provider
server.port=8081

#配置服务端地址
eureka.client.service-url.defaultZone=http://localhost:9090/eureka

        提供的测试Controller:

@RestController
public class OrderController {

    @GetMapping("getOrderById")
    public OrderDto getOrderById(@RequestParam("orderId") String orderId){
        try {
            Thread.sleep(2000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return orders.get(orderId);
    }

    @PostMapping("saveOrder")
    public OrderDto saveOrder(@RequestBody OrderDto orderDto){
        return orders.putIfAbsent(orderDto.getId(), orderDto);
    }

    public static final Map<String , OrderDto> orders = new HashMap<>();
}

 

 2.4、consumer项目核心代码列举:

          pom.xml:

    <parent>
        <artifactId>spring-cloud-discovere-erueka-standalone</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>consumer</artifactId>
    <packaging>jar</packaging>

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

        <dependency>
            <groupId>com.wzy</groupId>
            <artifactId>provider-api</artifactId>
            <version>${project.version}</version>
        </dependency>
    </dependencies>

         启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class ConsumerApplication {

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

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

          配置文件application.properties:

# 应用名称
spring.application.name=consumer
server.port=8080

#配置服务端地址
eureka.client.service-url.defaultZone=http://localhost:9090/eureka
provider.application.name = provider

         提供测试的Controller:

@RestController
public class UserController {

    @Value("${provider.application.name}")
    private String providerName;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("userGetOrderById")
    public OrderDto userGetOrderById(@RequestParam("orderId") String orderId){
        ResponseEntity <OrderDto> forEntity = restTemplate.getForEntity("http://" + providerName + "/getOrderById?orderId=" + orderId, OrderDto.class);
        return forEntity.getBody();
    }

    @PostMapping("userSaveOrder")
    public OrderDto userSaveOrder(@RequestBody OrderDto orderDto){
        OrderDto forObject = restTemplate.postForObject("http://" + providerName + "/saveOrder", orderDto, OrderDto.class);
        return forObject;
    }
}

以上就是单机版eureka的使用,调用链路为consumer----->provider。

 

3、使用eureka集群模式

      集群模式我们采用三个节点的规模,案例项目结构如下:

                 

                 eureka-server01:第一个节点

                 eureka-server02:第二个节点

                 eureka-server03:第三个节点

                 eureka-client1、eureka-client2:用于测试服务发现功能。

       集群搭建准备工作:

                 1、在本机构建三个域名,分别为: www.eureka-server01.com、www.eureka-server02.com、www.eureka-server03.com ;

                 2、配置hosts为如下:127.0.0.1     www.eureka-server03.com
                                                     127.0.0.1     www.eureka-server02.com
                                                     127.0.0.1     www.eureka-server01.com

 

        eureka-server01核心代码列举:

               pom.xml:

    <parent>
        <artifactId>spring-cloud-discovery-eureka-cluster</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server01</artifactId>
    <packaging>jar</packaging>

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

              启动类:

@SpringBootApplication
@EnableEurekaServer
public class EurekaServer1Application {

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

              配置文件application.properties:

# 应用名称
spring.application.name=eureka-server01
server.port=9091

eureka.instance.hostname=www.eureka-server01.com

#三个eureka-server 两两注册
eureka.client.service-url.defaultZone=\
  http://www.eureka-server02.com:9092/eureka,\
  http://www.eureka-server03.com:9093/eureka

        eureka-server02核心代码列举:处理application.properties文件内容有所区别其他都一样

               application.properties:

# 应用名称
spring.application.name=eureka-server02
server.port=9092

eureka.instance.hostname=www.eureka-server02.com
eureka.client.service-url.defaultZone=\
  http://www.eureka-server01.com:9091/eureka,\
  http://www.eureka-server03.com:9093/eureka

        eureka-server03核心代码列举:处理application.properties文件内容有所区别其他都一样

                application.properties:

# 应用名称
spring.application.name=eureka-server03
server.port=9093

eureka.instance.hostname=www.eureka-server03.com
eureka.client.service-url.defaultZone=\
  http://www.eureka-server01.com:9091/eureka,\
  http://www.eureka-server02.com:9092/eureka

 

       eureka-client1核心带代码列举:

             pom.xml :

    <parent>
        <artifactId>spring-cloud-discovery-eureka-cluster</artifactId>
        <groupId>com.wzy</groupId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-client1</artifactId>
    <packaging>jar</packaging>

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

               启动类:

@SpringBootApplication
@EnableDiscoveryClient
public class EurekaClient1Application {

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

                application.properties:

spring.application.name=eureka-client1
server.port=8081

eureka.client.service-url.defaultZone = \
  http://www.eureka-server01.com:9092/eureka,\
  http://www.eureka-server02.com:9092/eureka,\
  http://www.eureka-server03.com:9093/eureka

                eureka-client2的核心代码跟eureka-client1的基本一致。

 

以上就是eureka集群模式的使用方式,核心思想就是让多个eurekaServer节点进行两两注册,客户端在使用的时候,配置多个eurekaServer节点的地址即可。

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Starter Eureka Server is a starter dependency that provides the Eureka Server module of the Spring Cloud Netflix project. Eureka Server is a service registry that enables microservices to discover and communicate with each other. With Eureka, each microservice registers itself with the Eureka Server and queries the Eureka Server to find other microservices it depends on. The Spring Cloud Starter Eureka Server provides an easy way to set up an Eureka server instance in a Spring Boot application. It includes all the necessary dependencies and configuration to enable the Eureka Server and register services. To use Spring Cloud Starter Eureka Server, you can add the following dependency to your project: ``` <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> ``` After adding the dependency, you can annotate your main Spring Boot application class with `@EnableEurekaServer` to enable the Eureka Server: ``` @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } } ``` Once you have the Eureka Server set up, you can register microservices with it using the `@EnableDiscoveryClient` annotation in your microservice application. Overall, the Spring Cloud Starter Eureka Server provides an easy and efficient way to set up an Eureka Server in a Spring Boot application and enable microservice communication.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值