Eureka项目搭建、数据请求
- Eureka简介
- Eureka项目创建
- 1、新建Maven项目
- 2、只保留Maven项目的依赖文件
- 3、创建子模块(Eureka服务模块)
- 4、修改`pom.xml`
- 5、创建并修改配置文件
- 6、添加Eureka注解
- 7、运行
- 8、创建其他服务
- 9、修改`pom.xml`
- 10、创建并修改配置文件
- 11、添加Eureka注解
- 12、添加项目服务
- 13、运行
- 14、访问404解决
- 15、项目目录结构
- 搭建Eureka集群
- 1、准备
- 2. 创建 Eureka Server 模块
- 3、运行
- 4、创建 Eureka Client 模块并注册到Eureka服务中
- 5、运行
- 6、项目目录结构
- 网络请求获取数据
- 1、 创建项目
- 2、 创建请求
- 3、 运行
- 4、 目录结构
- 利用Eureka实现服务间数据请求
- 1、 创建项目
- 2、 添加依赖
- 3、 `demo/eureka-server`
- 4、 `demo/service-01`
- 5、 `demo/service-02`
- 6、 运行
- 7、 目录结构
Eureka简介
Eureka采用的是Server/Client的模式进行设计。
Server扮演了服务注册中心的角色,为Client提供服务注册和发现的功能,维护着注册到自身的Client的相关信息,同时提供接口给Client获取到注册表中其他服务的信息。
Client将有关自己的服务的信息通过一定的方式登记到Server上,并在正常范围内维护自己信息的一致性,方便其他服务发现自己,同时可以通过Server获取到自己的依赖的其他服务信息,从而完成服务调用。
Eureka功能主要包括:
服务注册
、服务续约
、服务剔除
、服务下线
、获取注册表信息
、远程调用
等。
- 以下是Eureka几个角色的解释:
Eureka服务端
:负责服务注册、发现并管理每项服务的中心。Eureka实例
:服务(如订单系统)部署多个服务器,每个服务器上提供的服务都是实例。Eureka服务
:指提供特定服务功能的服务,例如:订单系统,同一服务可以提供多个实例;Eureka客户端
:主要向服务中心注册自己成为服务。但它既可以是服务提供者,也可以是消费者。它与Eureka实例感觉相似,但实际上意义不同。
Eureka项目创建
1、新建Maven项目
2、只保留Maven项目的依赖文件
pom.xml
文件作为后续创建的子模块依赖的父依赖文件
3、创建子模块(Eureka服务模块)
4、修改pom.xml
demo/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.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>demo-eureka</module></modules><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version><spring-cloud.version>2021.0.5</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
demo/demo-eureka/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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-eureka</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-eureka</name><description>demo-eureka</description></project>
5、创建并修改配置文件
demo/demo-eureka/src/main/resources/application.yml
server:port: 8000spring:application:name: demo-eurekaeureka:instance:hostname: 127.0.0.1client:register-with-eureka: false # 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry: false # 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心地址
6、添加Eureka注解
demo/demo-eureka/src/main/java/com/example/demo/demoeureka/DemoEurekaApplication.java
package com.example.demo.demoeureka;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class DemoEurekaApplication {public static void main(String[] args) {SpringApplication.run(DemoEurekaApplication.class, args);}}
7、运行
8、创建其他服务
9、修改pom.xml
demo/pom.xml
<packaging>pom</packaging><modules><module>demo-eureka</module><module>demo-one</module><module>demo-two</module>
</modules>
demo/demo-one/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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-one</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-one</name><description>demo-one</description></project>
demo/demo-two/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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>demo-two</artifactId><version>0.0.1-SNAPSHOT</version><name>demo-two</name><description>demo-two</description></project>
10、创建并修改配置文件
把服务注册到Eureka中
demo/demo-one/src/main/resources/application.yml
server:port: 8001spring:application:name: demo-oneeureka:client:service-url:defaultZone: http://127.0.0.1:8000/eureka/
demo/demo-two/src/main/resources/application.yml
server:port: 8002spring:application:name: demo-twoeureka:client:service-url:defaultZone: http://127.0.0.1:8000/eureka/
11、添加Eureka注解
demo/demo-one/src/main/java/com/example/demo/demoone/DemoOneApplication.java
package com.example.demo.demoone;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class DemoOneApplication {public static void main(String[] args) {SpringApplication.run(DemoOneApplication.class, args);}}
demo/demo-two/src/main/java/com/example/demo/demotwo/DemoTwoApplication.java
package com.example.demo.demotwo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient
@SpringBootApplication
public class DemoTwoApplication {public static void main(String[] args) {SpringApplication.run(DemoTwoApplication.class, args);}}
12、添加项目服务
13、运行
14、访问404解决
开启监控服务配置demo/demo-one/src/main/resources/application.yml
开启监控服务配置demo/demo-two/src/main/resources/application.yml
management:endpoints:web:exposure:include: "*"
15、项目目录结构
搭建Eureka集群
应对服务挂了找不到服务的问题
1、准备
SwitchHosts
:用于修改 IP 和 域名 的映射https://github.com/oldj/SwitchHosts/releases
2. 创建 Eureka Server 模块
demo/pom.xml
<packaging>pom</packaging><modules><module>eureka-server-01</module><module>eureka-server-02</module><module>eureka-server-03</module><module>eureka-client-01</module>
</modules>
demo/eureka-server-01
、demo/eureka-server-02
、demo/eureka-server-03
- 启动类
demo/eureka-server-01/src/main/java/com/example/demo/eurekaserver01/EurekaServer01Application.java
package com.example.demo.eurekaserver01;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer @SpringBootApplication public class EurekaServer01Application {public static void main(String[] args) {SpringApplication.run(EurekaServer01Application.class, args);} }
demo/eu¬reka-server-02/src/main/java/com/example/demo/eurekaserver01/EurekaServer02Application.java
package com.example.demo.eurekaserver02;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer @SpringBootApplication public class EurekaServer02Application {public static void main(String[] args) {SpringApplication.run(EurekaServer02Application.class, args);} }
demo/eureka-server-03/src/main/java/com/example/demo/eurekaserver01/EurekaServer03Application.java
package com.example.demo.eurekaserver03;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer @SpringBootApplication public class EurekaServer03Application {public static void main(String[] args) {SpringApplication.run(EurekaServer03Application.class, args);} }
- 配置项
demo/eureka-server-01/src/main/resources/application.yml
server: port: 8001spring: application: name: eureka-server-01eureka: instance: hostname: www.eureka-server-01.com client: register-with-eureka: false # 是否将自己注册到 Eureka-Server 中,默认的为 true fetch-registry: false # 是否需要拉取服务信息,默认未true service-url:defaultZone: http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
demo/eureka-server-02/src/main/resources/application.yml
server:port: 8002spring:application:name: eureka-server-02eureka:instance:hostname: www.eureka-server-02.comclient:register-with-eureka: false # 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry: false # 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-03.com:8003/eureka/ # 注册中心地址
demo/eureka-server-03/src/main/resources/application.yml
server:port: 8003spring:application:name: eureka-server-03eureka:instance:hostname: www.eureka-server-03.comclient:register-with-eureka: false # 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry: false # 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/ # 注册中心地址
3、运行
4、创建 Eureka Client 模块并注册到Eureka服务中
- 启动类
demo/eureka-client-01/src/main/java/com/example/demo/eurekaclient01/EurekaClient01Application.java
package com.example.demo.eurekaclient01;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@EnableEurekaClient @SpringBootApplication public class EurekaClient01Application {public static void main(String[] args) {SpringApplication.run(EurekaClient01Application.class, args);} }
- 配置项
demo/eureka-client-01/src/main/resources/application.yml
server:port: 8081spring:application:name: eureka-client-01eureka:instance:hostname: www.eureka-client-01.cominstance-id: eureka-client-01client:service-url:defaultZone: http://www.eureka-server-01.com:8001/eureka/,http://www.eureka-server-02.com:8002/eureka/,http://www.eureka-server-03.com:8003/eureka/management:endpoints:web:exposure:include: "*"
5、运行
6、项目目录结构
网络请求获取数据
1、 创建项目
demo/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.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>service-01</module><module>service-02</module></modules><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><spring-boot.version>2.6.13</spring-boot.version></properties><dependencies><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.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.8.1</version><configuration><source>1.8</source><target>1.8</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><id>repackage</id><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build></project>
demo/service-01/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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-01</artifactId><version>0.0.1-SNAPSHOT</version><name>service-01</name><description>service-01</description></project>
demo/service-02/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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><groupId>com.example.demo</groupId><artifactId>demo</artifactId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>service-02</artifactId><version>0.0.1-SNAPSHOT</version><name>service-02</name><description>service-02</description></project>
demo/service-01/src/main/resources/application.yml
server:port: 8001
demo/service-02/src/main/resources/application.yml
server:port: 8002
2、 创建请求
demo/service-01/src/main/java/com/example/demo/service01/TestController.java
package com.example.demo.service01;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping
public class TestController {private final RestTemplate restTemplate = new RestTemplate();@GetMapping("msg")public String msg() {return "service-01 服务中请求 service-02";}@GetMapping("say")public String say() {String html = restTemplate.getForObject("https://www.example.com/", String.class);String msg = restTemplate.getForObject("http://127.0.0.1:8002/msg", String.class);return String.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);}}
demo/service-01/src/main/java/com/example/demo/service01/TestController.java
package com.example.demo.service02;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;@RestController
@RequestMapping
public class TestController {private final RestTemplate restTemplate = new RestTemplate();@GetMapping("msg")public String msg() {return "service-02 服务中请求 service-01";}@GetMapping("say")public String say() {String html = restTemplate.getForObject("https://www.example.com/", String.class);String msg = restTemplate.getForObject("http://127.0.0.1:8001/msg", String.class);return String.format("%s<h1 style=\"text-align: center;\">%s</h1>", html, msg);}}
3、 运行
4、 目录结构
利用Eureka实现服务间数据请求
1、 创建项目
demo/pom.xml
<packaging>pom</packaging><modules><module>eureka-server</module><module>service-01</module><module>service-02</module>
</modules>
2、 添加依赖
demo/pom.xml
<!-- 用于服务间请求的依赖 -->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
3、 demo/eureka-server
demo/eureka-server/src/main/resources/application.yml
server:port: 8001spring:application:name: eureka-servereureka:instance:hostname: www.eureka-server.comclient:register-with-eureka: false # 是否将自己注册到 Eureka-Server 中,默认的为 truefetch-registry: false # 是否需要拉取服务信息,默认未trueservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ # 注册中心地址
demo/eureka-server/src/main/java/com/example/demo/eurekaserver/EurekaServerApplication.java
package com.example.demo.eurekaserver;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {public static void main(String[] args) {SpringApplication.run(EurekaServerApplication.class, args);}}
4、 demo/service-01
demo/service-01/src/main/resources/application.yml
server:port: 8081spring:application:name: service-01eureka:instance:hostname: www.eureka-server.cominstance-id: service-01client:service-url:defaultZone: http://www.eureka-server.com:8001/eureka/management:endpoints:web:exposure:include: "*"
demo/service-01/src/main/java/com/example/demo/service01/Service01Application.java
package com.example.demo.service01;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Service01Application {public static void main(String[] args) {SpringApplication.run(Service01Application.class, args);}}
demo/service-01/src/main/java/com/example/demo/service01/api/IService02.java
package com.example.demo.service01.api;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value = "SERVICE-02")
public interface IService02 {@GetMapping("/getMsg")public String getMsg();
}
demo/service-01/src/main/java/com/example/demo/service01/Service01Controller.java
package com.example.demo.service01;import com.example.demo.service01.api.IService02;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping
public class Service01Controller {@Resourceprivate IService02 service02;@GetMapping("/say")public String say(){return service02.getMsg();}@GetMapping("getMsg")public String getMsg() {return "SERVICE-02 调用 SERVICE-01";}}
5、 demo/service-02
demo/service-02/src/main/resources/application.yml
server:port: 8082spring:application:name: service-02eureka:instance:hostname: www.eureka-server.cominstance-id: service-02client:service-url:defaultZone: http://www.eureka-server.com:8001/eureka/management:endpoints:web:exposure:include: "*"
demo/service-02/src/main/java/com/example/demo/service02/Service02Application.java
package com.example.demo.service02;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class Service02Application {public static void main(String[] args) {SpringApplication.run(Service02Application.class, args);}}
demo/service-02/src/main/java/com/example/demo/service02/api/IService01.java
package com.example.demo.service02.api;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;@FeignClient(value = "SERVICE-01")
public interface IService01 {@GetMapping("/getMsg")public String getMsg();
}
demo/service-02/src/main/java/com/example/demo/service02/Service02Controller.java
package com.example.demo.service02;import com.example.demo.service02.api.IService01;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;@RestController
@RequestMapping
public class Service02Controller {@Resourceprivate IService01 service01;@GetMapping("/say")public String say(){return service01.getMsg();}@GetMapping("getMsg")public String getMsg() {return "SERVICE-01 调用 SERVICE-02";}}
6、 运行
优先运行
EurekaServerApplication
启动类