SpringCloud+Eureka入门Demo

2021-04-30 22:29

阅读:564

标签:relative   url   string   autowired   启动器   big   注解   创建   enable   

SpringCloud入门Demo

1、创建一个pom工程,管理版本

2、引入依赖

4.0.0cn.rayfoo
    SpringCloudParent
    1.0-SNAPSHOT../emp-provider../emp-consumer../Eureka-Serverpom
        spring-boot-starter-parent
        org.springframework.boot2.0.4.RELEASEUTF-8UTF-81.8Finchley.SR12.0.35.1.321.2.5org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}pomimporttk.mybatis
                mapper-spring-boot-starter
                ${mapper.starter.version}com.github.pagehelper
                pagehelper-spring-boot-starter
                ${pageHelper.starter.version}mysql
                mysql-connector-java
                ${mysql.version}org.springframework.boot
                spring-boot-maven-plugin
            

3、创建Eureka注册中心

pom文件


        SpringCloudParent
        cn.rayfoo1.0-SNAPSHOT4.0.0
    Eureka-Server
    jarorg.springframework.cloud
            spring-cloud-starter-netflix-eureka-server
        

yml文件

server:
  port: 8761
spring:
  application:
    name: eureka-server
eureka:
  client:
    service-url:
      defaultZone : http://127.0.0.1:8761/eureka

启动器

package cn.rayfoo;

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

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:45 下午
 * @Description:
 */
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerRunner {

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

}

4、创建提供者

pom文件


        SpringCloudParent
        cn.rayfoo1.0-SNAPSHOT4.0.0
    emp-provider
    jarorg.springframework.boot
            spring-boot-starter-web
        mysql
            mysql-connector-java
        tk.mybatis
            mapper-spring-boot-starter
        org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

yml

server:
  port: 8081
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: root
  application:
    name: emp-provider
mybatis:
  mapper-locations: classpath*:cn.rayfoo.mapper
  type-aliases-package: cn.rayfoo.bean
  configuration:
    map-underscore-to-camel-case: true
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

javaBean

提供者需要实现序列化、由于使用了通用mapper,需要加上Table和Id注解

package cn.rayfoo.bean;

import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/1 8:52 下午
 * @Description:
 */
@Table(name = "emp")
public class Employee implements Serializable {

    @Id
    private Integer empno;
    private String ename;
    private String job;
    private String mgr;
    private Date hiredate;
    private BigDecimal salary;
    private Double comm;
    private Integer deptno;

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getMgr() {
        return mgr;
    }

    public void setMgr(String mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public Double getComm() {
        return comm;
    }

    public void setComm(Double comm) {
        this.comm = comm;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empno=" + empno +
                ", ename=‘" + ename + ‘\‘‘ +
                ", job=‘" + job + ‘\‘‘ +
                ", mgr=‘" + mgr + ‘\‘‘ +
                ", hiredate=" + hiredate +
                ", salary=" + salary +
                ", comm=" + comm +
                ", deptno=" + deptno +
                ‘}‘;
    }

    public Employee() {
    }
}

mapper接口,注意使用的是tk包下的Mapper

package cn.rayfoo.mapper;

import cn.rayfoo.bean.Employee;
import tk.mybatis.mapper.common.Mapper;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:12 下午
 * @Description:
 */
public interface EmployeeMapper extends Mapper {
}

mapper xml

service

package cn.rayfoo.service;

import cn.rayfoo.bean.Employee;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:15 下午
 * @Description:
 */
public interface EmployeeService {
    Employee getEmployeeById(Integer id);
}

package cn.rayfoo.service.impl;

import cn.rayfoo.bean.Employee;
import cn.rayfoo.mapper.EmployeeMapper;
import cn.rayfoo.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:16 下午
 * @Description:
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }
}

controller

package cn.rayfoo.controller;


import cn.rayfoo.bean.Employee;
import cn.rayfoo.service.EmployeeService;
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;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:14 下午
 * @Description:
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/{id}")
    public Employee getEmployeeById(@PathVariable Integer id){
        return employeeService.getEmployeeById(id);
    }

}

启动器

package cn.rayfoo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import tk.mybatis.spring.annotation.MapperScan;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:11 下午
 * @Description:
 */
@EnableDiscoveryClient
@SpringBootApplication
@MapperScan("cn.rayfoo.mapper")
public class ProviderRunner {

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

}

5、创建消费者

pom文件


        SpringCloudParent
        cn.rayfoo1.0-SNAPSHOT4.0.0
    emp-consumer
    jarorg.springframework.boot
            spring-boot-starter-web
        org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
        

yml文件

server:
  port: 8082
spring:
  application:
    name: emp-customer
eureka:
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

JavaBean 实现序列化接口

package cn.rayfoo.bean;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/1 8:52 下午
 * @Description:
 */
public class Employee implements Serializable {

    private Integer empno;
    private String ename;
    private String job;
    private String mgr;
    private Date hiredate;
    private BigDecimal salary;
    private Double comm;
    private Integer deptno;

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public String getMgr() {
        return mgr;
    }

    public void setMgr(String mgr) {
        this.mgr = mgr;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public BigDecimal getSalary() {
        return salary;
    }

    public void setSalary(BigDecimal salary) {
        this.salary = salary;
    }

    public Double getComm() {
        return comm;
    }

    public void setComm(Double comm) {
        this.comm = comm;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "empno=" + empno +
                ", ename=‘" + ename + ‘\‘‘ +
                ", job=‘" + job + ‘\‘‘ +
                ", mgr=‘" + mgr + ‘\‘‘ +
                ", hiredate=" + hiredate +
                ", salary=" + salary +
                ", comm=" + comm +
                ", deptno=" + deptno +
                ‘}‘;
    }

    public Employee() {
    }
}

配置类

package cn.rayfoo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:29 下午
 * @Description:
 */
@Configuration
public class RestTemplateConfiguration {

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

}

controller

package cn.rayfoo.controller;

import cn.rayfoo.bean.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
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;

import java.util.List;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:30 下午
 * @Description:
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private RestTemplate restTemplate;

    /***
     * 注入discoveryClient 注意是Spring的包
     */
    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
    public Employee getEmployeeById(@PathVariable Integer id) {
        //获取实例列表
        List instancesList = discoveryClient.getInstances("emp-provider");
        //获取实例
        ServiceInstance instance = instancesList.get(0);
        //获取主机地址
        String hostName = instance.getHost();
        //获取端口号
        int port = instance.getPort();
        //拼接url
        String url = "http://" + hostName + ":" + port + "/employee/" + id;
        //调用接口
        Employee employee = restTemplate.getForObject(url, Employee.class);
        //返回结果
        return employee;
    }

}

启动器

package cn.rayfoo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

/**
 * @Author: rayfoo@qq.com
 * @Date: 2020/7/2 2:27 下午
 * @Description:
 */
@EnableDiscoveryClient
@SpringBootApplication
public class CustomerRunner {

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

}

SpringCloud+Eureka入门Demo

标签:relative   url   string   autowired   启动器   big   注解   创建   enable   

原文地址:https://www.cnblogs.com/zhangruifeng/p/13225990.html


评论


亲,登录后才可以留言!