eureka初探--单机启动-RPC方式(Feign)

RPC方式需要添加-api模块(定义接口,报文),服务提供方和消费方双方均需要引用此模块依赖,且双方需引入org.springframework.cloud:spring-cloud-starter-feign依赖。

以下绿色注明RPC方式与REST方式的区别

parent:

<modules>
    <module>eurekaServer</module>
    <module>eurekaClient</module>
    <module>eurekaClient-api</module>
    <module>eurekaConsumer</module>
</modules>
<springcloud.version>Dalston.SR5</springcloud.version>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${springcloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

eureka Server (服务治理) :

pom

<artifactId>eurekaServer</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        <version>1.4.2.RELEASE</version>
    </dependency>
</dependencies>

配置

server.port:8761

eureka.instance.hostname: localhost
eureka.client.registerWithEureka: false
eureka.client.fetchRegistry: false
eureka.client.serviceUrl.defaultZone: http://localhost:8761/eureka/

启动

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

 

eureka Client (服务提供)

pom

<artifactId>eurekaClient</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.dragon.test</groupId>
        <artifactId>eurekaClient-api</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

配置

server.port=18765
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

spring.application.name: eureka-client-first

启动

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.eureka.test.api"})
public class EurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaClientApplication.class, args);
    }
}

具体服务

@RestController
public class TestController implements TestAPI {
    @PostMapping("/test/hello")
    public Map<String, Object> hello(@RequestBody String word){
        Map<String,Object> result = new HashMap<String,Object>();
        result.put("name", "lulu");
        result.put("age", "18");
        result.put("word", word);
        return result;
    }
}

 

eureka Client API (服务接口)

pom

<artifactId>eurekaClient-api</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.2.RELEASE</version>
    </dependency>
</dependencies>

接口

package com.eureka.test.api;
@FeignClient("eureka-client-first")
public interface TestAPI {
    @PostMapping("/test/hello")
    Map<String, Object> hello(@RequestBody String word);
}

 

eureka Consumer (服务消费)

pom

<artifactId>eurekaConsumer</artifactId>
<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        <version>1.4.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
    </dependency>
    <dependency>
        <groupId>com.dragon.test</groupId>
        <artifactId>eurekaClient-api</artifactId>
        <version>${project.version}</version>
    </dependency>
</dependencies>

配置

server.port=18766
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=true
eureka.client.fetchRegistry=true
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/

spring.application.name: eureka-client-consumer

启动

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients(basePackages = { "com.eureka.test.api"})
public class EurekaConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaConsumerApplication.class, args);
    }
}

具体服务调用

@RestController
public class TestController {

    @Autowired
    TestAPI testAPI;

    @RequestMapping("/test/hello")
    public Map<String, Object> hello(@RequestParam String word){
        return testAPI.hello(word);
    }
}

转载于:https://my.oschina.net/u/2424132/blog/1863400

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值