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默認開啟的本地負載均衡~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM