spring cloud的注册中心,-二、Spring Cloud之注册中心 Eureka - client

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力网 👁️ 590 次阅读

import

org.springframework.boot

spring-boot-maven-plugin

eureka-server-8000

eureka-server-8001

eureka-server-8002

zlflovemm

可以看到文件中指定了spring boot 和Spring cloud 等基础依赖的版本,这样保证各个模块版本的一致性。

子模块

接下来我们创建一个eureka-server-8000 的子模块。

pom.xml

pom.xml的内容如下:

xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd”>

4.0.0

cn.quellanan

SpringCloud

1.0.0

com.quellanan.springcloud

eureka-server-8000

1.0.0

eureka-server-8000

eureka project for Spring Boot

org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

可以看到继承了父级pom,额外的增加了 spring-cloud-starter-netflix-eureka-server 的依赖。

@EnableEurekaServer

在启动类中增加@EnableEurekaServer 注解,表示启用Eureka 服务端。

@SpringBootApplication

@EnableEurekaServer

public class EurekaServer8000Application {

public static void main(String[] args) {

SpringApplication.run(EurekaServer8000Application.class, args);

}

}

application.properties

配置文件中增加如下配置

spring.application.name=spring-cloud-eureka-server-8000

server.port=8000

#表示是否将自己注册到Eureka Server,默认为true。 拼多多帮助力入口

eureka.client.register-with-eureka=true

表示是否从Eureka Server获取注册信息,默认为true。

eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。

eureka.client.serviceUrl.defaultZone=http://localhost:${server.port}/eureka/

现在我们就可以启动项目看看

可以看到我们将自身注册到了服务中。

Eureka 客户端

前面说了,服务提供者和服务消费者都是客户端,其实就是我们具体的某一业务的项目。所以我们再创建一个子模块。我这里分开吧,我们分别创建服务提供者和服务消费者。

服务提供者

我们创建一个eureka-client-provider的子模块,pom 文件中引入spring-cloud-starter-netflix-eureka-client。

cn.quellanan

SpringCloud

1.0.0

groupId>com.quellanan.springcloud

eureka-client-provider

1.0.0

eureka-client-provider

eureka-client-provider 服务提供者

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

启动类中加入@EnableEurekaClient注解或者@EnableDiscoveryClient注解都可以。

@SpringBootApplication

@EnableDiscoveryClient

public class EurekaClientProviderApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaClientProviderApplication.class, args);

}

}

application.properties 中增加如下配置

server.port=9000

#服务名,在注册时所用

spring.application.name=eureka-client-provider

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

这里指定的eureka的服务中心的地址为8000。如上配置就可以将服务注册到注册中心啦。

我们在写一个测试接口。

创建一个IndexController 类,内容如下:

@RestController

public class HelloController {

@RequestMapping(“/hello”)

public String hello(){

return "hello world ";

}

}

服务消费者

我们一样的创建一个 eureka-client-consumer的模块。pom文件如下:

cn.quellanan

SpringCloud

1.0.0

com.quellanan.springcloud

eureka-client-consumer

1.0.0

eureka-client-consumer

eureka-client-consumer 服务消费者

org.springframework.boot

spring-boot-starter-web

org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

org.springframework.cloud

spring-cloud-starter-openfeign

相对于服务提供者,我们增加了Feign 依赖,主要用来发现服务和实现客户端负载均衡,我们这里用来发现服务就可以了。

在启动类中@EnableDiscoveryClient 用来发现服务,并注入RestTemplate 的实例bean 用来对服务提供的接口进行调用。@LoadBalanced 是开启客户端负载均衡的,最开始我没有加这个注解,但是发现不加的话,服务消费者就不能通过服务名来获取可用的服务提供者的实例。所以这里大家可以试验一下。

@EnableDiscoveryClient

@SpringBootApplication

public class EurekaClientConsumerApplication {

@Bean

@LoadBalanced

public RestTemplate restTemplate(){

return new RestTemplate();

}

public static void main(String[] args) {

SpringApplication.run(EurekaClientConsumerApplication.class, args);

}

}

我们接下写一个接口,调用服务消费者,我们创建一个IndexController,内容如下:

@RestController

public class IndexController {

private static final String applicationName = “eureka-client-provider”;

@Autowired

private RestTemplate restTemplate;

@RequestMapping(“/index”)

public String getHello(){

String url = “http://”+ applicationName +“/hello”;

return restTemplate.getForObject(url,String.class);

}

}

这里我们可以看到applicationName 就是服务提供者的服务名。实际中,一种类型的服务可能有好几台服务器,可能物理地址和ip不一样,但是保证他们的服务名一样就可以了,这样服务消费者就可以通过服务名获取可用的列表,再通过复杂均衡策略选择其中一个实例访问。

最后我们在application中加上如下配置:

server.port=9001

#服务名,在注册时所用

spring.application.name=eureka-client-consumer

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

测试

现在我们启动注册中心和客户端这两个项目来看下。启动后,我们输入

http://localhost:8000

可以发现我们的客户端已经注册到注册中心啦。服务提供者和服务消费者都已经注册到了注册中心啦。我们再来调接口试试。我们输入如下:

http://localhost:9001/index

可以看到其实获取了9000服务提供者的接口。这样就实现了服务的注册和发现啦,并实现远程调用。

集群模式(高可用)

========================================================================

上面我们我们搭建的注册中心只是单机模式,只有一个Eureka 服务端,单实际应用中注册中心其实是尤为重要的,所以就需要搭建集群环境,其实Eureka 对集群模式是天然的支持的,我们搭建也非常简单。

为什么这么说呢,我们前面可以看到只要配置了eureka.client.serviceUrl.defaultZone 就就会被对应的注册中线检测到,所以我们代码完全一样,只需要将eureka.client.serviceUrl.defaultZone相互指引就可以了,就就可以简单的搭建一个高可用的环境。

下面我们来搭建一个,因为我们就一台服务器,所以就用不同的端口,其实代码完全一样的,只是配置文件中配置不一样,我们分别把三个分配置文件贴出来。

8000端口的

spring.application.name=spring-cloud-eureka-server-8000

server.port=8000

#表示是否将自己注册到Eureka Server,默认为true。

eureka.client.register-with-eureka=true

表示是否从Eureka Server获取注册信息,默认为true。

eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。

eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/,http://localhost:8002/eureka/

8001端口:

spring.application.name=spring-cloud-eureka-server-8001

server.port=8001

#表示是否将自己注册到Eureka Server,默认为true。

eureka.client.register-with-eureka=true

表示是否从Eureka Server获取注册信息,默认为true。

eureka.client.fetch-registry=true

#设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。多个地址可使用 , 分隔。

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/,http://localhost:8002/eureka/

8002 端口

spring.application.name=spring-cloud-eureka-server-8002

server.port=8002

#表示是否将自己注册到Eureka Server,默认为true。

eureka.client.register-with-eureka=true

表示是否从Eureka Server获取注册信息,默认为true。

eureka.client.fetch-registry=true

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧