学习链接:
Spring Cloud构建微服务架构:服务注册与发现(Eureka、Consul)【Dalston版】:https://blog.didispace.com/spring-cloud-starter-dalston-1/
Spring Cloud微服务简介:https://blog.csdn.net/cjtxzg/article/details/80535685
server:
port: 8001 #指定运行端口
spring:
application:
name: eureka-server #指定服务名称
eureka:
instance:
hostname: localhost #指定主机地址
client:
fetch-registry: false #指定是否从注册中心获取服务(注册中心不需要开启)
register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
server:
enable-self-preservation: false #关闭保护模式
server:
port: 8101 #运行端口号
spring:
application:
name: eureka-client #服务名称
eureka:
client:
register-with-eureka: true #注册到Eureka的注册中心
fetch-registry: true #获取注册实例列表
service-url:
defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
Spring Cloud OpenFeign 是声明式的服务调用工具,它整合了Ribbon和Hystrix,拥有负载均衡和服务容错功能。
Feign是声明式的服务调用工具,我们只需创建一个接口并用注解的方式来配置它,就可以实现对某个服务接口的调用,简化了直接使用RestTemplate来调用服务接口的开发量。
Feign具备可插拔的注解支持,同时支持Feign注解、JAX-RS注解及SpringMvc注解。
当使用Feign时,Spring Cloud集成了Ribbon和Eureka以提供负载均衡的服务调用及基于Hystrix的服务容错保护功能。
在eureka-client客户端的pom.xml导入
如果创建项目之前勾选了相关组建,部分包就不需要再次引入
<!--导入Feign的依赖包-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--MyBatis 整合 SpringBoot 的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--MySQL 的驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--jdbc 的驱动依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.yml
spring:
#配置数据库的连接信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/newsdata?serverTimezone=UTC
username: root
password: 123456
eureka-client完整的application.yml:
server:
port: 8101 #运行端口号
spring:
application:
name: eureka-client #服务名称
#配置数据库的连接信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/newsdata?serverTimezone=UTC
username: root
password: 123456
eureka:
client:
register-with-eureka: true #注册到Eureka的注册中心
fetch-registry: true #获取注册实例列表
service-url:
defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
eureka-client1完整的application.yml:
server:
port: 8108 #运行端口号
spring:
application:
name: eureka-client1 #服务名称
#配置数据库的连接信息
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/newsdata?serverTimezone=UTC
username: root
password: 123456
eureka:
client:
register-with-eureka: true #注册到Eureka的注册中心
fetch-registry: true #获取注册实例列表
service-url:
defaultZone: http://localhost:8001/eureka/ #配置注册中心地址
eureka-client和eureka-client1客户端
@EnableFeignClients//Feign客户端
@EnableDiscoveryClient//Eureka客户端
@SpringBootApplication
public class EurekaClient1Application {
public static void main(String[] args) {
SpringApplication.run(EurekaClient1Application.class, args);
}
}
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello world!";
}
}
@Controller
public class UserController {//服务的消费者(取消费者)
@Resource
UserService userService;
@ResponseBody
@RequestMapping("/test")
public String test(){
//业务逻辑层
String s=userService.test();
return s;
}
}
UserService.java
public interface UserService {
public String test();
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
@Override
public String test() {
//调用数据访问层
return userMapper.test();
}
}
@FeignClient("eureka-client")//调用哪个服务器
@ResponseBody
public interface UserMapper {
@RequestMapping("/hello")
public String test();
}
public class User {
private Integer uid;
private String uname;
private String upwd;
public User() {
}
public User(Integer uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
@Controller
public class HelloController {//服务的提供方
//返回字符串
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello world!";
}
//返回对象
@ResponseBody
@RequestMapping("/getUser")//提供User对象
public User getUser(){
User user=new User(1,"admin","admin");
return user;
}
}
public class User {
private Integer uid;
private String uname;
private String upwd;
public User() {
}
public User(Integer uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
@FeignClient("eureka-client")//调用哪个服务器
@ResponseBody
public interface UserMapper {
@RequestMapping("/hello")
public String test();
@RequestMapping("/getUser")
public User getClientUser();
}
UserService.java
public interface UserService {
public String test();
//得到提供方的User对象
public User getClientUser();
}
UserServiceImpl.java
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
@Override
public String test() {
//调用数据访问层
return userMapper.test();
}
@Override
public User getClientUser() {
//调用数据访问层
return userMapper.getClientUser();
}
}
@Controller
public class UserController {//服务的消费者(取消费者)
@Resource
UserService userService;
@ResponseBody
@RequestMapping("/test")
public String test(){
//业务逻辑层
String s=userService.test();
return s;
}
@ResponseBody
@RequestMapping("/getUser")
public User getClientUser(){
//业务逻辑层
User user = userService.getClientUser();
return user;
}
}
Spring Cloud Gateway 为 SpringBoot 应用提供了API网关支持,具有强大的智能路由与过滤器功能,例如:熔断、限流、重试等。
Spring Cloud Gateway 具有如下特性:
基于Spring Framework 5, Project Reactor 和 Spring Boot 2.0 进行构建;
动态路由:能够匹配任何请求属性;
可以对路由指定 Predicate(断言)和 Filter(过滤器);
集成Hystrix的断路器功能;
集成 Spring Cloud 服务发现功能;
易于编写的 Predicate(断言)和 Filter(过滤器);
请求限流功能;
支持路径重写。
相关概念
Route(路由):路由是构建网关的基本模块,它由ID,目标URI,一系列的断言和过滤器组成,如果断言为true则匹配该路由;
Predicate(断言):指的是Java 8 的 Function Predicate。 输入类型是Spring框架中的ServerWebExchange。
这使开发人员可以匹配HTTP请求中的所有内容,例如请求头或请求参数。如果请求与断言相匹配,则进行路由;
Filter(过滤器):指的是Spring框架中GatewayFilter的实例,使用过滤器,可以在请求被路由前后对请求进行修改。
application.yml
#端口号
server:
port: 9201
#客户端eureka-client服务地址
service-url:
eureka-client: http://localhost:8101
#配置路由
spring:
cloud:
gateway:
routes:
- id: path_route #路由的ID
uri: ${service-url.eureka-client}/hello #匹配后路由地址
predicates:
- Path=/user/{uid} #断言,路径相匹配的进行路由
User.java
public class User {
private Integer uid;
private String uname;
private String upwd;
public User() {
}
public User(Integer uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpwd() {
return upwd;
}
public void setUpwd(String upwd) {
this.upwd = upwd;
}
}
<!--导入spring-cloud-api依赖-->
<dependency>
<groupId>com.macro.cloud</groupId>
<artifactId>spring-cloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
@Controller
public class UserController {//服务的消费者(取消费者)
@Resource
UserService userService;
@ResponseBody
@RequestMapping("/test")
public String test(){
//业务逻辑层
String s=userService.test();
return s;
}
@ResponseBody
@RequestMapping("/getUser")
public User getClientUser(){
//业务逻辑层
User user = userService.getClientUser();
return user;
}
@ResponseBody
//@RequestMapping("/getUser")
@GetMapping("/user/{uid}")
public User getUser(@PathVariable Integer uid){
return userService.getUser(uid);
}
}
public interface UserService {
public String test();
//得到提供方的User对象
public User getClientUser();
public User getUser(Integer uid);
}
@Service
public class UserServiceImpl implements UserService {
@Resource
UserMapper userMapper;
@Override
public String test() {
//调用数据访问层
return userMapper.test();
}
@Override
public User getClientUser() {
//调用数据访问层
return userMapper.getClientUser();
}
@Override
public User getUser(Integer uid) {
return userMapper.getUser(uid);
}
}
@FeignClient("eureka-client")//调用哪个服务器
@ResponseBody
public interface UserMapper {
@RequestMapping("/hello") //被调服务的方法
public String test();
@RequestMapping("/getUser")
public User getClientUser();
//restful风格
@GetMapping("/user/{uid}")
public User getUser(@PathVariable Integer uid);
}
<!--导入spring-cloud-api依赖-->
<dependency>
<groupId>com.macro.cloud</groupId>
<artifactId>spring-cloud-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
原来
import com.micro.client.pojo.User;
更改后
import com.macro.cloud.pojo.User;
@Controller
public class HelloController {//服务的提供方
//返回字符串
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "hello world!";
}
//返回对象
@ResponseBody
@RequestMapping("/getUser")
public User getUser(){
User user=new User(1,"admin","admin");
return user;
}
//返回对象,restful风格
@ResponseBody
@GetMapping("/user/{uid}")
public User getUserById(@PathVariable("uid") Integer uid){
User user=new User(1,"admin","admin");
return user;
}
}