xl_echo编辑整理,欢迎转载,转载请声明文章来源。更多IT编程案例、资料请联系QQ:1280023003 ,群:298140694
百战不败,依不自称常胜,百败不颓,依能奋力前行。——这才是真正的堪称强大!!
重点声明,这里不需要eureka集群,只需要一个即可。
需要创建一个服务消费者,并且希望能够完美按照案例运行,请先创建一个注册中心(参考:https://blog.csdn.net/xlecho/article/details/80614496),同时需要一个服务提供者和(参考:https://blog.csdn.net/xlecho/article/details/80620344)。并且拥有以下环境。
开发工具 | 版本 |
---|---|
JDK | 1.8 |
Maven | maven-3.1.1 |
IDEA | IntelliJ IDEA 2018.1.3 x64 |
Notepad | Notepad++ |
服务的消费者创建一个简单的springboot即可,创建成功之后,pom.xml内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.echo.demo</groupId>
<artifactId>demo_client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>ribbon-consumer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.13.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然后在主类上加入注解@EnableDiscoveryClient
,这样才可以访问到服务,并注入RestTemplat。主类代码:
package com.echo.demo;
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;
/**
* Created by Xlecho on 2018/6/13/013
*/
@EnableDiscoveryClient
@SpringBootApplication
public class RibbonConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(RibbonConsumerApplication.class, args);
}
@LoadBalanced//表示开启客户端负载均衡
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
}
对配置文件进行配置
spring.application.name=ribbon-consumer
server.port=9000
eureka.client.service-url.defaultZone=http://demo1:1111/eureka
这个时候基本工作都完成了,就可以直接来调用服务啦。这里使用RestTemplate调用服务,我们在主类加载了它,这里直接注入使用即可。
package com.echo.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* Created by Xlecho on 2018/6/13/013
*/
@RestController
public class ConsumerController {
@Autowired
RestTemplate restTemplate;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET)
public String helloController() {
return restTemplate.getForEntity("http://HELLO-SERVICE/hello", String.class).getBody();
}
}
启动注册中心可以看到我们的服务提供者注册上了,同时我们的服务消费者也注册上了,这里声明一下,其实服务提供者,也可以是消费者。(看到这张图的前提是,你要启动这些服务)
然后访问我们的服务消费者,访问controller提供的访问接口,可以看到我们打印出来了服务提供者的返回信息。
到这就成功啦!
RestTemplate调用服务报错了,看看这:https://blog.csdn.net/xlecho/article/details/80682097