SpringCloud之Eureka入门案例(附源码地址)
Posted ITdfq
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringCloud之Eureka入门案例(附源码地址)相关的知识,希望对你有一定的参考价值。
创建服务提供者
-
添加pom文件
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itdfq</groupId> <artifactId>EurekaServer</artifactId> <version>0.0.1-SNAPSHOT</version> <name>EurekaServer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.3</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-netflix-eureka-server</artifactId> </dependency> <!-- 添加权限验证 security--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</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>
-
添加配置
server: # 注意这里的eureka注册是个模板,在搭建集群的时候会被多次复制,其中Eureka01的端口号是:8091,Eureka02的端口号是8092。注意修改 port: 8090 spring: application: # 服务注册列表中的应用名称,会在Eureka中显示(注意中间不要以下划线连接,否则会注册错误。) name: eureka-server #添加security密码 security: user: name: root password: 123456 eureka: client: # 是否注册自己的信息到EurekaServer,默认是true。搭建集群的时候需要互相注册,提高项目的高可用性。在用Dashboard模板时,重新修改配置文件部署在Tomcat相当于把原来的复制了一份 register-with-eureka: true # 是否拉取其它服务的信息,默认是true,同上 fetch-registry: true # EurekaServer的地址,现在是自己的地址,如果是集群,需要加上其它Server的地址。 service-url: # 注意搭建eureka集群的时候,要相互注册,需要修改此处配置,这里暂时不搭建集群,注册在自己的服务列表中 # 默认是http://127.0.0.1:8090/eureka 这里采用了security权限认证,加入了用户名和密码 defaultZone: http://root:123456@127.0.0.1:8090/eureka # defaultZone: http://127.0.0.1:${server.port}/eureka # 这个是自动获取当前端口号的注册,yml语法${}
-
开启功能
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
-
启动项目,访问
http://localhost:8090/
服务提供者
-
pom坐标
<?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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.2</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.itdfq</groupId> <artifactId>provider</artifactId> <version>0.0.1-SNAPSHOT</version> <name>provider</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2020.0.3</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-netflix-eureka-client</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>
-
配置
spring.application.name= eureka-client-user-service server.port=8081 eureka.client.serviceUrl.defaultZone=http://root:123456@localhost:8090/eureka/ # 采用IP注册 eureka.instance.preferIpAddress=true # 定义实例ID格式 eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port}
-
提供服务
@RestController public class ProviderController { @RequestMapping("/test") public String test(){ return "hello world"; } }
-
注意:这里注册服务的时候需要添加密码,因为采用了security权限
-
访问接口测试
服务消费者
-
pom与服务提供者一样
-
配置
``` spring.application.name=eureka-client-article-service server.port=8082 eureka.client.serviceUrl.defaultZone=http://root:123456@localhost:8090/eureka/ # 采用IP注册 eureka.instance.preferIpAddress=true # 定义实例ID格式 eureka.instance.instance-id=${spring.application.name}:${spring.cloud.client.ip-address}:${server.port} ```
-
配置类
package com.itdfq.consumer.config; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; /** * @Author GocChin * @Date 2021/7/19 16:41 * @Blog: itdfq.com * @QQ: 909256107 * @Description: RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端, * RestTemplate 提供了多种便捷访问远程 Http 服务的方法,能够大大提高客户端的编写效率 */ @Configuration public class BeanConfiguration { @Bean @LoadBalanced public RestTemplate getRestTemplate() { return new RestTemplate(); } }
-
方法调用
@GetMapping("/article/callHello2") public String callHello2() { System.out.println("运行了方法二"); return restTemplate.getForObject("http://eureka-client-user-service/test", String.class); }
-
结果
源代码:gitee
Eureka集群搭建
以上是关于SpringCloud之Eureka入门案例(附源码地址)的主要内容,如果未能解决你的问题,请参考以下文章
Java 微服务之 SpringCloud快速入门day01 Eureka注册中心快速入门