Spring Cloud入门实例

建立父工程

新建Maven空项目
删除src文件夹

Eureka注册中心

新建Module SpringBoot项目 eureka-server
并导入Eureka Server
在这里插入图片描述
启动类添加
@EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(EurekaServerApplication.class, args);
	}

}

application.yml

server:
  port: 8080

spring:
  application:
    #注册名称
    name: eureka-server

eureka:
  instance:
    hostname: 127.0.0.1

  client:
    #是否获取eureka服务器的注册信息,默认为true
    fetch-registry: false
    #是否注册到eureka服务器,默认为true
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

启动erueka-server
浏览器访问 http://localhost:8080/
在这里插入图片描述

服务提供者

新建Module SpringBoot项目 provider-server
并勾选Spring Web 、Eureka Discovery Client
在这里插入图片描述

启动类添加
@EnableEurekaClient

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ProviderServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProviderServerApplication.class, args);
	}

}

application.yml

server:
  port: 8181

spring:
  application:
    name: provider-server

eureka:
  client:
    #获取注册服务
    fetch-registry: false
    #注册在eureka上
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8080/eureka

添加Controller

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("learn")
public class MathController {

    @RequestMapping("/math")
    public String learnMath(){
        return "学习数学";
    }
}

启动服务
刷新Eureka
在这里插入图片描述
浏览器访问 http://localhost:8181/learn/math
在这里插入图片描述

消费者服务

新建Module SpringBoot项目 consumer-server
并勾选Spring Web 、Eureka Discovery Client
在这里插入图片描述
启动类添加
@EnableEurekaClient

application.yml

server:
  port: 8282

spring:
  application:
    name: consumer-server

eureka:
  client:
    #获取注册服务
    fetch-registry: true
    #注册在eureka上
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8080/eureka

启动类:
这里调用接口提供两个方法,使用RestTemplate和OpenFeign
方法一:用RestTemplate调用
1.启动类添加 @EnableEurekaClient
2.注入 RestTemplate

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class ConsumerServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerServerApplication.class, args);
    }

    @Bean
    //客户端负载均衡
    @LoadBalanced
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

新建service

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class LearnService {

    @Autowired
    private RestTemplate restTemplate;

    public String getMath() {
        return restTemplate.getForObject("http://provider-server/learn/math", String.class);
    }

}

新建controller

import com.gm.consumerserver.service.LearnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("learn")
public class LearnController {

    @Autowired
    private LearnService service;

    @GetMapping("math")
    public String math(){
        return service.getMath();
    }
}

启动consumer-server
刷新Eureka
在这里插入图片描述
浏览器输入http://localhost:8282/learn/math
在这里插入图片描述
方法二OpenFeign
导入 spring-cloud-starter-openfeign

		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-openfeign</artifactId>
		</dependency>

修改启动类:
添加 @EnableFeignClients

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class ConsumerServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerServerApplication.class, args);
    }

//    @Bean
      //客户端负载均衡
//    @LoadBalanced
//    RestTemplate restTemplate() {
//        return new RestTemplate();
//    }

}

修改service,注意已修改为interface

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

@Service
@FeignClient(name = "provider-server",path = "/learn")
public interface LearnService {

//    @Autowired
//    private RestTemplate restTemplate;
//
//    public String getMath() {
//        return restTemplate.getForObject("http://provider-server/learn/math", String.class);
//    }

    @RequestMapping(value = "math")
    String getMath();
}

重启该服务,继续访问http://localhost:8282/learn/math
在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值