Spring Cloud之Feigin客户端重构思想
应该重构接口信息(重点)
toov5-parent 存放共同依赖信息
toov5-api api的只有接口没有实现
toov5-api-member
toov5-api-order
too5-member-impl api接口的实现
toov5-order-impl
1、 创建 parent的 pom工程
2、 点击parent创建maven model 的 service pom
3、 点击 service 创建两个 api-service 的jar
4、点击parent创建 两个 两个接口的实现 jar
实体类 是单独建立一个项目 实体类存放在接口下面 接口可能会被别人调用 其他核心代码就别写到这里了 实体类还是可以的
代码是吸纳存放在接口实现类里面
依赖jar 放在parent
定义member 接口 和涉及到的 实体类
这里面的核心 订单服务调用会员服务接口,用来实现feign客户端 ,减少重复代码
Eureka server:
pom:
<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>
<groupId>com.toov5</groupId>
<artifactId>SpringCloud-eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<!-- 管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.M7</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--SpringCloud eureka-server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories> </project>
yml:
###eureka 服务端口号
server:
port: 8100
###服务注册名称
eureka:
instance:
##注册中心ip地址
hostname: 127.0.0.1
###客户端调用地址
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
###因为该应用为注册中心,不会注册自己 (集群设为true)
register-with-eureka: false
###因为自己为注册中心 ,不会去在该应用中的检测服务
fetch-registry: false
启动类:
package com.toov5; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer //开启注册中心
@SpringBootApplication
public class AppEureka { public static void main(String[] args) {
SpringApplication.run(AppEureka.class, args);
} }
业务逻辑部分:
其中service 是个pom文件 包括两个model : member service 和 order service 两个接口
实现类分别是 member service impl 和 order service
service 接口项目聚合:
Member:
entity:
package com.toov5.api.entity; import lombok.Data; @Data
public class UserEntity {
private String name;
private Integer age; }
service接口:
package com.toov5.api.service; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity; @RestController
public interface IMemberService { @RequestMapping("/getMember") //接口加@RequestMapping 被其他项目调用时候 feign客户端可以继承
public UserEntity getMember(@RequestParam("name") String name); }
实现:
pom:
<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>
<parent>
<groupId>com.toov5</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>toov5-api-member-service-impl</artifactId> <dependencies>
<dependency>
<groupId>com.toov5</groupId>
<artifactId>toov5-api-member-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.toov5</groupId>
<artifactId>toov5-api-order-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies> </project>
实现:
package com.toov5.api.service.impl; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import com.toov5.api.entity.UserEntity;
import com.toov5.api.service.IMemberService; //注意加在实现类上面!!! 接口不能加 接口不能被实例化
@RestController
public class MemberServiceImpl implements IMemberService { @RequestMapping("/getMember")
public UserEntity getMember(@RequestParam("name") String name) {
UserEntity userEntity = new UserEntity();
userEntity.setName(name);
userEntity.setAge(10);
return userEntity;
} }
启动类:
package com.toov5.api.service.impl; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class AppMember {
public static void main(String[] args) {
SpringApplication.run(AppMember.class, args);
}
}
Order:
接口类:
package com.toov5.api.service; import org.springframework.web.bind.annotation.RequestMapping; public interface IOrderService {
//订单服务带哦用会员服务接口信息fegin
@RequestMapping("/orderToMember")
public String orderToMember(String name);
}
实现:
Feign: 通过继承 减少代码
package com.toov5.api.feign; import org.springframework.cloud.openfeign.FeignClient; import com.toov5.api.service.IMemberService;
//避免了冗余代码 直接过来就ok了
@FeignClient("app-toov5-member")
public interface MemberServiceFeign extends IMemberService {
//实体类是存放接口项目还是存放在实现项目 实体类存放在接口项目里面
//实体类和定义接口信息存放在接口项目
//代码实现放在接口实现类里面
}
package com.toov5.api.service.impl; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.toov5.api.entity.UserEntity;
import com.toov5.api.feign.MemberServiceFeign;
import com.toov5.api.service.IOrderService; @RestController
public class OrderServiceImpl implements IOrderService {
@Autowired
private MemberServiceFeign memberServiceFeign; @RequestMapping("orderToMmeber")
public String orderToMember(String name) {
UserEntity user = memberServiceFeign.getMember(name);
return user==null ? "没有找到用户先关信息" : user.toString();
}
}
package com.toov5.api; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication(scanBasePackages={"com.toov5.*"})
@EnableEurekaClient
@EnableFeignClients
public class AppOrder {
public static void main(String[] args) {
SpringApplication.run(AppOrder.class, args);
}
}
yml:
###服务启动端口号
server:
port: 8001
###服务名称(服务注册到eureka名称)
spring:
application:
name: app-toov5-order
###服务注册到eureka地址
eureka:
client:
service-url:
defaultZone: http://localhost:8100/eureka ###因为该应用为注册中心,不会注册自己
register-with-eureka: true
###是否需要从eureka上获取注册信息
fetch-registry: true
都启动后:
访问订单接口
按照这个规范目录去做 尤其@FeignClient要写到客户端里面 方便做服务降级
Feign默认开启的本地负载均衡~
Spring Cloud之Feigin客户端重构思想的更多相关文章
- spring cloud 自定义ribbon客户端
一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...
- 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)
在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...
- Spring Cloud之Feign客户端超时时间配置
关于雪崩效应: 默认情况下tomcat只有一个线程去处理客户端发送的所有请求.高并发情况下,如果客户端请求都在同一接口,tomcat的所有线程池去处理,导致其他接口服务访问不了,等待. Tomcat有 ...
- Spring Cloud之Feign客户端调用工具
feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate Fetin Feign客户端实际开发 ...
- Spring Cloud配置中心客户端读取配置
微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...
- Spring Cloud Ribbon实现客户端负载均衡
1.构建microservice-consumer-movie-ribbon项目,在pom.xml中引入ribbon依赖 在引入Eureka依赖的时候,默认里面含有ribbon依赖 2.添加@Load ...
- Spring Cloud Ribbon之URL重构(三)
接着前面的说,前两篇中分析了解析和动态服务列表的获取,这两步完成后那接下来要做的事就是重组解析后的URL路径和发起通信了,这一步完成应该是在前面分析的RibbonLoadBalancerClient. ...
- Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了
先摆解决方案: 方法一: 方法二: zuul.routes.<routeName>.sensitive-headers= zuul.routes.<routeName>.cus ...
- spring cloud 使用ribbon简单处理客户端负载均衡
假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...
随机推荐
- windows 配置squid反向代理服务器
发现Window版本的Squid 和 Linux 配置有点不一样 一.配置squid\etc目录1.squid.conf.default 拷贝一份重新命名为squid.conf2.cachemgr.c ...
- github上比較好的开源项目(持续更新)
1:https://github.com/Skykai521/StickerCamera 实现相机功能 实现对图片进行裁剪的功能 图片的滤镜功能 能为图片加入贴纸(贴纸可移动,放大,旋转) 能为图片加 ...
- NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(三)
Precondition: hadoop 2.7.1 hbase 0.98.13 solr 5.2.1 / Apache Solr 4.8.1 http://archive.apache.org ...
- centos7 中文输入法设置
安装centos7 后,他有自带的中文输入法安装包找到 applications->systemTools->settings->region&language 2:在 in ...
- mysql中百万级别分页查询性能优化
前提条件: 1.表的唯一索引 2.百万级数据 SQL语句: select c.* FROM ( SELECT a.logid FROM tableA a where 1 = 1 <#if pho ...
- MongoDB入门学习(1)
什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为WEB应用提供 ...
- 【WPF学习笔记】之依赖属性
概述: Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能.这些服务通常统称为 WPF 属性系统.由 ...
- Objective-C 的动态提示和技巧
过去的几年中涌现了大量的Objective-C开发者.有些是从动态语言转过来的,比如Ruby或Python,有些是从强类型语言转过来的,如Java或C#,当然也有直接以Objective-C作为入门语 ...
- python selenium - SSL处理(https)
在实际的自动化测试实践中,因为越来越多的站点接入https,使得我们原有的python selenium2自动化测试代码进行测试时,浏览器总是报安全问题,即便在浏览器选项中将被测网址加入信任网址也没用 ...
- PHP-Manual的学习----【语言参考】----【类型】-----【float浮点型】
笔记:1.浮点型(也叫浮点数 float,双精度数 double 或实数 real)可以用以下任一语法定义: <?php$a = 1.234; $b = 1.2e3; $c = 7E-10;?& ...