Spring_cloud

Spring_cloud服务提供者

在父类导入pom.xml

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
  </parent>

依赖

  <dependencies>0
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <!--springCloud依赖-->
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Greenwich.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

包结构
在这里插入图片描述
IDoSomeService 接口


public interface IDoSomeService {
    public String doSome();
}

IDoSomeServiceimpl 实现类
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@RestController
public class IDoSomeServiceimpl implements IDoSomeService {
    @RequestMapping("/doSome")
    @Override
    public String doSome() {
        System.out.println("服务提供者");
        return "服务提供者eureka";
    }
}

App启动类

@SpringBootApplication
@EnableEurekaServer
public class App 
{
    public static void main( String[] args )
    {

        SpringApplication.run(App.class,args);
    }
}

application.yml

##应用名称
spring:
  application:
    #可以自定义
    name: eureka_server
##声明当前eurekaserver的端口号
server:
  port: 8888
##配置eureka
eureka:
  client:
    ##代表是否将自己注册到注册中心去
    register-with-eureka: false
    ##表明自己是注册中心
    fetch-registry: false
    ##配置地址
    service-url:
      defaultZone: http://localhost:8888/eureka

启动类启动后,可以输入配置地址的的路径http://localhost:8888
在这里插入图片描述

spring_cloud_provider生产者

包结构
在这里插入图片描述
application.yml

##应用名称
spring:
  application:
    name: eureka-provider
##声明当前eurekaserver的端口号
server:
  port: 8890
##配置eureka
eureka:
  client:
    ##配置地址
    service-url:
      defaultZone: http://localhost:8888/eureka

ConsumerController

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

import javax.annotation.Resource;

@RestController
public class ConsumerController {
//Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTemplate 提供了多种便捷访问远程Http服务的方法
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/doSome")
    public String doSome(){
        System.out.println("ConsumerController");
        return restTemplate.getForObject("http://localhost:8889/doSome",String.class);
    }
}

App 启动类
@EnableEurekaClient注解是基于spring-cloud-netflix依赖,只能为eureka作用;

@SpringBootApplication
@EnableEurekaClient
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class,args);
    }
    @Bean
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

在这里插入图片描述

spring_ cloud_ consumer消费者

在这里插入图片描述
application.yml

##应用名称
spring:
  application:
    name: eureka-consumer
##声明当前eurekaserver的端口号
server:
  port: 8891
##配置eureka
eureka:
  client:
    ##配置地址
    service-url:
      defaultZone: http://localhost:8888/eureka

ConsumerController

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RequestMapping
public class ConsumerController {
    @Resource
    private RestTemplate restTemplate;

    @RequestMapping("/doSome")
    public String doSome(){
        System.out.println("ConsumerController");
        return restTemplate.getForObject("http://eureka-provider/doSome",String.class);
    }
}

写生产者的name,如果只要单个可以写localhost…8080这样子
在这里插入图片描述
App 启动类

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

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

}

运行结果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值