一、Eureka Server
1.1、项目new-module-Spring Initializr,现在jdk仅支持17及以上版本

1.2、注意Spring Boot版本,勾选Eureka Server,点击Create

1.3、配置该模块下的application文件
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:8761/eureka
4、Application类使用 @EnableEurekaServer,否则会报错
Cannot invoke "org.springframework.cloud.netflix.eureka.CloudEurekaClient.getApplications()" because the return value of "org.springframework.cloud.netflix.eureka.serviceregistry.EurekaRegistration.getEurekaClient()" is null

二、Eureka Discovery Client
2.1本次操作从 start.spring.io 中下载,添加的依赖有Eureka Discovery Client、MyBatis Framework、MySQL Driver、Spring Web,点击GENERATE生成压缩文件

2.2解压压缩文件,复制到工作空间的项目中,idea将自动导入(此时两个项目还没有相关性,需要添加到mvn列表里);在idea界面maven project的“+”,选择文件,package有蓝光表示导入成功。


2.3在Application.java添加@EnableDiscoveryClient

2.4、配置该模块下的application文件
2.5、启动Eureka Server、Eureka Client,输入网址localhost:8761

三、feign
3.1、重复1.1步骤(注意name)

3.2、选择dependencies

3.3、在Application.java添加@SpringBootApplication、@EnableDiscoveryClient、@EnableFeignClients

3.4、配置该模块下的application文件
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
spring:
application:
name: base
cloud:
openfeign:
client:
config:
feignName:
connect-timeout: 5000
read-timeout: 5000
3.5、测试
a、templates下编写index.html,发送test1.do请求,
b、请求到达TestAction类,在类上用@RestController或@Controller处理,在方法上使用@RequestMapping标注访问路径,
c、运行BaseApplication类
d、访问localhost:8081,点击test1,显示“ok”,成功


![]()

SpringBoot微服务实战:EurekaServer与Feign集成
本文介绍了如何使用SpringBoot构建微服务架构,包括设置EurekaServer,添加EurekaDiscoveryClient,以及集成Feign进行服务调用。详细步骤涉及新模块创建、依赖管理、配置和测试等内容。
505






