亲宝软件园·资讯

展开

Spring Cloud Eureka 注册 Spring Cloud Eureka 注册与发现操作步骤详解

godliu711 人气:0
想了解Spring Cloud Eureka 注册与发现操作步骤详解的相关内容吗,godliu711在本文为您仔细讲解Spring Cloud Eureka 注册的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Spring,Cloud,Eureka,注册,Spring,Cloud,Eureka,下面大家一起来学习吧。

在搭建Spring Cloud Eureka环境前先要了解整个架构的组成,常用的基础模式如下图:

在这里插入图片描述

服务提供者:将springboot服务编写好以后,通过配置注册中心地址方式注册,提供给消费者使用。
注册中心:服务的中间桥梁,服务提供者将服务注册。服务消费者可以通过注册信息调用需要使用的服务。
服务消费者:通过规定的调用方式,读取注册中心的注册信息,调用相应的服务。

在这里插入图片描述

根据后续的服务复杂度进化以后,可以看到服务提供者也可以是服务消费者,服务消费者也可以是服务提供者。根据不同的业务情况是可以互相调用的。

下面来搭建一个基础的eureka。环境还是使用的之前的spring官方下载的。

内容写的比较详细,可以跟这一步步操作。

一、注册中心

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

以下是几个需要修改和添加的地方,后面会有完整的pom.xml

<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
<!-- 最新版的 eureka 服务端包 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<!-- 监控管理 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
Spring Cloud Spring Boot
Angel版本 兼容Spring Boot 1.2.x
Brixton版本 兼容Spring Boot 1.3.x,也兼容Spring Boot 1.4.x
Camden版本 兼容Spring Boot 1.4.x,也兼容Spring Boot 1.5.x
Dalston版本、Edgware版本 兼容Spring Boot 1.5.x,不兼容Spring Boot 2.0.x
Finchley版本 兼容Spring Boot 2.0.x,不兼容Spring Boot 1.5.x
Greenwich版本 兼容Spring Boot 2.1.x

这里采用Finchley.SR2版本 springboot版本改成 2.1.3.RELEASE

完整的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eureka1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eureka1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!-- 最新版的 eureka 服务端包 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<!-- 监控管理 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>
package com.example.demo;

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

@EnableEurekaServer
@SpringBootApplication
public class Eureka1Application {

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

}

然后在代码文件中添加@EnableEurekaServer注解

修改application.yml文件

server:
 port: 3000 # 端口
eureka:
 instance:
 hostname: eureka-center 
 appname: 注册中心
 client:
 registerWithEureka: false # 单点的时候设置为 false 禁止注册自身
 fetchRegistry: false
 serviceUrl:
 defaultZone: http://localhost:3000/eureka
 server:
 enableSelfPreservation: false
 evictionIntervalTimerInMs: 4000

在这里插入图片描述

启动服务

浏览器输入 http://127.0.0.1:3000

在这里插入图片描述

证明注册中心搭建成功了

二、服务提供者

前面的步骤一样

在这里插入图片描述

ProviderController是新建的一个服务代码

<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-web</artifactId>
		</dependency>
		<!-- eureka 客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

不同的地方有加了一个spring-boot-starter-web和spring-cloud-starter-netflix-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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eurekaClient1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eurekaClient1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<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-web</artifactId>
		</dependency>
		<!-- eureka 客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

EurekaClient1Application

package com.example.demo;

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

@EnableEurekaClient
@SpringBootApplication
public class EurekaClient1Application {

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

}

ProviderController

package com.example.demo;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

 @RequestMapping(value = "/hello")
 public String hello(){
 
 return "hello spring cloud!";
 }

 @RequestMapping(value = "/nice")
 public String nice(){
 
 return "nice to meet you!";
 }
}

application.yml

server:
 port: 3001

eureka:
 instance:
 preferIpAddress: true
 client:
 serviceUrl:
 defaultZone: http://localhost:3000/eureka ## 注册到 eureka 
spring:
 application:
 name: single-provider ## 应用程序名称,后面会在消费者中用到

在这里插入图片描述

http://127.0.0.1:3001/hello

在这里插入图片描述

http://127.0.0.1:3000/

在这里插入图片描述

证明服务已经注册成功

三、服务消费者

前面还是同上

在这里插入图片描述

<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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- eureka 客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>

pom.xml新增以上内容

完整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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.3.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eurekaClient1</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>eurekaClient1</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>Finchley.SR2</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>
	<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-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>
		<!-- eureka 客户端 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

EurekaClient2Application

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@EnableEurekaClient
@EnableFeignClients
@SpringBootApplication
public class EurekaClient2Application {

	/**
 * 注入 RestTemplate 
 * 并用 @LoadBalanced 注解,用负载均衡策略请求服务提供者
 * 这是 Spring Ribbon 的提供的能力
 * @return
 */
 @LoadBalanced
 @Bean
 public RestTemplate restTemplate() {
 return new RestTemplate(); //用于调用服务对象
 }
	
	public static void main(String[] args) {
		SpringApplication.run(EurekaClient2Application.class, args);
	}

}

ConsumerController

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ConsumerController {
	@Autowired
 private RestTemplate restTemplate;

 private static final String applicationName = "single-provider";//服务注册名

 @RequestMapping(value = "commonRequest")
 public Object commonRequest(){
 String url = "http://"+ applicationName +"/hello";
 String s = restTemplate.getForObject(url,String.class);//Ribbon方式调用服务
 return s;
 }
}

application.yml

server:
 port: 3002
eureka:
 client:
 serviceUrl:
 defaultZone: http://127.0.0.1:3000/eureka ## 注册到 eureka
 instance:
 preferIpAddress: true
spring:
 application:
 name: single-customer

在这里插入图片描述

启动服务
http://127.0.0.1:3002/commonRequest

在这里插入图片描述

返回服务提供者的 hello方法参数。

整个最简单的过程就完成了,需要更好的使用spring cloud 后续需要了解分布式、负载均衡、熔断等概念。在后续章节将一步步的拆分。

加载全部内容

相关教程
猜你喜欢
用户评论