使用IDEA搭建SpringCloud项目

本文详细介绍了如何通过Spring Cloud创建Eureka Server,并指导读者如何配置生产者和消费者以实现服务注册与发现。步骤包括创建EurekaServer模块,修改配置文件,添加相关注解,以及运行和测试服务之间的交互。

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

创建Eurake Server

1、选中项目右击–>New–>Module

勾选Web->Spring Web

 勾选Spring Cloud Discovery->Eureka Server

2、修改application.properties文件

server.port=8761
eureka.instance.hostname=localhost
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

3、修改启动类DemocloudApplication

添加@EnableEurekaServer,该注解表明标注类是一个Eureka Server。

@SpringBootApplication
@EnableEurekaServer
public class DemocloudApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemocloudApplication.class, args);
    }
}

4、启动项目

在浏览器中输入http://localhost:8761/

创建生产者

1、勾选Spring Cloud Discovery->Eureka Discovery Client

2、修改application.properties文件

server.port=7901
spring.application.name=demo-user
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
logging.level.root=INFO

3、修改启动类DemoclientApplication

添加@EnableEurekaClient,该注解表明标注类是一个生产者。

@SpringBootApplication
@EnableEurekaClient
public class DemoclientApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoclientApplication.class, args);
    }
}

4、创建Controller

@RestController
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/sayHello")
    public String sayhello(){
        return "I`m provider 1 ,Hello consumer!";
    }
}

5、运行服务

在浏览器中输入http://localhost:7901/user/sayHello

创建消费者

1、勾选Spring Cloud Discovery->Eureka Discovery Client

2、修改application.properties文件

server.port=7902
spring.application.name=demo-guest
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
logging.level.root=INFO

3、修改启动类DemoguestApplication

添加@EnableDiscoveryClient,该注解表明标注类是一个消费者。

@SpringBootApplication
@EnableDiscoveryClient
public class DemoguestApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoguestApplication.class, args);
    }

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

4、创建Controller

@RestController
public class GuestController {
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String hello(){
        //服务地址 http://{服务提供者应用名名称}/{具体的controller}
        String url="http://DEMO-USER/user/sayHello";
        //返回值类型和我们的业务返回值一致
        return restTemplate.getForObject(url, String.class);
    }
}

5、运行服务

在浏览器中输入http://localhost:7902/hello

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值