SpringCloud微服务-Eureka
1 、什么是微服务
微服务架构风格是一种将单个应用程序作为一套小型服务开发的方法,每种应用程序都在自己的进程中运行,并与轻量级机制(通常是 HTTP 资源 API)进行通信。
2. 微服务的项目架构
注册中心: Eureka Nacos
负载均衡: Ribbon
远程调用: Feign
服务网管: Gatway
虚拟容器: Docker
消息队列: RabbitMQ
高速缓存: redis
熔断降级: Sentinel
3. 服务拆分和远程调用
单一职责:不同微服务,不要重复开发相同业务
数据独立:不要访问其它微服务的数据库
面向服务:将自己的业务暴露为接口,供其它微服务调
3.1创建数据库
3.2创建简单的maven项目(无需勾选maven模板)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--导入springboot依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.9.RELEASE</version>
<relativePath/>
</parent>
<!--子项目-->
<groupId>com.xmx</groupId>
<artifactId>SpringCloud_Eureka</artifactId>
<version>1.0-SNAPSHOT</version>
<!--打包-->
<packaging>pom</packaging>
<!--版本统一管理-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR10</spring-cloud.version>
<mysql.version>8.0.13</mysql.version>
<mybatis.version>2.1.1</mybatis.version>
</properties>
<!--引用管理的版本-->
<dependencyManagement>
<dependencies>
<!-- springCloud -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--nacos 的管理依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- mysql 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<!--依赖-->
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
3.3创建三个子项目studentservice、bookservice、 eurekaservice
studentservice 的配置(bookservice一样 依赖一样(只在studentservice引用书籍bookservice 的坐标))
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>SpringCloud_Eureka</artifactId>
<groupId>com.xmx</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>studentservice</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!--引入书籍的依赖-->
<dependency>
<groupId>com.xmx</groupId>
<artifactId>bookservice</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
代码
目录结构
实体类
package com.xmx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @create: 2022-02-28 17:59
* @author: 郑兴源
* @description:
**/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer sid;
private String sname;
private Integer age;
private Integer bid;
private Book book;
}
控制层
package com.xmx.controller;
import com.xmx.pojo.Student;
import com.xmx.service.StudentService;
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.RestController;
/**
* @create: 2022-02-28 17:59
* @author: 郑兴源
* @description:
**/
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/student/findById/{id}")
public Student findById(@PathVariable("id") Integer id) {
return studentService.findByid(id);
}
}
持久层
package com.xmx.dao;
import com.xmx.pojo.Student;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* @create: 2022-02-28 17:59
* @author: 郑兴源
* @description:
**/
@Repository
public interface StudentDao {
//根据id查询
@Select("select * from student where sid=#{id}")
Student findById(Integer id);
}
服务层(stundent 与book 不同)
package com.xmx.service;
import com.xmx.dao.StudentDao;
import com.xmx.pojo.Book;
import com.xmx.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @create: 2022-02-28 17:59
* @author: 郑兴源
* @description:
**/
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
@Autowired
RestTemplate rt;
public Student findByid(Integer id) {
//此时查询时 只有学生信息 无书籍信息
Student student = studentDao.findById(id);
//得到要远程调用的 url 请求 需为bookservice 的端口
String url = "http://localhost:9090/book/findById//" + student.getBid();//get到数据id
// 3 发送 http 请求,实现远程调用
Book book = rt.getForObject(url, Book.class);
student.setBook(book);
return student;
}
}
bookService
package com.xmx.service;
import com.xmx.dao.BookDao;
import com.xmx.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @create: 2022-02-28 18:10
* @author: 郑兴源
* @description:
**/
@Service
public class BookService {
@Autowired
BookDao bookDao;
public Book findByid(Integer id) {
return bookDao.findById(id);
}
}
BookApplication
package com.xmx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @create: 2022-02-28 17:50
* @author: 郑兴源
* @description:
**/
@SpringBootApplication
@MapperScan("com.xmx.dao")
public class BookApplication {
public static void main(String[] args) {
SpringApplication.run(BookApplication.class, args);
}
}
StudentApplication
package com.xmx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @create: 2022-02-28 17:50
* @author: 郑兴源
* @description:
**/
@SpringBootApplication
@MapperScan("com.xmx.dao")
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
//充当服务 ---调用的组件
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
yml文件
server:
port: 9090
spring:
application:
name: bookservice # 服务名称
datasource:
url: jdbc:mysql://localhost:3306/j9bookspcol?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username: root
password: 19990507
driver-class-name: com.mysql.cj.jdbc.Driver
#eureka:
# client:
# service-url:
# defaultZone: http://127.0.0.1:10086/eureka
server:
port: 9091
spring:
application:
name: studnetservice # 服务名称
datasource:
url: jdbc:mysql://localhost:3306/j9studentspcol?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username: root
password: 19990507
driver-class-name: com.mysql.cj.jdbc.Driver
#eureka:
# client:
# service-url:
# defaultZone: http://127.0.0.1:10086/eureka
******必须两个项目一起启动启两个
结果
4.使用Eureka
4.1创建eurekaService
导入依赖
<dependencies>
<!--eureka服务端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!--eureka客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
**在studentservice .bookservice 加入依赖 eureka服务端 依赖 **
并且配置yml(解封我的注解)
配置 Eureka application.yml
server:
port: 10086
spring:
application:
name: eurekaserver # eureka 的服务名称
eureka:
client: #自己充当一个客户端
service-url: # eureka 的地址信息
defaultZone: http://127.0.0.1:10086/eureka/
fetch-registry: false
# register-with-eureka: false
EurekaApplication运行类加注解@EnableEurekaServer(点击运行在运行student book service)
package com.xmx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @create: 2022-02-28 16:02
* @author: 郑兴源
* @description:
**/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
代开:http://localhost:10086/
如上显示则配置成功
5.使用Eureka修改studentservice
package com.xmx.service;
import com.xmx.dao.StudentDao;
import com.xmx.pojo.Book;
import com.xmx.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
/**
* @create: 2022-02-28 17:59
* @author: 郑兴源
* @description:
**/
@Service
public class StudentService {
@Autowired
private StudentDao studentDao;
@Autowired
RestTemplate rt;
public Student findByid(Integer id) {
//此时查询时 只有学生信息 无书籍信息
Student student = studentDao.findById(id);
//得到要远程调用的 url 请求
//方法1
// String url = "http://localhost:9090/book/findById//" + student.getBid();//get到数据id
//方法2 使用eureka---启分发作用
String url = "http://bookservice/book/findById//" + student.getBid();//get到数据id
// 3 发送 http 请求,实现远程调用
Book book = rt.getForObject(url, Book.class);
student.setBook(book);
return student;
}
}
修改StudentApplication
package com.xmx;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
/**
* @create: 2022-02-28 17:50
* @author: 郑兴源
* @description:
**/
@SpringBootApplication
@MapperScan("com.xmx.dao")
public class StudentApplication {
public static void main(String[] args) {
SpringApplication.run(StudentApplication.class, args);
}
//充当服务 ---调用的组件
@Bean
//加入
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
运行结果