Spring Cloud

今天我开始学习Spring Cloud的第一天,了解了什么是Spring Cloud,以及Spring Cloud的核心组件和功能。

 

首先,我们需要创建一个Eureka Server,用于服务的注册和发现。

 

@SpringBootApplication

@EnableEurekaServer

public class EurekaServerApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(EurekaServerApplication.class, args);

    }

}

 

 

然后,我们需要创建一个服务提供者,将其注册到Eureka Server上。

 

@SpringBootApplication

@EnableDiscoveryClient

public class ProviderApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(ProviderApplication.class, args);

    }

}

 

@RestController

class HelloController {

 

    @GetMapping("/hello")

    public String hello() {

        return "Hello, world!";

    }

}

 

 

最后,我们需要创建一个服务消费者,从Eureka Server上发现服务,并调用服务提供者提供的服务。

 

@SpringBootApplication

@EnableDiscoveryClient

public class ConsumerApplication {

 

    public static void main(String[] args) {

        SpringApplication.run(ConsumerApplication.class, args);

    }

}

 

@RestController

class HelloController {

 

    @Autowired

    private RestTemplate restTemplate;

 

    @GetMapping("/hello")

    public String hello() {

        String url = "http://provider-service/hello";

        return restTemplate.getForObject(url, String.class);

    }

}

 

@Configuration

class Config {

 

    @LoadBalanced

    @Bean

    public RestTemplate restTemplate() {

        return new RestTemplate();

    }

}

 

以上三个应用中,Eureka Server用于服务注册和发现,服务提供者提供服务并将服务注册到Eureka Server上,服务消费者从Eureka Server上发现服务,并调用服务提供者提供的服务。

posted @   ITJAMESKING  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示