β

原 荐 Spring Cloud之Eureka注册中心及集群

Harries Blog™ 3 阅读

创建项目

创建的 网站 http ://s tar t. spring .io/

创建两个 springboot 工程,一个作为 注册中心 ,一个作为 测试 客户端,注意要导入(eureka-server),创建的界面如下

原 荐 Spring Cloud之Eureka注册中心及集群

也可以用 IDE A 来创建

原 荐 Spring Cloud之Eureka注册中心及集群

依赖的 配置

<?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.ashuo</groupId>
	<artifactId>eureka-service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eureka-service</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.15.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.SR4</spring-cloud.version>
	</properties>

	<dependencies>
		<!---->
		<!--<dependency>-->
			<!--<groupId>org.springframework.cloud</groupId>-->
			<!--<artifactId>spring-cloud-starter-config</artifactId>-->
		<!--</dependency>-->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka-server</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>

在spring boot工程的入口类中加入@Enable Eureka Server,标注好这是注册中心

@EnableEurekaServer
@SpringBootApplication
public class EurekaServiceApplication {

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

资源的配置

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

eureka.instance.hostname=localhost

#不要向注册中心注册自己
#eureka.client.register-with-eureka=false
eureka.client.register-with-eureka=true
#禁止检索服务
#eureka.client.fetch-registry=false
eureka.client.fetch-registry=true
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

再新建一个客户端项目,注意依赖配置

<?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.ashuo</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.15.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.SR4</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>
	</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>
server.port=9001
spring.application.name=eureka-client

eureka.client.service-url.defaultZone=http://localhost:3333/eureka

在spring boot工程的入口类中加入@EnableDiscoveryClient

@EnableDiscoveryClient
@SpringBootApplication
public class EurekaClientApplication {

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

写一个小的测试类

@RestController
public class TestRestful {

    private final Logger logger= Logger.getLogger(getClass());

    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String home() {
        logger.info("进入home");
        return "hello eureka";
    }
}

接下来就来验证下

原 荐 Spring Cloud之Eureka注册中心及集群

原 荐 Spring Cloud之Eureka注册中心及集群

这个一个简单的eureka 的demo就成功了,下面我们再来看看 集群 的方式

在前面的eureka-server上增加两个配置文件 application-peer1.properties ,application-peer2.properties

application-peer1.properties

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

eureka.instance.hostname=peer1
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka
#eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
application-peer2.properties

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

eureka.instance.hostname=peer2

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

#eureka.client.service-url.defaultZone=http://peer1:1111/eureka/
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka

再修改下eureka-client 的配置文件

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

#eureka.client.service-url.defaultZone=http://localhost:3333/eureka

eureka.client.service-url.defaultZone=http://peer1:1111/eureka,http://peer2:1112/eureka

启动的时候要注意下

原 荐 Spring Cloud之Eureka注册中心及集群

修改hosts文件,文件地址为C:/Windows/System32/drivers/etc

.0.0.1 peer1
.0.0.1 peer2

最后验证集群,然后断掉其中一个,另一个服务"Instances currently registered with Eureka"就会变得有 实例

原 荐 Spring Cloud之Eureka注册中心及集群

原 荐 Spring Cloud之Eureka注册中心及集群

原文

https ://my.oschina.net/saulc/blog/2050824

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » 原 荐 Spring Cloud之Eureka注册中心及集群

作者:Harries Blog™
追心中的海,逐世界的梦
原文地址:原 荐 Spring Cloud之Eureka注册中心及集群, 感谢原作者分享。

发表评论