欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理

发布时间:2025/3/19 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

服务治理可以说是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现。

Spring Cloud Eureka是Spring Cloud Netflix 微服务套件的一部分,主要负责完成微服务架构中的服务治理功能。

本文通过简单的小例子来分享下如何通过Eureka进行服务治理:

搭建服务注册中心

注册服务提供者

服务发现和消费

==========我是华丽的分割线========================

一、搭建服务注册中心

先列出完整目录结构:

搭建过程如下:

创建maven工程:eureka(具体实现略)

修改pom文件,引入依赖

4.0.0

com.sam

eureka

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.8

org.springframework.cloud

spring-cloud-dependencies

Camden.SR6

pom

import

org.springframework.cloud

spring-cloud-starter-eureka-server

创建启动类

/***

* @EnableEurekaServer

* 用来指定该项目为Eureka的服务注册中心*/@EnableEurekaServer

@SpringBootApplicationpublic classEurekaApp {public static voidmain(String[] args) {

SpringApplication.run(EurekaApp.class, args);

}

}

配置application.properties文件

#设置tomcat服务端口号

server.port=1111#设置服务名称

spring.application.name=eureka-service

eureka.instance.hostname=localhost

#注册中心不需要注册自己

eureka.client.register-with-eureka=false#注册中心不需要去发现服务

eureka.client.fetch-registry=false#设置服务注册中心的URL

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

启动服务并访问,我们会看到这样的画面:

二、注册服务提供者

先列出完整目录结构:

搭建过程如下:

创建maven工程:hello-service(具体实现略)

修改pom文件,引入依赖

4.0.0

com.sam

hello-service

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.8

org.springframework.cloud

spring-cloud-dependencies

Camden.SR6

pom

import

org.springframework.cloud

spring-cloud-starter-eureka

创建启动类

/***

*

* @EnableDiscoveryClient

* 让服务使用eureka服务器

* 实现服务注册和发现

**/@EnableDiscoveryClient

@SpringBootApplicationpublic classHelloApp {public static voidmain(String[] args) {

SpringApplication.run(HelloApp.class, args);

}

}

创建controller

@RestControllerpublic classHelloController {

Logger logger= LoggerFactory.getLogger(HelloController.class);

@Autowired

DiscoveryClient discoveryClient;

@RequestMapping("/hello")publicString hello() {

ServiceInstance instance=discoveryClient.getLocalServiceInstance();//打印服务的服务id

logger.info("*********" +instance.getServiceId());return "hello,this is hello-service";

}

}

配置application.properties文件

server.port=9090#设置服务名

spring.application.name=hello-service

#设置服务注册中心的URL,本服务要向该服务注册中心注册自己

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

启动并测试)启动后再hello-service的控制台会有这种字样(xxx代表你的PC名)

Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

eureka的控制台会打印出如下字样(xxx代表你的PC名)

Registered instance HELLO-SERVICE/xxx:hello-service:9090 with status UP (replication=false)

)再次访问localhost:1111,会发现有服务注册到注册中心了

三、服务发现和消费

完整目录结构如下:

搭建过程:

创建maven工程(具体实现略)

修改pom文件,引入依赖

4.0.0

com.sam

hello-consumer

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-starter-parent

1.5.1.RELEASE

1.8

org.springframework.cloud

spring-cloud-dependencies

Camden.SR6

pom

import

org.springframework.cloud

spring-cloud-starter-eureka

org.springframework.cloud

spring-cloud-starter-ribbon

这里比hello-service服务提供者,多了ribbon的依赖

创建启动类

@EnableDiscoveryClient

@SpringBootApplicationpublic classConsumerApp {//@Bean 应用在方法上,用来将方法返回值设为为bean

@Bean

@LoadBalanced//@LoadBalanced实现负载均衡

publicRestTemplate restTemplate() {return newRestTemplate();

}public static voidmain(String[] args) {

SpringApplication.run(ConsumerApp.class, args);

}

}

这里也要用到@EnableDiscoveryClient, 让服务使用eureka服务器, 实现服务注册和发现

创建controller

@RestControllerpublic classConsumerController {//这里注入的restTemplate就是在com.sam.ConsumerApp中通过@Bean配置的实例

@Autowired

RestTemplate restTemplate;

@RequestMapping("/hello-consumer")publicString helloConsumer() {//调用hello-service服务,注意这里用的是服务名,而不是具体的ip+port

restTemplate.getForObject("http://hello-service/hello", String.class);return "hello consumer finish !!!";

}

}

配置application.properties文件

server.port=9999spring.application.name=hello-consumer

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

#这里的配置项目和服务提供者hello-service一样

启动,测试)启动eureka。为了展示负责均衡的效果,我们的hello-service启动两个服务,启动两个服务的具体步骤如下

以上是hello-service1的启动步骤,端口号为9090;同样方法设置hello-service2,端口号为9091(具体实现略)。

)启动hello-consumer

)再次访问http://localhost:1111/,会发现有2个hello-service服务(端口号一个是9090,一个是9091),1个hello-consume服务

)   多次访问http://localhost:9999/hello-consumer,会发现hello-service1和hello-service2会轮流被调用(已经实现了负责均衡),可以通过两者的控制台打印内容确认(还记得我们在hello-service的controller中有个loggerlogger.info("*********" + instance.getServiceId());吗?对,就是这个打印)

四、总结

以上实例实现了基本的服务治理:

通过spring-cloud-starter-eureka-server和@EnableEurekaServer实现服务注册中心

通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用并注册到服务注册中心

通过spring-cloud-starter-eureka和@EnableDiscoveryClient使用注册中心并发现服务,通过spring-cloud-starter-ribbon来实现负载均衡消费服务

PS:这里说明下,我用的IDE是Spring Tool Suite,是spring定制版的eclipse,方便我们使用spring进行开发,有兴趣的朋友可以自行百度了解下。

总结

以上是生活随笔为你收集整理的eureka java_spring cloud 入门系列二:使用Eureka 进行服务治理的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。