赞
踩
公司需要对提高项目的响应速度,经研究决定使用eureka微服务改造项目,于是自己做了一个小demo进行学习。
一、eureka简介
Eureka是Netflix开发的服务发现框架,本身是一个基于REST的服务,主要用于定位运行在AWS域中的中间层服务,以达到负载均衡和中间层服务故障转移的目的。SpringCloud将它集成在其子项目spring-cloud-netflix中,以实现SpringCloud的服务发现功能。Eureka采用cs架构:Eureka Server和Eureka Client,即服务端和客户端。
客户端将服务注册到服务端,客户端之间可以通过eureka服务的注册信息进行调用。客户端可以同时注册多个eureka服务,从而实现高可用,客户端可以发布多个到eureka上实现负载均衡。
二、Eureka Serve服务端
1、首先构建一个springboot项目,并在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 http://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.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>cn.itcast</groupId>
- <artifactId>eureka</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>eureka</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
- </properties>
-
- <dependencies>
- <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-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>
-
- <repositories>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- </repository>
- </repositories>
- </project>
2、配置application.yml
- server:
- port: 8761
- eureka:
- client:
- #是否向服务注册中心注册自己
- register-with-eureka: false
- #是否启用获取服务注册信息,不需要同步其他的Eureka Server节点的数据,故而设为false
- fetch-registry: false
3、主函数类中添加注解EurekaApplication
- @EnableEurekaServer
- @SpringBootApplication
- public class EurekaApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(EurekaApplication.class, args);
- }
-
- }
4、启动主函数就可以访问eureka注册中心了http://localhost:8761/
三、Eureka Client客户端
首先创建一个生产者,即服务的提供者,生产者和消费者没有绝对的区分,只是在服务调用的时候扮演的不同角色。
1、首先构建一个springboot项目,并在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 http://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.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>cn.vtits</groupId>
- <artifactId>provider</artifactId>
- <version>1.0.0-SNAPSHOT</version>
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </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>
- <defaultGoal>compile</defaultGoal>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <repositories>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- </repository>
- </repositories>
- </project>
2、配置application.yml
- server:
- port: 8762
- spring:
- application:
- name: hello-service
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka #注册eureka的地址,由于eureka在本地
3、主函数添加注解
- @Configuration
- @EnableAutoConfiguration
- @ComponentScan
- public class ProviderApplication {
- public static void main(String[] args) {
- SpringApplication.run(ProviderApplication.class, args);
- }
- }
4、添加测试用例
- @RestController
- public class MainController {
-
- @GetMapping("/test/{name}")
- String home(@PathVariable("name") String t) {
- return "Hello World!"+t;
- }
- }
5、启动主函数打开eureka注册中心
看到我们的服务注册到eureka服务上了。
访问http://localhost:8762/test/JAVA
创建一个服务消费者
6、构建一个springboot项目,并在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 http://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.1.RELEASE</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>cn.vtits</groupId>
- <artifactId>provider</artifactId>
- <version>1.0.0-SNAPSHOT</version>
-
- <properties>
- <java.version>1.8</java.version>
- <spring-cloud.version>Greenwich.RC2</spring-cloud.version>
- </properties>
-
- <dependencies>
- <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>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </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>
- <defaultGoal>compile</defaultGoal>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- </plugins>
- </build>
- <repositories>
- <repository>
- <id>spring-milestones</id>
- <name>Spring Milestones</name>
- <url>https://repo.spring.io/milestone</url>
- </repository>
- </repositories>
- </project>
7、主函数添加注解
- @SpringBootApplication
- @EnableDiscoveryClient
- @EnableFeignClients
- public class ConsumerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConsumerApplication.class, args);
- }
- }
8、配置application.yml
- server:
- port: 8763
- spring:
- application:
- name: consumer
-
- eureka:
- client:
- service-url:
- defaultZone: http://localhost:8761/eureka
9、创建ServiceFeignClient类,作为服务调用接口
- //name为生产者提供的服务名称
- @FeignClient(name = "hello-service" )
- public interface ServiceFeignClient {
-
- //这里接口要与被生产者home一致
- @GetMapping("/test/{name}")
- String home(@PathVariable("name") String name);
- }
10、编写ConsumerController,调用ServiceFeignClient
- @RestController
- public class ConsumerController {
- @Autowired
- ServiceFeignClient serviceFeignClient;
-
- @GetMapping("/test1/{name}")
- public String index(@PathVariable("name") String name) {
- return serviceFeignClient.home(name);
- }
- }
11、启动主函数打开eureka注册中心
12、访问http://localhost:8763/test1/JDK
这样消费者就从eureka服务调用到了eureka生产者的服务。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。