应该重构接口信息(重点)

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:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>com.toov5</groupId>
  4. <artifactId>SpringCloud-eureka-server</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.0.1.RELEASE</version>
  10. </parent>
  11. <!-- 管理依赖 -->
  12. <dependencyManagement>
  13. <dependencies>
  14. <dependency>
  15. <groupId>org.springframework.cloud</groupId>
  16. <artifactId>spring-cloud-dependencies</artifactId>
  17. <version>Finchley.M7</version>
  18. <type>pom</type>
  19. <scope>import</scope>
  20. </dependency>
  21. </dependencies>
  22. </dependencyManagement>
  23. <dependencies>
  24. <!--SpringCloud eureka-server -->
  25. <dependency>
  26. <groupId>org.springframework.cloud</groupId>
  27. <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  28. </dependency>
  29. </dependencies>
  30. <!-- 注意: 这里必须要添加, 否者各种依赖有问题 -->
  31. <repositories>
  32. <repository>
  33. <id>spring-milestones</id>
  34. <name>Spring Milestones</name>
  35. <url>https://repo.spring.io/libs-milestone</url>
  36. <snapshots>
  37. <enabled>false</enabled>
  38. </snapshots>
  39. </repository>
  40. </repositories>
  41.  
  42. </project>

  yml:

  1. ###eureka 服务端口号
  2. server:
  3. port: 8100
  4. ###服务注册名称
  5. eureka:
  6. instance:
  7. ##注册中心ip地址
  8. hostname: 127.0.0.1
  9. ###客户端调用地址
  10. client:
  11. serviceUrl:
  12. defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  13. ###因为该应用为注册中心,不会注册自己 (集群设为true)
  14. register-with-eureka: false
  15. ###因为自己为注册中心 ,不会去在该应用中的检测服务
  16. fetch-registry: false

启动类:

  1. package com.toov5;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  6.  
  7. @EnableEurekaServer //开启注册中心
  8. @SpringBootApplication
  9. public class AppEureka {
  10.  
  11. public static void main(String[] args) {
  12. SpringApplication.run(AppEureka.class, args);
  13. }
  14.  
  15. }

业务逻辑部分:

其中service 是个pom文件 包括两个model : member service  和 order service 两个接口

实现类分别是   member service  impl 和  order service

service 接口项目聚合:

Member:

entity:

  1. package com.toov5.api.entity;
  2.  
  3. import lombok.Data;
  4.  
  5. @Data
  6. public class UserEntity {
  7. private String name;
  8. private Integer age;
  9.  
  10. }

service接口:

  1. package com.toov5.api.service;
  2.  
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import com.toov5.api.entity.UserEntity;
  8.  
  9. @RestController
  10. public interface IMemberService {
  11.  
  12. @RequestMapping("/getMember") //接口加@RequestMapping 被其他项目调用时候 feign客户端可以继承
  13. public UserEntity getMember(@RequestParam("name") String name);
  14.  
  15. }

实现:

pom:

  1. <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">
  2. <modelVersion>4.0.0</modelVersion>
  3. <parent>
  4. <groupId>com.toov5</groupId>
  5. <artifactId>parent</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. </parent>
  8. <artifactId>toov5-api-member-service-impl</artifactId>
  9.  
  10. <dependencies>
  11. <dependency>
  12. <groupId>com.toov5</groupId>
  13. <artifactId>toov5-api-member-service</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>com.toov5</groupId>
  18. <artifactId>toov5-api-order-service</artifactId>
  19. <version>0.0.1-SNAPSHOT</version>
  20. </dependency>
  21. </dependencies>
  22.  
  23. </project>

  实现:

  1. package com.toov5.api.service.impl;
  2.  
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5.  
  6. import com.toov5.api.entity.UserEntity;
  7. import com.toov5.api.service.IMemberService;
  8.  
  9. //注意加在实现类上面!!! 接口不能加 接口不能被实例化
    @RestController
  10. public class MemberServiceImpl implements IMemberService {
  11.  
  12. @RequestMapping("/getMember")
  13. public UserEntity getMember(@RequestParam("name") String name) {
  14. UserEntity userEntity = new UserEntity();
  15. userEntity.setName(name);
  16. userEntity.setAge(10);
  17. return userEntity;
  18. }
  19.  
  20. }

启动类:

  1. package com.toov5.api.service.impl;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.openfeign.EnableFeignClients;
  7.  
  8. @SpringBootApplication
  9. @EnableEurekaClient
  10. @EnableFeignClients
  11. public class AppMember {
  12. public static void main(String[] args) {
  13. SpringApplication.run(AppMember.class, args);
  14. }
  15. }

Order:

接口类:

  1. package com.toov5.api.service;
  2.  
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4.  
  5. public interface IOrderService {
  6. //订单服务带哦用会员服务接口信息fegin
  7. @RequestMapping("/orderToMember")
  8. public String orderToMember(String name);
  9. }

实现:

Feign: 通过继承 减少代码

  1. package com.toov5.api.feign;
  2.  
  3. import org.springframework.cloud.openfeign.FeignClient;
  4.  
  5. import com.toov5.api.service.IMemberService;
  6. //避免了冗余代码 直接过来就ok了
  7. @FeignClient("app-toov5-member")
  8. public interface MemberServiceFeign extends IMemberService {
  9. //实体类是存放接口项目还是存放在实现项目 实体类存放在接口项目里面
  10. //实体类和定义接口信息存放在接口项目
  11. //代码实现放在接口实现类里面
  12. }
  1. package com.toov5.api.service.impl;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import com.toov5.api.entity.UserEntity;
  8. import com.toov5.api.feign.MemberServiceFeign;
  9. import com.toov5.api.service.IOrderService;
  10.  
  11. @RestController
  12. public class OrderServiceImpl implements IOrderService {
  13. @Autowired
  14. private MemberServiceFeign memberServiceFeign;
  15.  
  16. @RequestMapping("orderToMmeber")
  17. public String orderToMember(String name) {
  18. UserEntity user = memberServiceFeign.getMember(name);
  19. return user==null ? "没有找到用户先关信息" : user.toString();
  20. }
  21. }
  1. package com.toov5.api;
  2.  
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
  6. import org.springframework.cloud.openfeign.EnableFeignClients;
  7.  
  8. @SpringBootApplication(scanBasePackages={"com.toov5.*"})
  9. @EnableEurekaClient
  10. @EnableFeignClients
  11. public class AppOrder {
  12. public static void main(String[] args) {
  13. SpringApplication.run(AppOrder.class, args);
  14. }
  15. }

yml:

  1. ###服务启动端口号
  2. server:
  3. port: 8001
  4. ###服务名称(服务注册到eureka名称)
  5. spring:
  6. application:
  7. name: app-toov5-order
  8. ###服务注册到eureka地址
  9. eureka:
  10. client:
  11. service-url:
  12. defaultZone: http://localhost:8100/eureka
  13.  
  14. ###因为该应用为注册中心,不会注册自己
  15. register-with-eureka: true
  16. ###是否需要从eureka上获取注册信息
  17. fetch-registry: true

  都启动后:

访问订单接口

按照这个规范目录去做 尤其@FeignClient要写到客户端里面 方便做服务降级

Feign默认开启的本地负载均衡~

Spring Cloud之Feigin客户端重构思想的更多相关文章

  1. spring cloud 自定义ribbon客户端

    一.自定义Ribbon客户端-[方式一]配置类 1.1.自定义负载规则 增加RibbonConfiguration.java配置类 public class RibbonConfiguration { ...

  2. 服务注册发现Eureka之三:Spring Cloud Ribbon实现客户端负载均衡(客户端负载均衡Ribbon之三:使用Ribbon实现客户端的均衡负载)

    在使用RestTemplate来消费spring boot的Restful服务示例中,我们提到,调用spring boot服务的时候,需要将服务的URL写死或者是写在配置文件中,但这两种方式,无论哪一 ...

  3. Spring Cloud之Feign客户端超时时间配置

    关于雪崩效应: 默认情况下tomcat只有一个线程去处理客户端发送的所有请求.高并发情况下,如果客户端请求都在同一接口,tomcat的所有线程池去处理,导致其他接口服务访问不了,等待. Tomcat有 ...

  4. Spring Cloud之Feign客户端调用工具

    feign介绍 Feign客户端是一个web声明式http远程调用工具,提供了接口和注解方式进行调用. Spring Cloud 支持 RestTemplate  Fetin Feign客户端实际开发 ...

  5. Spring Cloud配置中心客户端读取配置

    微服务连接配置中心来实现外部配置的读取. 引入依赖 <dependencies> <dependency> <groupId>org.springframework ...

  6. Spring Cloud Ribbon实现客户端负载均衡

    1.构建microservice-consumer-movie-ribbon项目,在pom.xml中引入ribbon依赖 在引入Eureka依赖的时候,默认里面含有ribbon依赖 2.添加@Load ...

  7. Spring Cloud Ribbon之URL重构(三)

    接着前面的说,前两篇中分析了解析和动态服务列表的获取,这两步完成后那接下来要做的事就是重组解析后的URL路径和发起通信了,这一步完成应该是在前面分析的RibbonLoadBalancerClient. ...

  8. Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了

    先摆解决方案: 方法一: 方法二: zuul.routes.<routeName>.sensitive-headers= zuul.routes.<routeName>.cus ...

  9. spring cloud 使用ribbon简单处理客户端负载均衡

    假如我们的multiple服务的访问量剧增,用一个服务已经无法承载, 我们可以把Hello World服务做成一个集群. 很简单,我们只需要复制Hello world服务,同时将原来的端口8762修改 ...

随机推荐

  1. sudo保持环境变量

    编译Linux内核的最后是make modules_install install,这两个一般都需要root权限,即sudo,而一般我交叉编译内核时都是在.bashrc中export ARCH=arm ...

  2. [译]GLUT教程 - 整合代码1

    Lighthouse3d.com >> GLUT Tutorial >> Input >> The Code So Far 以下是前面几节的完整整合代码: #inc ...

  3. 2018年EI收录中文期刊目录【转】

    [转]2018年EI收录中文期刊目录 Elsevier官网于2018年1月1日更新了EI Compendex目录,共收录中文期刊158种,其中新增期刊5种. 序号 中文刊名 收录情况 1 声学学报 保 ...

  4. 2018,从AI看安卓生态的变革

    AI的发展与影响 与传统技术不同的是,AI技术算法清晰,优化目标明确,基础技术成熟,使得一众中小创企也看到了市场的机会.2017年中国企业动作频频,在自动驾驶,智能安防,智慧城市等领域都取得了不俗的成 ...

  5. centos7防火墙--firewall

    centos7的防火墙变成firewall了 systemctl start firewalld.service#启动firewallsystemctl stop firewalld.service# ...

  6. 在Ubuntu下利用Eclipse开发FFmpeg配置小结

    首先需要编译FFmpeg得到头文件和lib文件,参见:在Ubuntu下编译FFmpeg 选择File-New-C Project 选择Executable下的Empty Project,右侧选择Lin ...

  7. PHP里的socket_recv方法解释

    以前一直经为PHP里没有低级的socket帧接收函数,看来是没看仔细,不过那些说明也太少了,(更令人气的里在英文版说明里的例子下有一句话:这个程序不能运行,因为没用listen函数,但在中文版里却没了 ...

  8. c#脚本控制shader

    如图所示,c#脚本控制shader颜色. public class ControlColor : MonoBehaviour { , , , ); public Material mat; publi ...

  9. PHP-Manual的学习----【语言参考】----【基本语法】

    2017年6月28日11:29:311.当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php 和 ?>,这告诉 PHP 开始和停止解析二者之间的代码.此种解析方式使得 PHP ...

  10. SET IDENTITY_INSERT ON/OFF 权限

    今天突然遇到了,找不到对象“XXXX”,因为它不存在或者没有您所需的权限,于是检查程序,突然发现程序中有一段代码是: SET IDENTITY_INSERT eticket ON //执行业务 ... ...