springcloud-feign call service

Here we need to say that feign is just an implementation that conforms to our interface calls, and it is not necessary to use this technology

Transform the springcloud-custom ribbon load balancing algorithm on the basis of the following code

1. Modify the springcloud-api file

1. Add dependencies

Add the dependency of feign, the complete pom file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-api</artifactId>

<dependencies>
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.18</version>
    </dependency>

    <!-- mybatis_plus -->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.0.5</version>
    </dependency>

    <!-- feign -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.6.RELEASE</version>
    </dependency>
</dependencies>
</project>

2. Write feignservice and call the service through feign

Create a new service folder and create a UserFeignService interface under the folder. The
specific code is as follows:

package com.hzxy.springcloud.service;


import com.hzxy.springcloud.model.User;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Component
@FeignClient(value = "springcloud-provider-user")      // value 参数是服务提供者的服务名称
public interface UserClientService {
    
    

    // 请求提供者的url
    @RequestMapping("/hello")
    public List<User> Hi();
}

2. Write new consumer services

Most of the code is the same as springcloud-consumer-user-80, for the sake of difference, a new one is created

1. Create a new maven project, springcloud-consumer-user-feign

Insert picture description here

2. Import dependencies

The pom file is as follows

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <artifactId>springcloud</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer-user-feign</artifactId>

    <dependencies>

        <!-- ribbon -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.6.RELEASE</version>
        </dependency>

        <!-- 我们需要拿到实体类 ,所以配置api module -->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <!-- springboot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 热部署工具 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- 数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

    </dependencies>
</project>

3. Write the configuration file

server:
  port: 80
spring:
  application:
    name: springcloud-consumer-user
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
    username: root
    password: 520China

# Eureka配置
eureka:
  client:
    register-with-eureka: false # 不向Eureka注册自己
    service-url:
      defaultZone: http://ybg.eureka1.com:7001/eureka/,http://ybg.eureka2.com:7002/eureka/   # 向哪个地方获取Eureka服务

4. Create a new config folder and write the ConfigBean class

code show as below

package com.hzxy.springcloud.config;

import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ConfigBean {
    
    

    // @Configuration -- spring applicationContext.xml
    // 配置负载均衡实现RestTemplate
    // RoundRobinRule 轮询
    // RandomRule 随机
    // AvailabilityFilteringRule  会先过滤掉,跳闸,访问故障的服务,对剩下的进行轮询
    // RetryRule  会先按照轮询获取服务,如果服务获取失败,则会在指定时间内进行,重试
    @Bean
    @LoadBalanced     //ribbon
    public RestTemplate getRestTemplate(){
    
    
        return new RestTemplate();
    }
}

5. Create a new controller folder and write the UserController class

code show as below

package com.hzxy.springcloud.controller;

import com.hzxy.springcloud.model.User;
import com.hzxy.springcloud.service.UserClientService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;


@RestController
public class UserController {
    
    

//    @Resource
//    private RestTemplate restTemplate;      //提供多种便捷访问远程http服务方法,简单的Restful服务模板

//    private static final String REST_URL = "http://localhost:8001/hello";
    //ribbon 我们这里的地址,应该是一个变量,通过服务名来访问
//    private static final String REST_URL = "http://springcloud-provider-user/hello";
//
//
//    @RequestMapping("/hi")
//    public List<User> Hi(){
    
    
//        return restTemplate.getForObject(REST_URL,List.class);
//    }
    @Resource
    private UserClientService userClientService;

    @RequestMapping("/hi")
    public List<User> Hi(){
    
    
        return this.userClientService.Hi();
    }

}

6. Write the startup class

code show as below

package com.hzxy.springcloud;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;



// ribbon 和 Eureka 整合以后,客户端可以直接调用,不用关心ip地址和端口号
@SpringBootApplication
@EnableEurekaClient  //自动配置
@EnableFeignClients(basePackages = {
    
    "com.hzxy.springcloud"})
public class FeignConsumer_80 {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(FeignConsumer_80.class,args);
    }
}

3. Test

Request http://ybg.eureka1.com/hi again, after a while, she can appear the following page, the test is successful 1
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/114294972