springcloud服务提供producer and 服务调用consumer

---------------------------------producer-------------------------------------------

1.pom文件中,作为客户端的jar包

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

2.application.properties添加

#生产者名称
spring.application.name=spring-cloud-producer
#端口号
server.port=9000
#注册中心地址
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3.启动类

@SpringBootApplication
// 启用服务注册与发现
@EnableDiscoveryClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(DemoApplication.class);
        springApplication.run(args);
    }

}

4.controller

@RestController
public class HelloProvider {

    /**
     * Description:提供服务接收参数
     * @author bing
     * 2018年3月30日
     */
    @RequestMapping("/helloProducer")
    public String HelloProvider(@RequestParam String name) {
        return "hello " + name + ",this is first messge";
    }

}

---------------------------------consumer-------------------------------------------

1.pom文件添加

什么是feign 英 [feɪn]?

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡。

        <!-- eureka -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>
        
        <!-- 添加feign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
            <version>1.4.4.RELEASE</version>
        </dependency>

2.application.properties

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3.接口

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

@FeignClient(name= "spring-cloud-producer")
public interface IHelloConsumer {
    @RequestMapping(value = "/helloProducer")
    public String hello(@RequestParam(value = "name") String name);
}

4.controller

将编写的接口采用autowired的方式注入

@RestController
public class HelloConsumer {

    @Autowired
    private IHelloConsumer helloConsumer;

    @RequestMapping("/getMessage")
    public String getMessage(String name){
        return helloConsumer.hello(name);
    }

}

5.启动类

@SpringBootApplication
//// 启用服务注册与发现
@EnableDiscoveryClient
// 启用feign进行远程调用
@EnableFeignClients
public class SpringbootUploadApplication {

    // 启动主方法
    public static void main(String[] args) {
        SpringApplication.run(SpringbootUploadApplication.class, args);
    }
    
}

 


*******各个工程的springboot版本号一定要一致

依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目

都启动成功后,浏览器输入:http://localhost:9001/getMessage?name=bing

返回:hello bing,this is first messge

成功!

posted @ 2018-04-02 13:49  在谷歌上百度  阅读(1040)  评论(0编辑  收藏  举报