转载

SpringCloud(二):服务发现与消费

[TOCM]

[TOC]

通过上一节内容介绍与实践,我们已经搭建起微服务架构中的核心组件——服务注册中心 SpringCloud(一):服务注册中心Eureka

以下Model基于上一节项目新增

现在我们已经有了一个服务注册中心,下面就来尝试下构建服务提供者和服务消费者,

###注册服务提供者

当client向server注册时,它会提供一些元数据,例如主机和端口,URL,主页等。Eureka server(注册中心) 从每个client实例接收心跳消息。 如果心跳超时,则通常将该实例从注册server中删除。

  • 创建一个model项目(eureka-client)

  • 服务提供者的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      	<modelVersion>4.0.0</modelVersion>
    
      	<groupId>com.caogen</groupId>
      	<artifactId>eureka-client</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
    
      	<name>eureka-client</name>
      	<description>Demo project for Spring Boot</description>
    
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>1.5.8.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
    
      	<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>Edgware.RELEASE</spring-cloud.version>
      	</properties>
    
      	<dependencies>
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-eureka</artifactId>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-actuator</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>
    
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      			</plugin>
      		</plugins>
      	</build>
    
      </project>
  • 在应用类中通过加上@EnableDiscoveryClient注解,表明自己是服务提供者

    import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
      @EnableDiscoveryClient
      @SpringBootApplication
      public class EurekaClientApplication {
    
      	public static void main(String[] args) {
      		SpringApplication.run(EurekaClientApplication.class, args);
      	}
      }
  • 需要在application.yml配置文件中,通过一系列配置来制定注册中心地址和服务名等等

    server:
        port: 1112
    
      management:
        security:
      	enabled: false
    
      spring:
        application:
      	name: eureka-client
    
      eureka:
        client:
      	serviceUrl:
      			  defaultZone: http://localhost:1111/eureka/
  • 创建HelloController类并实现/hello接口

    import org.springframework.cloud.client.ServiceInstance;
      import org.springframework.cloud.client.discovery.DiscoveryClient;
      import org.springframework.web.bind.annotation.RequestMapping;
      import org.springframework.web.bind.annotation.RestController;
    
      import javax.annotation.Resource;
    
      @RestController
      public class HelloController {
    
      	@Resource
      	private DiscoveryClient client;
    
      	@RequestMapping("/hello")
      	public String sayHello() {
      		ServiceInstance instance = client.getLocalServiceInstance();
      		return "Hello,World! port:" + instance.getPort();
      	}
    
      }
  • 然后在注册中心启动的情况下,启动eureka-client

SpringCloud(二):服务发现与消费

我们可以通过访问Eureka的信息面板看到服务的注册信息

SpringCloud(二):服务发现与消费

我通过修改端口又启动了一个eureka-client应用,这个时候有两个服务提供实例了。 可以看到名为EUREKA-CLIENT的服务中出现了两个实例单元。

###服务消费者

现在我们已经有了服务注册中心和服务提供者,下面就来尝试构建一个服务消费者,它主要完成两个目标,发现服务以及消费服务。其中,服务发现的任务有Eureka的客户端完成,而服务消费的任务由Ribbon完成。Ribbon是一个基于HTTP和TCP的客户端负载均衡器

  • 创建一个Model项目(ribbon-consumer)

  • 服务消费者的pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
      <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      	<modelVersion>4.0.0</modelVersion>
    
      	<groupId>com.caogen</groupId>
      	<artifactId>ribbon-consumer</artifactId>
      	<version>0.0.1-SNAPSHOT</version>
      	<packaging>jar</packaging>
    
      	<name>ribbon-consumer</name>
      	<description>Demo project for Spring Boot</description>
    
      	<parent>
      		<groupId>org.springframework.boot</groupId>
      		<artifactId>spring-boot-starter-parent</artifactId>
      		<version>1.5.8.RELEASE</version>
      		<relativePath/> <!-- lookup parent from repository -->
      	</parent>
    
      	<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>Edgware.RELEASE</spring-cloud.version>
      	</properties>
    
      	<dependencies>
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-web</artifactId>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-eureka</artifactId>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.cloud</groupId>
      			<artifactId>spring-cloud-starter-ribbon</artifactId>
      		</dependency>
    
      		<dependency>
      			<groupId>org.springframework.boot</groupId>
      			<artifactId>spring-boot-starter-test</artifactId>
      			<scope>test</scope>
      		</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>
    
      	<build>
      		<plugins>
      			<plugin>
      				<groupId>org.springframework.boot</groupId>
      				<artifactId>spring-boot-maven-plugin</artifactId>
      			</plugin>
      		</plugins>
      	</build>
    
      </project>
  • 在应用类中通过@EnableDiscoveryClient注解让该应用注册为Eureka客户端应用,以获得服务发现的能力。同事,在该主类中创建RestTemplate的Spring Bean实例,并通过@LoadBalanced注解开启客户端负载均衡。

    import org.springframework.boot.SpringApplication;
      import org.springframework.boot.autoconfigure.SpringBootApplication;
      import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
      import org.springframework.cloud.client.loadbalancer.LoadBalanced;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.client.RestTemplate;
    
      @EnableDiscoveryClient
      @SpringBootApplication
      public class RibbonConsumerApplication {
    
      	@Bean
      	@LoadBalanced
      	RestTemplate restTemplate() {
      		return new RestTemplate();
      	}
    
      	public static void main(String[] args) {
      		SpringApplication.run(RibbonConsumerApplication.class, args);
      	}
      }
  • 创建ConsumerController类并实现/ribbon接口。

    import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      import org.springframework.web.client.RestTemplate;
    
      import javax.annotation.Resource;
    
      @RestController
      public class ConsumerController {
    
      	@Resource
      	RestTemplate restTemplate;
    
      	@GetMapping(value = "/ribbon")
      	public String helloConsumer() {
      		return restTemplate.getForObject("http://EUREKA-CLIENT/hello", String.class);
      	}
    
      }
  • 需要在application.yml配置文件中,通过一系列配置来制定注册中心地址和服务名等等

    eureka:
        client:
      	serviceUrl:
      	  defaultZone: http://localhost:1111/eureka/
    
      server:
        port: 1115
      spring:
        application:
      	name: ribbon-consumer
  • 在注册中心和提供服务者都启动的情况下,启动ribbon-consumer应用,我们可以在Eureka信息面板中看到,当前除了EUREKA-CLIENT之外,还多了我们实现的RIBBON-CONSUMER服务。

SpringCloud(二):服务发现与消费

  • 通过向http://localhost:1115/ribbon发起多次GET请求,浏览器会交替显示

    Hello,World! port:1113 Hello,World! port:1112

这说明我们通过向注册中心发现服务并消费进行了轮询访问,以实现基于客户端的负载均衡。

原文  https://www.kcaogen.com/blog/info/21
正文到此结束
Loading...