pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <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-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-ribbon</artifactId> </dependency>
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
|
application.yml
1 2 3 4 5 6 7 8 9 10 11 12 13
| server: port: 7004
eureka: instance: hostname: localhost client: serviceUrl: defaultZone: http://localhost:7001/eureka/ #注册中心的地址
spring: application: name: service-consumer #服务的名字
|
Ribbon
RibbonService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.linjian.serviceconsumer.service;
public interface RibbonService {
String hello(String name); }
|
RibbonServiceImpl.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.linjian.serviceconsumer.service.impl;
import com.linjian.serviceconsumer.service.RibbonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;
@Service public class RibbonServiceImpl implements RibbonService { @Autowired RestTemplate restTemplate;
@Override public String hello(String name) { return restTemplate.getForObject("http://service-producer/hello?name=" + name, String.class); } }
|
Feign
FeignExampleService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.linjian.serviceconsumer.service;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "service-producer") public interface FeignExampleService {
@GetMapping("hello") public String hello(@RequestParam(value = "name") String name); }
|
启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.linjian.serviceconsumer;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;
@SpringBootApplication @EnableDiscoveryClient @EnableFeignClients @EnableEurekaClient public class ServiceConsumerApplication {
public static void main(String[] args) { SpringApplication.run(ServiceConsumerApplication.class, args); }
@Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
|
ConsumerController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package com.linjian.serviceconsumer.controller;
import com.linjian.serviceconsumer.service.FeignExampleService; import com.linjian.serviceconsumer.service.impl.RibbonServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController public class ConsumerController { @Resource private FeignExampleService feignExampleService;
@Autowired RibbonServiceImpl ribbonServiceImpl;
@GetMapping("/hello/{name}") public String hello(@PathVariable("name") String name) { return ribbonServiceImpl.hello(name); }
@GetMapping("/helloFeign/{name}") public String helloFeign(@PathVariable("name") String name) { return feignExampleService.hello(name); }
}
|
启动eureka-server、service-producer、service-consumer
调用http://localhost:7004/hello/linjian
http://localhost:7004/helloFeign/linjian