springcloud (Feign load balancing)

Build step

1.搭建pay-server工程
2.导入相关依赖,注册到EurekaServer  (拷贝order-server相关代码进行修改)
3.导入Feign的依赖
4.pay-server的主配置类开启Feign : @EnableFeignClients
5.pay-server编写Fiegn的客户端接口:UserFeignClient
6.pay-server编写controller注入UserFeignClient调用服务

1. What is Feign

What is the problem 1.1.Ribbon

Can be found in front of the other when we call API services through RestTemplate, the parameters needed to be spliced ​​in the URL request, if the parameter less, then perhaps we can live with, if there is more argument,Request string will splicing efficiency is low .

1.2. What is Feign
  • Feign Web Service client is a declarative, its aim is to make Web Service calls easier. Feign provides a template for HTTP requests, through a simple interface to write and insert annotations , you can define the parameters of good information HTTP request, format, address and so on. The Feign will be full proxy HTTP requests, we only need to call it like to call the same method can be completed service requests and related processing. Feign integration Ribbon and Hystrix (About Hystrix we revisit later), you can let us no longer need to explicitly use these two components.

2. build a new payment service registration to Eureka

2.1. Creating Project

1. Create a project-the Pay-Server-springcloud 1040 , to integrate Feign

2.2. Import dependencies
  • We let payment services registered with the registry before considering the integration Feign, pom.xml as follows
 <dependencies>
        <!--1.导入EurekaClient的包-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
		
        
        <!--web包-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--user-common-->
        <dependency>
            <groupId>cn.itsource.springboot</groupId>
            <artifactId>springcloud-user-common</artifactId>
        </dependency>
    </dependencies>
2.3. Following configuration class
/**
 * 支付的启动类
 * @EnableFeignClients :开启Feign支持
 */
@SpringBootApplication
@EnableEurekaClient
public class PayServerApplication1040
{

    public static void main( String[] args )
    {
        SpringApplication.run(PayServerApplication1040.class);
    }
}
2.4. Profile application.yml
#注册到EurekaServer
eureka:
  client:
    serviceUrl:
      defaultZone: http://peer1:1010/eureka/,http://peer2:1011/eureka/,http://peer3:1012/eureka/
  #使用ip地址进行注册
  instance:
    prefer-ip-address: true
    #要指定服务的实例ID
    instance-id:  pay-server:1040
server:
  port: 1040
spring:
  application:
    name: pay-server  #服务名

3. Integrated Feign

Examine documents: https://cloud.spring.io/spring-cloud-static/Greenwich.SR5/multi/multi_spring-cloud-feign.html#netflix-feign-starter

3.1. Join dependent
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
3.2. Turn Feign

Main configuration class label increases @EnableFeignClients


/**
 * 支付的启动类
 * @EnableFeignClients :开启Feign支持
 */
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients("cn.itsource.springboot.feignclient")
public class PayServerApplication1040
{


    public static void main( String[] args )
    {
        SpringApplication.run(PayServerApplication1040.class);
    }
}
3.3. Writing Feign client interface

Write a UserFeignClient class (used to invoke the User Service)

@FeignClient("user-server")
public interface UserFeignClient {

    //订单服务来调用这个方法      http://localhost:1020/user/10
    // @GetMapping(value = "/user/{id}" )
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    User getById(@PathVariable("id")Long id);
}
  • Explanation

UserFeignClient: This interface is used to call this Feign user-server service client interface
@FeignClient ( "user-server") : user-server is the name of the service, we want to swap customer service
feign according to the service name to find the target service according RequestMapping value method can be found on the controller target service method

  • Be careful:

1. The service name must not wrong
requestMapping method of controller of 2. @ RequestMapping and must target the same service
parameters 3. The method must be a method of controller parameters and target service as
4. The method must return value and the return value of the method as controller of the target service

  • Suggest

Service name to go directly to the target service configuration copy

Method directly copied from the target service controller in

Written Controller 3.4.
  • By injecting UserFeignClient, initiate calls directly
//支付服务的controller
@RestController
public class PayController{

    @Autowired
    private UserFeignClient userFeignClient;

    //浏览器来掉
    @RequestMapping("/pay/{id}")
    public User getById(@PathVariable("id")Long id){
        //使用Feign调用用户服务获取User
        return userFeignClient.getById(id);
    }
}
3.5. Test
  • Access to pay-server through a browser controller
Published 33 original articles · won praise 0 · Views 394

Guess you like

Origin blog.csdn.net/weixin_45737653/article/details/104976269