SpringCloud 微服务的注册与发现(一)

Eureka 是Netflix的一个微服务发现组件,主要实现微服务的发现与注册,好了废话不多说,咱们直接开工,just do it!

一、搭建SpringCloud 的server项目取名叫EurekaServer。

1.1 我是用的开发工具是intellij idea ,使用Spring Intializr创建项目


接下来选择SpringCloud 依赖的Eureka



勾选上Eureka Server,接下来直接Next,直至创建工程结束。

1.2 打开我们的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>eurekaserver</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EurekaServer</name>
	<description>Demo project for Spring Boot</description>

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

	<dependencies>
		<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>

1.3 编写我们的启动类,在启动类上我们添加了@EnableEurekaServer 注解,声明这是一个Eureka Server,代码如下

package com.pdl.eureka.server.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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



}

1.4 在配置文件application.yml中添加如下内容

#服务器端口号
server:
  port: 8761
#添加Eureka配置
eureka:
  instance:
    hostname: localhost
  client:
    #registerWithEureka表示是否将自己注册到Eureka Server,默认为true,由于当前是Eureka Server,故设置为false
    registerWithEureka: false
    #fetchRegistry 表示是否从Eureka Server获取注册信息,默认为true,因为这是一个服务端,故而设置为false
    fetchRegistry: false
    serviceUrl:
      #设置与Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址时可使用逗号分隔
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

1.5 至此我们的Eureka Server 已经搭建完成,启动EurekaServer,访问http://localhost://8761,可看到如下页面


二、创建一个EurekaClient,为服务提供者

2.1 同样使用Spring Intializr创建项目,如下图所示



这次勾选Eureka Discovery,然后一直next,直至项目创建完成

2.2 下面我们来看下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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>eurekaclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>EurekaClient</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.9.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.SR1</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-web</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>

2.3 更改配置文件application.yml

eureka:
  client:
    serviceUrl:
    #设置与Eureka Server交互的地址
      defaultZone: http://localhost:8761/eureka/
  #eureka.instance.prefer-ip-address = true表示将自己的IP注册到Eureka Server,默认为false,
  #false时表示Z注册为服务所在操作系统的hostname到Eureka Server
  instance:
    prefer-ip-address: true
server:
  port: 8762
#spring.application.name用于指定注册到Eureka Server上的应用名称
spring:
  application:
    name: service-hi

2.4 编写启动类,增加@EnableDiscoveryClient 注解,声明这是一个Eureka Client

package com.pdl.eureka.client.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableEurekaClient
@RestController
public class EurekaClientApplication {

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

	@Value("${server.port}")
	String port;

	@RequestMapping("/hi")
	public String home(@RequestParam String name) {
		return "hi "+name+",i am from port:" +port;
	}
}

2.5 启动EurekaClient 项目,此时在EurekaServer 服务器上可以看到你注册的EurekaClient,访问http://localhost:8761


三、 构建EurekaServer 集群

3.1 复制项目EurekaServer 将其更改为EureKaServer2

3.2 修改系统的hosts文件 ,在其文件内添加 127.0.0.1 peer1 peer2

3.3 将EurekaServer项目中的application.yml修改如下

#服务器端口号
server:
  port: 8761
spring:
  profiles:peer1
#添加Eureka配置
eureka:
  instance:
    hostname: peer1
  client:
    serviceUrl:
      #设置与Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址时可使用逗号分隔
      defaultZone: http://peer2:8769/eureka/

3.4 将EurekaServer2项目中的application.yml修改如下

#服务器端口号
server:
  port: 8769
spring:
  profiles:peer2
#添加Eureka配置
eureka:
  instance:
    hostname: peer2
  client:
    #registerWithEureka表示是否将自己注册到Eureka Server,默认为true,由于当前是Eureka Server,故设置为false
    registerWithEureka: false
    #fetchRegistry 表示是否从Eureka Server获取注册信息,默认为true,因为这是一个服务端,故而设置为false
    fetchRegistry: false
    serviceUrl:
      #设置与Eureka Server 交互的地址,查询服务和注册服务都需要依赖这个地址,多个地址时可使用逗号分隔
      defaultZone: http://peer1:8761/eureka/
至此EurekaServer集群已经搭建完成





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值