java version b2b2c social electricity supplier spring cloud distributed micro-services (c) the service consumer (Feign)

A, Feign About
Feign is a pseudo Http client a declarative, it makes writing easier Http client. Using Feign, only you need to create an interface and annotation. It has pluggable annotation feature can be used Feign JAX-RS annotations and notes. Feign pluggable encoder and decoder. Feign default integrated Ribbon, and Eureka and combined, the default implementation of load balancing.

in short:

Feign uses annotation-based interface
Feign integrated ribbon
Second, prepare for work
to continue to spend a project, start eureka-server, port 8761; start service-hi twice, ports are 8762, 8773.

Third, create a service feign
a new spring-boot project, named serice-feign, in its pom-dependent file introduced Feign start spring-cloud-starter-feign, Eureka start dependent spring-cloud-starter-eureka , Web start dependent spring-boot-starter-web, code 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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.forezp</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>service-feign</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Dalston.RC1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

In the project's profile application.yml file, specify the program called service-feign, port number 8765, registered service address is http: // localhost: 8761 / eureka /, the following code:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

In the startup class ServiceFeignApplication program, plus @EnableFeignClients comment open Feign features:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServiceFeignApplication.class, args);
    }
}

Feign a defined interface through @ FeignClient ( "service name"), which specifies the service call. For example, to call the "/ hi" Interface service-hi services in the code, the code is as follows:

/**
 * Created by fangzhipeng on 2017/4/6.
 */
@FeignClient(value = "service-hi")
public interface SchedualServiceHi {
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

In the controller layer Web layer, external exposure to a "/ hi" API interface, Feign customers through the above-defined end SchedualServiceHi to consumer services. code show as below:

@RestController
public class HiController {
 
    @Autowired
    SchedualServiceHi schedualServiceHi;
    @RequestMapping(value = "/hi",method = RequestMethod.GET)
    public String sayHi(@RequestParam String name){
        return schedualServiceHi.sayHiFromClientOne(name);
    }
}

spring cloud b2b2c e-commerce social platform source code, please add penguin beg: three four five three six II qi II fifty-nine

Guess you like

Origin blog.csdn.net/qq_42748864/article/details/93464372