eureka服务之间调用(3)

本文介绍如何使用 Spring Cloud Eureka 搭建微服务架构,包括配置pom.xml引入所需依赖、配置properties文件指定服务名称及注册中心地址、实现启动类并启用Eureka客户端和服务发现功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

配置pom.xml

<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
			<version>1.2.7.RELEASE</version>
		</dependency>
        <!--表示为去注册中心发现其他服务 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</artifactId>
			<version>1.2.7.RELEASE</version>
		</dependency>
		<!--表示为web工程 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>http://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>http://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</pluginRepository>
	</pluginRepositories>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

配置properties

server:
  port: 8763
spring:
  application:
    name: service-two   这是发布至注册中心的服务名,靠此服务名服务之间相互调用
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka   配置注册中心地址

配置启动类

/**
 * 启动类
 * @author WangD
 *
 */
@EnableDiscoveryClient    //通过注解@EnableDiscoveryClient 表明自己是一个eurekaclient.去服务中心注册
@EnableFeignClients(basePackages="com.eureka.service")  //去注册中心调用服务扫描包范围
@SpringBootApplication(scanBasePackages="com.eureka")
public class CloudApplication2 {
	
	/** 启动 */
	public static void main(String[] args) {
		SpringApplication.run(CloudApplication2.class, args);
	}

}

编写调用其他服务service接口

@FeignClient("service-hi") //调用其服务的服务名  即上一条博客(2)
public interface SchedualService {
	
	@RequestMapping(value="/hi",method=RequestMethod.POST)
	public String gethi();
	
}

编写controller

调用器service接口调用其他服务

@RestController
public class CloudClient {
	
	@Autowired
	private SchedualService schedualService;
	
	@RequestMapping("/testhi")
	public String cloudOne(){
		return schedualService.gethi();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值