zoukankan      html  css  js  c++  java
  • SpringCloud入门

    一。首先创建一个服务提供者

    实体类:

    package com.dsjg.user.pojo;

    import lombok.Data;
    import tk.mybatis.mapper.annotation.KeySql;

    import javax.persistence.Id;
    import javax.persistence.Table;
    import java.util.Date;

    @Data
    @Table(name = "tb_user")
    public class User{
    // id
    @Id
    //开启主键自动回填
    @KeySql(useGeneratedKeys = true)
    private Long id;

    // 用户名
    private String userName;

    // 密码
    private String password;

    // 姓名
    private String name;

    // 年龄
    private Integer age;

    // 性别,1男性,2女性
    private Integer sex;

    // 出生日期
    private Date birthday;

    // 创建时间
    private Date created;

    // 更新时间
    private Date updated;

    // 备注
    private String note;
    }

     mybatisplus:

    package com.dsjg.user.mapper;

    import com.dsjg.user.pojo.User;
    import tk.mybatis.mapper.common.Mapper;

    public interface UserMapper extends Mapper<User> {
    }
    Server服务:
    package com.dsjg.user.service;


    import com.dsjg.user.mapper.UserMapper;
    import com.dsjg.user.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    @Service
    public class UserService {


    @Autowired
    private UserMapper userMapper;


    public User queryById(Long id){

    return userMapper.selectByPrimaryKey(id);
    }
    }

    web层:
    package com.dsjg.user.controller;

    import com.dsjg.user.pojo.User;
    import com.dsjg.user.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;

    @RestController
    @RequestMapping("/user")
    public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    public User queryById(@PathVariable Long id){

    return userService.queryById(id);
    }
    }
    SpringBoot项目启动:
    package com.dsjg.user;

    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;

    import tk.mybatis.spring.annotation.MapperScan;

    @SpringBootApplication
    @MapperScan("com.dsjg.user.mapper")

    public class UserApplication {
    public static void main(String[] args) {
    SpringApplication.run(UserApplication.class, args);
    }
    }
    二。服务调用者:

    package com.dsjg.consumer.pojo;

    import lombok.Data;

    import java.util.Date;

    @Data
    public class User {
    private Long id;

    // 用户名
    private String userName;

    // 密码
    private String password;

    // 姓名
    private String name;

    // 年龄
    private Integer age;

    // 性别,1男性,2女性
    private Integer sex;

    // 出生日期
    private Date birthday;

    // 创建时间
    private Date created;

    // 更新时间
    private Date updated;

    // 备注
    private String note;
    }

    web层:
    package com.dsjg.consumer.controller;


    import com.dsjg.consumer.pojo.User;
    import org.springframework.beans.factory.annotation.Autowired;

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

    @RestController
    @RequestMapping("/consumer")
    public class ConsumerController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{id}") public User queryById(@PathVariable Long id){
    String url = "http://localhost:9091/user/" + id;
    return restTemplate.getForObject(url, User.class);
    }
    }
    SpringBoot启动层:
    package com.dsjg.consumer;
    import org.springframework.boot.SpringApplication;

    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    @SpringBootApplication
    public class ConsumerApplication {
    public static void main(String[] args) {

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

    @Bean
    public RestTemplate restTemplate(){

    return new RestTemplate();
    }
    }












    一个简单的远程服务调用案例就实现了。
     思考问题
    简单回顾一下,刚才我们写了什么:
    user-service:对外提供了查询用户的接口
    consumer-demo:通过RestTemplate访问 http://locahost:9091/user/{id} 接口,查询用户数据
    存在什么问题?
    在consumer中,我们把url地址硬编码到了代码中,不方便后期维护
    consumer需要记忆user-service的地址,如果出现变更,可能得不到通知,地址将失效
    consumer不清楚user-service的状态,服务宕机也不知道
    user-service只有1台服务,不具备高可用性
    即便user-service形成集群,consumer还需自己实现负载均衡
    其实上面说的问题,概括一下就是分布式服务必然要面临的问题:
    服务管理
    如何自动注册和发现
    如何实现状态监管
    如何实现动态路由
    服务如何实现负载均衡
    服务如何解决容灾问题服务如何实现统一配置
    以上的问题,都将在SpringCloud中得到答案。



  • 相关阅读:
    判断有无网络
    Listview的OnScrollListener的滑动监听实现分页加载
    第三方 XListview 上拉加载、下拉刷新、分页加载和Gson解析
    Gson解析数据
    百度地图实现普通地图、定位、周边搜索功能
    Viewpager实现网络图片的轮播
    英语wacche腕表
    caement Archaic spelling of cement
    水泥caement单词
    英语caement水泥
  • 原文地址:https://www.cnblogs.com/wang123--/p/13258722.html
Copyright © 2011-2022 走看看