Fork me on GitHub

SpringCloud个人笔记-02-Feign初体验

 

  • 项目结构

 

 

 

  • sb_cloud_product
<?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.huarui</groupId>
    <artifactId>sb_cloud_product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sb_cloud_product</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.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>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

    <dependencies>

        <!-- eureka client -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- ribbon依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</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>${spring-cloud.version}</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>

</project>
pom.xml
server:
  port: 9000

spring:
  application:
    name: sb-cloud-product

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
application.yml

 

@SpringBootApplication
@EnableDiscoveryClient //开启服务注册
@EnableFeignClients   //开启Feign
public class SbCloudProductApplication {
public static void main(String[] args) { SpringApplication.run(SbCloudProductApplication.class, args); } }

 


 

  • sb_cloud_order

 

 

<?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.huarui</groupId>
    <artifactId>sb_cloud_order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>sb_cloud_order</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.13.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>
        <spring-cloud.version>Edgware.SR3</spring-cloud.version>
    </properties>

    <dependencies>

        <!-- eureka client -->
        <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>


        <!-- ribbon依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</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>${spring-cloud.version}</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>

</project>
pom.xml
server:
  port: 9001

spring:
  application:
    name: sb-cloud-order

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
application.yml

 

@Configuration
public class RestTemplateConfig {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

}

 

 

@FeignClient(name = "sb-cloud-product")
public interface ProductRemote {

    /**
     * name:远程服务名,即spring.application.name配置的名称
     *类中的方法和远程服务中contoller中的方法名和参数需保持一致
     *
     * @return
     */

    @GetMapping(value = "/product/getMsg")
    public Map<String,String> getMsg();
}

 

 

  • RestTemplate三种访问服务端口接口方法

 


@RestController
@RequestMapping("/order")
public class OrderCtrl {

@Autowired
private LoadBalancerClient loadBalancerClient;

@Autowired
private RestTemplate restTemplate2;

@Autowired
private ProductRemote productRemote;

/**
* RestTemplate 三种使用方式
* @return
*/
@RequestMapping("/getProduct")
public Map<String,String> getProduct(){

//http://localhost:9000/product/getMsg

RestTemplate restTemplate = new RestTemplate();
//1.第一种方式(直接通过地址访问)
//Map<String,String> map = restTemplate.getForObject("http://localhost:9000/product/getMsg", Map.class);

//2.第二种方式(利用loadBalancerClient通过应用名获取url)
/*ServiceInstance serviceInstance = loadBalancerClient.choose("sb-cloud-product");
String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort() + "/product/getMsg");
Map<String,String> map = restTemplate.getForObject(url, Map.class);*/

//3.第三种方式(通过@LoadBalanced注解让restTemplate获得负载均衡能力 访问接口时使用应用名字)
Map<String,String> map = restTemplate2.getForObject("http://sb-cloud-product:9000/product/getMsg", Map.class);

return map;

}


  /* 通过feign 远程访问sb_cloud_product 服务 */
@GetMapping("/feign/getProduct")
public Map<String,String> feignGetProduct(){
return productRemote.getMsg();
}


}
 

 

posted @ 2019-02-26 16:57  youxiu326  阅读(166)  评论(0编辑  收藏  举报