Spring Cloud入门:服务注册与服务发现

文章实例使用的Spring Cloud版本为Finchley.SR1,Spring Boot版本为2.0.4

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

Spring Cloud Eureka 是Spring Cloud Netflix项目下的服务治理模块。服务治理是微服务架构中最为核心和基础的模块,它主要用户实现各个微服务实例的自动化注册与发现。
服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号、版本号、通信协议等一些附加信息告知注册中心,注册中心按服务名分类组织服务清单。
服务发现:服务之间的调用不再通过指定具体的实例地址来实现,而是通过向服务名发起请求调用实现。
Spring Cloud Eureka是一个使用了Netflix Eureka 来实现服务注册与发现的模块。

2 搭建服务注册中心eureka-server

2.1 新建Spring Boot项目,引入相关依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version><!--注意:Spring Cloud 2.x是不支持Spring Boot1.x版本的-->
		<relativePath/>
	</parent>

	<properties>
		<project.build.sou1.rceEncoding>UTF-8</project.build.sou1.rceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<!--与Spring Cloud1.x区别,1.x使用的是spring-cloud-starter-eureka-server-->
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

2.2 使用@EnableEurekaServer注解启动一个服务注册中心提供给其他应用进行对话

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

2.3 application.properties添加配置

spring.application.name=eureka-server
server.port=1001

#主机名
eureka.instance.hostname=localhost

#不向注册中心注册自己
eureka.client.register-with-eureka=false

#是否允许客户端向Eureka 注册表获取信息
#由于注册中心本身的职责是维护服务实例,所以并不需要去检索服务
eureka.client.fetch-registry=false

#指定服务注册中心的位置
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

2.4 启动注册中心

打开http://localhost:1001/,可以看到
注册中心

3 创建服务提供者eureka-client

3.1 创建Spring Boot项目,引入相关依赖

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4.RELEASE</version>
		<relativePath/>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<!--与Spring Cloud1.x区别,1.x使用的是spring-cloud-starter-eureka-->
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR1</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

3.2 使用@SpringCloudApplication注解,使能够注册到注册中心并对外提供服务

该注解包含了@SpringBootApplication,@EnableDiscoveryClient,@EnableCircuitBreaker等其他注解

@SpringCloudApplication
public class EurekaClientApplication {

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

}

3.3 application.properties添加配置

spring.application.name=eureka-client
server.port=2001

#项目信息
info.name=${spring.application.name}
info.server.ip-address=${spring.cloud.client.ip-address}
info.server.port=${server.port}

#实例默认通过使用域名形式注册到注册中心:false
eureka.instance.prefer-ip-address=true

#实例名
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}

#指定注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

3.4 编写接口

@RestController
public class HelloController {
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/hello")
    public String hello() {
        String services = "Services: " + discoveryClient.getServices();
        System.out.println(services);
        return "Hello,Spring Cloud";
    }
}

3.5 启动eureka-client

打开注册中心http://localhost:1001/,可以看到服务eureka-client已经成功注册到了服务注册中心:
服务注册中心

猜你喜欢

转载自blog.csdn.net/CrazyLai1996/article/details/83957039