SpringCloud搭建分布式服务架构(通用版本)

问题引入:SpringCloud是什么?(在了解这个之前需要有微服务的概念)

基于springBoot的一套实现微服务的框架,提供了微服务所需的配置管理,基于Http协议的restful风格(返回异步数据)

SpringCould组件架构图
在这里插入图片描述
由于在一台电脑上演示分布式项目,需要创建多个项目模块

步骤:

创建父类工程maven工程,修改pom文件,将打包方式改为pom
创建服务工程(注册中心)SpringBoot模块,选择需要导入的包,具体见下截图
在这里插入图片描述
在这里插入图片描述
在SpringBoot文件中添加eureka服务配置

#设置服务器端口
server.port:9000
#设置应用程序名称  名字不许用下划线,不支持服务调用
spring.application.name=sc-demo-server
#主机地址
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

在这个模块的启动类上添加eureka注解开启服务

@SpringBootApplication
@EnableEurekaServer  //启动eureka,开启服务
public class ScDemoServerApplication {
    public static void main(String[] args) {
SpringApplication.run(ScDemoServerApplication.class, args);
    }
}

这时候运行你的启动类,浏览器地址栏输入访问url

http://localhost:自己设置的端口号/ 看到这个,说明eureka服务配置成功
在这里插入图片描述
创建eureka的服务提供者 SpringBoot工程,具体见图(注意组件的区别)
在这里插入图片描述
在这里插入图片描述
这个模块的启动类上添加eureka注解开启服注意注解的使用

@SpringBootApplication
@EnableEurekaClient
public class ScDemoProviderApplication {

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

SpringBoot的配置文件中发布服务配置

#服务器端口
server.port=9001
#配置发布服务地址
spring.application.name=sc-demo-provider
eureka.client.service-url.defaultZone=http://localhost:9000/eureka

测试eureka服务者

在模块中创建一个包写一个实体类

public class Student {
    private Integer xh;
    private String name;
    private String sex;
    private Integer age;
    setter和getter  构造
}

编写Controller层代码

@RestController
public class TestController {
    //接收请求(服务)  返回json
    @RequestMapping("/getData")
    public Student getData(){
       //返回一个学生   --调数据库
        return new Student(101,"张三","人妖",21);
    }
}

在浏览器输入http://localhost:自己设置的服务提供者端口号/控制器请求名

页面会返回学生的json数据则成功

创建eureka的服务消费者 SpringBoot工程

创建流程与提供者相同

编写消费者配置文件:

#服务器
server.port=9002
#服务的名称
spring.application.name=eureka-consumer
#指定注册中心:eureka服务器
eureka.client.service-url.defaultZone=http://localhost:9000/eureka

在启动类上开启eureka

package com.sc.dome;

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;

@EnableEurekaClient
@SpringBootApplication
public class ScConsumerApplication {

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

    @Bean   //交给spring容器管理
    @LoadBalanced  // 支持使用服务名称发现服务进行调用,且支持负载
    public RestTemplate getRestTemplate() {
        return new RestTemplate();
    }
}

开发Controller层代码,调用微服务

package com.sc.dome.controller;

import com.sc.dome.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class StudentController {
    @Autowired
    private RestTemplate restTemplate; // 从spring容器自动注入RestTemplate对象
    @RequestMapping("showStudent")
    public Student showStudent(){
        Student stu = restTemplate.getForObject("http://SC-PROVIDER/getStudent", Student.class);
        return stu;
    }
}

特别注意:启动服务测试的时候需要开启三个启动类

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值