Spring Cloud Eureka(服务注册与发现)

Spring Cloud Eureka?

基于Netflix Eureka做了二次封装

两个组件组成:

    Eureka Server    注册中心

    Eureka Client    服务发现


(实现)Spring Cloud Eureka Server:

pom.xml

            org.springframework.cloud

            spring-cloud-starter-netflix-eureka-server

Application.java

@EnableEurekaServer

public class Application {

......

application.yml

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/

    #表示是否将自己注册在EurekaServer上,默认为true。由于当前应用就是EurekaServer,所以置为false

    register-with-eureka: false

#关闭注册保护机制

#  server:

#    enable-self-preservation: false

spring:

  application:

    name: eureka

server:

  port: 8761


(实现)Spring Cloud Eureka Client:

pom.xml

            org.springframework.cloud

            spring-cloud-starter-netflix-eureka-client

Application.java

@EnableDiscoveryClient

public class Application {

......

application.yml

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

#自定义链接

#  instance:

#    hostname: clientName

spring:

  application:

    name: client


Spring Cloud Eureka 高可用

启动多份Eureka后对其进行互相注册


Eureka1 端口8761

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8762/eureka/

spring:

  application:

    name: eureka


Eureka2 端口8762

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/

spring:

  application:

    name: eureka


Client

eureka:

  client:

    service-url:

      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/

spring:

  application:

    name: client


应用间通信的两种方式

   1)RestTemplate:

        利用@LoadBanlancer,然后再使用RestTemplate

2)Feign

        在pom.xml中增加feign依赖,在启动类加上注解@EnableFeignClients,声明要调用的方法 @FeignClient @GetMapping


RestTemplate

第一种方式(直接使用restTemplate,url写死)

RestTemplate restTemplate = new RestTemplate();

String response = restTemplate.getForObject("http://localhost:8080/msg", String.class);

第二种方式(利用loadBalancerClient通过应用名称获取url,然后再使用restTemplate)

ServiceInstance serviceInstance = loadBalancerClient.choose("PRODUCT");

String url = String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort()) + "/msg";

String response = restTemplate.getForObject(url, String.class);

第三种方式(利用@LoadBalancer,可在restTemplate里使用应用名字)

@Component

public class RestTemplateConfig {

    @Bean

    @LoadBalanced

    public RestTemplate restTemplate(){

        return new RestTemplate();

    }

}

String response = restTemplate.getForObject("http://PRODUCT/msg", String.class);


Feign

Client

pom.xml

    org.springframework.cloud

    spring-cloud-starter-feign

Application.java

@EnableFeignClients

public class Application {

......

ProductClient

@FeignClient(name = "product")

public interface ProductClient {

    @GetMapping("/msg")

    String productMsg();

}

@Autowired

private ProductClient productClient;

String response = productClient.ProductMsg();

你可能感兴趣的:(Spring Cloud Eureka(服务注册与发现))