微服务架构风格是一种将单个应用程序作为一套小型服务开发的方法,每种应用程序都在自己的进程中运行,并与轻量级机制(通常是 HTTP 资源 API)进行通信。
2. 微服务的项目架构
注册中心: Eureka Nacos
负载均衡: Ribbon
远程调用: Feign
服务网管: Gatway
虚拟容器: Docker
消息队列: RabbitMQ
高速缓存: redis
熔断降级: Sentinel
单一职责:不同微服务,不要重复开发相同业务
数据独立:不要访问其它微服务的数据库
面向服务:将自己的业务暴露为接口,供其它微服务调
3.1创建数据库
3.2创建简单的maven项目(无需勾选maven模板)
4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.9.RELEASE com.xmx SpringCloud_Eureka 1.0-SNAPSHOT pom UTF-8 UTF-8 1.8 Hoxton.SR10 8.0.13 2.1.1 org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import com.alibaba.cloud spring-cloud-alibaba-dependencies 2.2.5.RELEASE pom import mysql mysql-connector-java ${mysql.version} org.mybatis.spring.boot mybatis-spring-boot-starter ${mybatis.version} org.projectlombok lombok
3.3创建三个子项目studentservice、bookservice、 eurekaservice
studentservice 的配置(bookservice一样 依赖一样(只在studentservice引用书籍bookservice 的坐标))
SpringCloud_Eureka com.xmx 1.0-SNAPSHOT 4.0.0 studentservice 8 8 com.xmx bookservice 1.0-SNAPSHOT org.springframework.boot spring-boot-starter-web mysql mysql-connector-java org.mybatis mybatis 3.5.3 org.mybatis.spring.boot mybatis-spring-boot-starter
代码
目录结构
实体类
package com.xmx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@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;
@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;
@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;
@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;
@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;
@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;
@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.1创建eurekaService
导入依赖
org.springframework.cloud spring-cloud-starter-netflix-eureka-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web
**在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;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
代开:http://localhost:10086/
如上显示则配置成功
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;
@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;
@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();
}
}
运行结果



