SpringCloud学习笔记(1):Eureka注册中心

简介

Eureka是Netflix开源的基于rest的服务治理方案,分为Server端和Client端,Server端为注册中心,其他微服务通过Client端连接Server端进行服务的注册和发现。

项目介绍

  1. sc-parent,父模块
  2. sc-provider,提供者模块
  3. sc-eureka,注册中心
  4. sc-consumer-discovery,消费者模块

搭建父模块

创建父模块sc-parent,pom.xml:

<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.cf</groupId>
  <artifactId>sc-parent</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
   
   <!-- 继承Spring Boot的默认值 -->
   <parent>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-starter-parent</artifactId>
       <version>2.1.6.RELEASE</version>
       <relativePath /> <!-- lookup parent from repository -->
   </parent>
   
   <properties>
  	<!-- 降低maven-jar-plugin插件版本,防止版本不匹配导致的pom.xml第一行报错 -->
  	<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
   </properties>
   
   <dependencies>
   		<!-- 添加web应用依赖 -->
   		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-web</artifactId>
		</dependency>
   </dependencies>
   
   <dependencyManagement>
		<dependencies>
			<!-- SpringCloud依赖 -->
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Greenwich.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
   </dependencyManagement>
   
   <build>
		<plugins>
			   <!-- 打可执行jar包插件 -->
	           <plugin>
	               <groupId>org.springframework.boot</groupId>
	               <artifactId>spring-boot-maven-plugin</artifactId>
	           </plugin>
	           
	           <!-- 指定编译插件使用的jdk版本 -->
	           <plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<configuration>
						<source>1.8</source>
						<target>1.8</target>
					</configuration>
				</plugin>
		</plugins>
   </build>
</project>

搭建注册中心

1.在父模块下创建子模块项目sc-eureka,pom.xml:

<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>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-eureka</artifactId>
  
  <dependencies>
  	<!-- Eureka服务端依赖 -->
  	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
	</dependency>
  </dependencies>
</project>

2.创建启动类eureka.EurekaApplication:

package eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

//声明该类为SpringBoot服务的入口
@SpringBootApplication
//声明该微服务为注册中心,提供服务发现和注册的功能
@EnableEurekaServer
public class EurekaApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekaApplication.class, args);
	}
}

3.创建配置文件/src/main/resources/application.yml:

server:
  port: 8080 #当前服务端口

eureka:
  instance:
    hostname: localhost #当前Eureka实例主机名
  client:
    registerWithEureka: false #表示不向注册中心注册自己
    fetchRegistry: false #表示此客户端不需要从Eureka注册中心获取Eureka注册表信息
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ ##eureka对外提供的地址(客户端连接的地址)

其他配置信息可以参考EurekaInstanceConfigBean和EurekaClientConfigBean两个配置类

4.运行启动类EurekaApplication,在浏览器中访问http://localhost:8080/,出现如下图表示注册中心搭建成功:

提供者与服务注册

1.在父模块下创建子模块项目sc-provider,pom.xml:

<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>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-provider</artifactId>
  
  <dependencies>
  	<!-- Eureka客户端依赖,用于连接服务端进行服务注册和发现 -->
  	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
  </dependencies>
</project>

2.创建启动类provider.ProviderApplication:

package provider;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ProviderApplication {

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

3.创建Controller:provider.controller.BookController

package provider.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/book")
@RestController
public class BookController {
	
	@GetMapping("/list")
	public String getBookList(){
		//模拟从service返回数据
		return "[\"Java入门到放弃\",\"C++入门到放弃\",\"Python入门到放弃\",\"C入门到放弃\"]";
	}
}

4.创建配置文件/src/main/resources/application.yml:

server:
  port: 8081

spring:
  application:
    name: sc-provider #注册到Eureka注册中心上的服务名称,对应Eureka界面上的Application列
    
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/ #注册中心的访问地址
  instance:
    preferIpAddress: true #表示将自己的IP注册到Eureka注册中心。默认为false,表示将hostname注册到注册中心

5.依次启动注册中心sc-eureka和提供者sc-provider,当提供者启动时,会将自己的信息注册到Eureka注册中心,在浏览器中访问http://localhost:8080/,提供者sc-provider已经在注册中心注册。

消费者和服务发现

1.在父模块下创建子模块项目sc-consumer-discovery,pom.xml:

<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>
  <parent>
    <groupId>com.cf</groupId>
    <artifactId>sc-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>sc-consumer-discovery</artifactId>
  
  <dependencies>
  	<dependency>
	    <groupId>org.springframework.cloud</groupId>
	    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
	</dependency>
  </dependencies>
</project>

2.创建启动类consumer.ConsumerDiscoveryApplication:

package consumer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class ConsumerDiscoveryApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConsumerDiscoveryApplication.class, args);
	}
	
	@Bean
	public RestTemplate restTemplate(){
		return new RestTemplate();
	}
}

3.创建调用提供者服务的Controller:consumer.controller.ConsumerDiscoveryController

package consumer.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerDiscoveryController {
	@Autowired
	private DiscoveryClient discoveryClient;
	
	@Autowired
	private RestTemplate restTemplate;
	
	@GetMapping("/getBookList")
	public String getBookList(){
		//通过服务名获取实例信息
		List<ServiceInstance> list = discoveryClient.getInstances("sc-provider");
	    if (list != null && list.size() > 0 ) {
	    	//调用服务,并返回服务结果
	        return restTemplate.getForObject(list.get(0).getUri() + "/book/list", String.class);
	    }
	    return null;
	}
}

4.创建application.yml:

server:
  port: 8082

spring:
  application:
    name: sc-consumer-discovery
    
eureka:
  client:
    registerWithEureka: false #在本实例中消费者不提供服务,所以无需到注册中心注册。在实际应用中,消费者也可能是提供者。
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

5.依次启动注册中心sc-eureka、提供者sc-provider、消费者sc-consumer-discovery,当消费者启动时,会从注册中心查询可用的服务列表及其网络地址。直接访问提供者和消费者调用提供者结果如下:

总结

在传统的应用程序中,都是把提供者的网络地址硬编码在代码中,导致提供者和消费者耦合度高,当提供者网络地址发生了变化,则需要修改消费者配置并重新发布。Eureka起到了解耦的作用,提供者到Eureka注册中心中注册,消费者从Eureka注册中心中获取提供者的网络地址并进行调用,当提供者网络地址变更时会重新到注册中心注册。

posted @ 2019-09-11 12:01  布禾  阅读(986)  评论(0编辑  收藏  举报