当前位置: 代码迷 >> 综合 >> Eureka(1)--->使用eureka
  详细解决方案

Eureka(1)--->使用eureka

热度:121   发布时间:2023-10-24 14:46:40.0

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@LoadBalancedpublic 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;@Autowiredprivate 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=9091eureka.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=9092eureka.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=9093eureka.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=8081eureka.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节点的地址即可。

 

 

 

 

 

 

  相关解决方案