SpringBoot+SpringCloud+vue+Element开发项目——服务消费(Ribbon、Feign)
一、服务提供者
1、新建一个项目jansens-producer、添加依赖
- Swagger:API文档。
- Consul:注册中心。
- Spring Boot Admin:服务监控。
pom.xml
<properties> <swagger.version>2.9.2</swagger.version> <spring.boot.admin.version>2.0.4</spring.boot.admin.version> </properties> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!--spring-boot-admin--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring.boot.admin.version}</version> </dependency> <!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
2、配置文件
application.yml
server: port: 8003 spring: application: name: jansens-producer cloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name} # 注册到consul的服务名称 boot: admin: client: url: "http://localhost:8000" # 开放健康检查接口 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
3、启动类
JansensProducerApplication.java
@EnableDiscoveryClient @SpringBootApplication public class JansensProducerApplication { public static void main(String[] args){ SpringApplication.run(JansensProducerApplication.class,args); } }
4、自定义Banner
banner.txt
//////////////////////////////////////////////////////////////////// // _ooOoo_ // // o8888888o // // 88" . "88 // // (| ^_^ |) // // O\ = /O // // ____/`---'\____ // // .' \\| |// `. // // / \\||| : |||// \ // // / _||||| -:- |||||- \ // // | | \\\ - /// | | // // | \_| ''\---/'' | | // // \ .-\__ `-` ___/-. / // // ___`. .' /--.--\ `. . ___ // // ."" '< `.___\_<|>_/___.' >'"". // // | | : `- \`.;`\ _ /`;.`/ - ` : | | // // \ \ `-. \_ __\ /__ _/ .-` / / // // ========`-.____`-.___\_____/___.-`____.-'======== // // `=---=' // // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // // 佛祖保佑 上海研发 永无BUG // ////////////////////////////////////////////////////////////////////
5、添加控制器
HelloController.java
@RestController public class HelloController { @RequestMapping("/hello") public String hello(){ return "hello Jansens!"; } }
注意:为了模拟负载均衡,复制一份上面的项目取名为jansens-producer2,修改端口为8004,修改hello方法返回值为hello Jansens 2。依次启动注册中心、服务监控和两个服务提供者。依次访问http://localhost:8003/hello和http://localhost:8004/hello
二、服务消费者
1、新建一个项目jansens-consumer、添加依赖。
- Swagger:API文档。
- Consul:注册中心。
- Spring Boot Admin:服务监控。
pom.xml
<properties> <swagger.version>2.9.2</swagger.version> <spring.boot.admin.version>2.0.4</spring.boot.admin.version> </properties> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency> <!--spring-boot-admin--> <dependency> <groupId>de.codecentric</groupId> <artifactId>spring-boot-admin-starter-client</artifactId> <version>${spring.boot.admin.version}</version> </dependency> <!--consul--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-consul-discovery</artifactId> </dependency>
2、添加配置
application.yml
server: port: 8005 spring: application: name: jansens-consumer cloud: consul: host: localhost port: 8500 discovery: serviceName: ${spring.application.name} # 注册到consul的服务名称 boot: admin: client: url: "http://localhost:8000" # 开放健康检查接口 management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
3、启动类
JansensConsumerApplication.java
@EnableDiscoveryClient @SpringBootApplication public class JansensConsumerApplication { public static void main(String[] args){ SpringApplication.run(JansensConsumerApplication.class,args); } }
4、服务消费
添加消费服务测试类,添加两个接口,一个查询所有我们注册的服务,另一个从我们注册的服务中选取一个服务,采取轮询的方式。
ServiceController.java
@RestController public class ServiceController { @Autowired private LoadBalancerClient loadBalancerClient; @Autowired private DiscoveryClient discoveryClient; /** * 获取所有服务 */ @RequestMapping("/services") public Object services() { return discoveryClient.getInstances("jansens-producer"); } /** * 从所有服务中选择一个服务(轮询) */ @RequestMapping("/discover") public Object discover() { return loadBalancerClient.choose("jansens-producer").getUri().toString(); } }
访问http://localhost:8005/services、返回两个服务,分别是注册的8003、8004。反复访问http://localhost:8005/discover,结果交替的返回服务8003和8004,因为默认的负载均衡器采用的是轮询的方式,两个交替出现,从而实现了获取服务端地址的均衡负载。
CallHelloController.java
@RestController public class CallHelloController { @Autowired private LoadBalancerClient loadBalancer; @RequestMapping("/call") public String call() { ServiceInstance serviceInstance = loadBalancer.choose("jansens-producer"); System.out.println("服务地址:" + serviceInstance.getUri()); System.out.println("服务名称:" + serviceInstance.getServiceId()); String callServiceResult = new RestTemplate().getForObject(serviceInstance.getUri().toString() + "/hello", String.class); System.out.println(callServiceResult); return callServiceResult; } }
使用RestTemplate进行远程调用。
5、负载均衡器(Ribbon)
修改启动类注入RestTemplate并添加@LoadBalanced注解(用于拦截请求),以使用Ribbon来进行负载均衡。
JansensConsumerApplication.java
@Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); }
6、添加服务
RibbonHelloController.java
@RestController public class RibbonHelloController { @Autowired private RestTemplate restTemplate; @RequestMapping("/ribbon/call") public String call() { // 调用服务, service-producer为注册的服务名称,LoadBalancerInterceptor会拦截调用并根据服务名找到对应的服务 String callServiceResult = restTemplate.getForObject("http://jansens-producer/hello", String.class); return callServiceResult; } }
启动消费者服务,访问http://localhost:8005/ribbon/call
三、服务消费(Feign)
1、修改jansens-consumer的pom文件,添加feign依赖
pom.xml
<!--feign--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.1.RELEASE</version> </dependency>
2、修改启动类,添加@EnableFeignClients注解,开启扫描Spring Cloud Feign客户端的功能。
@EnableFeignClients @EnableDiscoveryClient @SpringBootApplication public class JansensConsumerApplication { public static void main(String[] args){ SpringApplication.run(JansensConsumerApplication.class,args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
3、添加Feign接口
JansensProducerService.java
@FeignClient(name = "mdh-producer") public interface JansensProducerService { @RequestMapping("/hello") public String hello(); }
4、添加控制器
FeignHelloController.java
@RestController public class FeignHelloController { @Autowired private JansensProducerService jansensProducerService; @RequestMapping("/feign/call") public String call() { // 像调用本地服务一样 return jansensProducerService.hello(); } }
5、启动项目并访问http://localhost:8005/feign/call
本文来自博客园,作者:小珍珠在河里敲代码,转载请注明原文链接:https://www.cnblogs.com/Jansens520/p/13658769.html