参考:http://www.ityouknow.com/springcloud/2017/05/12/eureka-provider-constomer.html
springboot版本:2.0.3.RELEASE
首先配置一个Erureka server,这里为了方便我们可以使用单机版并启动服务,参考上一篇文章《spring cloud Eureka server配置》
provider配置
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 非必须,监控用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
application:
name: spring-cloud-provider
server:
port: 8080
eureka:
client:
service-url:
defaultZone: http://localhost:8765/eureka
# spring boot信息,可选配置
info:
app:
name: spring-cloud-provider-wly
version: 1.0.0
启动类(添加@EnableDiscoveryClient注解,用于注册到Eureka)
package com.wang.eurekaprovider; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication
@EnableDiscoveryClient
public class EurekaproviderApplication { public static void main(String[] args) {
SpringApplication.run(EurekaproviderApplication.class, args);
}
}
controller
package com.wang.eurekaprovider.controller; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Author wly
* @Date 2018/6/27 16:15
*/
@RestController
@RequestMapping("/a")
public class HelloController {
@RequestMapping("/b")
public String hello(String name) {
return "Hello World!" + name;
}
}
启动服务并访问
http://localhost:8080/a/b?name=sb
页面展示(chrome浏览器)
Hello World!sb
consumer配置
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 引入客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- 引入feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!-- 非必须,监控用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
application.yml
spring:
application:
name: spring-cloud-consumer
server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8765/eureka
# spring boot信息,可选配置
info:
app:
name: spring-cloud-consumer-wly
version: 1.0.0
启动类(添加@EnableDiscoveryClient、@EnableFeignClients注解)
package com.wang.eurekaconsumer; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class EurekaconsumerApplication { public static void main(String[] args) {
SpringApplication.run(EurekaconsumerApplication.class, args);
}
}
调用接口定义
package com.wang.eurekaconsumer.remote; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; /**
* @FeignClient.name对应provider的spring.appliation.name
* @RequestMapping要和想调用的服务一致,方法名随意,参数名要一致(猜测是用Rest方式调用)
* @Author wly
* @Date 2018/6/27 17:08
*/
@FeignClient(name="spring-cloud-provider")
public interface HelloRemote {
@RequestMapping("/a/b")
public String hahaha(@RequestParam("name")String qqq);
}
controller
package com.wang.eurekaconsumer.controller; import com.wang.eurekaconsumer.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @Description
* @Author wly
* @Date 2018/6/27 17:16
*/
@RestController
@RequestMapping("/a")
public class HelloController {
@Autowired
private HelloRemote helloRemote; @RequestMapping("/c")
public String hello(String name) {
return helloRemote.hahaha(name);
}
}
启动服务并访问
http://localhost:8081/a/c?name=sb
页面展示(chrome浏览器)
Hello World!sb
调用结果和provider一致!
查看Eureka页面(发现服务成功注册)
正常流程:启动服务后应该先看监控注服务是否注册成功~
补充
如果使用不同端口启动多个provider,它会自动的进行负载均衡,有兴趣的同学可以尝试一下。
贴两张provider的图
下面是我同时启动两个provider,通过consumer访问后,provider的控制台效果