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已经成功注册到了服务注册中心:
服务注册中心

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于Spring Cloud入门教程,你可以参考以下步骤: 1. 安装Java开发环境:确保你的机器上已经安装了Java Development Kit(JDK)。 2. 创建Spring Boot项目:使用Spring Initializr(https://start.spring.io/)创建一个新的Spring Boot项目。在这个页面上,你可以选择使用的Spring Boot版本、添加的依赖以及其他项目配置。 3. 添加Spring Cloud依赖:在创建项目时,你可以选择添加Spring Cloud的依赖。选择适合你需求的Spring Cloud组件,如Eureka、Zuul、Feign等。 4. 配置Spring Cloud组件:根据你选择的Spring Cloud组件,进行相应的配置。比如使用Eureka作为服务注册中心,需要配置Eureka Server;使用Feign进行服务调用,需要配置Feign Client等。 5. 创建微服务:根据你的业务需求,创建相应的微服务。每个微服务都是一个独立的Spring Boot应用。 6. 注册服务服务注册中心:在每个微服务启动时,将自己注册服务注册中心。这样其他微服务就可以通过服务注册中心来发现和调用该微服务。 7. 实现服务间通信:使用Spring Cloud提供的组件进行服务间通信。比如使用Feign调用其他微服务提供的接口。 8. 配置负载均衡:可以使用Spring Cloud提供的负载均衡组件(如Ribbon)来实现负载均衡。 9. 实现服务容错:使用Spring Cloud提供的熔断器(如Hystrix)来实现服务容错。 10. 部署和测试:将微服务部署到服务器上,并进行测试验证。 这只是一个简单的入门教程的大致流程,具体的实现细节和配置可能因你的需求和环境而有所不同。你可以根据这个流程逐步学习和实践Spring Cloud,进一步深入了解和应用它。 希望对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值