java版b2b2c社交电商spring cloud分布式微服务源码请加企鹅求求:一零三八七七四六二六。上一篇文章咱们介绍了eureka服务注册中心的搭建,这篇文章介绍一下如何使用eureka服务注册中心,搭建一个简单的服务端注册服务,客户端去调用服务使用的案例。java
案例中有三个角色:服务注册中心、服务提供者、服务消费者,其中服务注册中心就是咱们上一篇的eureka单机版启动既可,流程是首先启动注册中心,服务提供者生产服务并注册到服务中心中,消费者从服务中心中获取服务并执行。web
服务提供spring
咱们假设服务提供者有一个hello方法,能够根据传入的参数,提供输出“hello xxx,this is first messge”的服务浏览器
一、pom包配置springboot
建立一个springboot项目,pom.xml中添加以下配置:bash
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
复制代码
二、配置文件app
application.properties配置以下:负载均衡
spring.application.name=spring-cloud-producer
server.port=9000
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
复制代码
参数在上一篇都已经解释过,这里很少说。分布式
三、启动类spring-boot
启动类中添加@EnableDiscoveryClient
注解
@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {
public static void main(String[] args) {
SpringApplication.run(ProducerApplication.class, args);
}
}
复制代码
四、controller
提供hello服务
@RestController
public class HelloController {
@RequestMapping("/hello")
public String index(@RequestParam String name) {
return "hello "+name+",this is first messge";
}
}
复制代码
添加@EnableDiscoveryClient
注解后,项目就具备了服务注册的功能。启动工程后,就能够在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。
到此服务提供者配置就完成了。
服务调用
一、pom包配置
和服务提供者一致
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
复制代码
二、配置文件
application.properties配置以下:
spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
复制代码
三、启动类
启动类添加@EnableDiscoveryClient
和@EnableFeignClients
注解。
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerApplication.class, args);
}
}
复制代码
@EnableDiscoveryClient` :启用服务注册与发现
@EnableFeignClients`:启用feign进行远程调用
Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,而后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign能够与Eureka和Ribbon组合使用以支持负载均衡。
四、feign调用实现
@FeignClient(name= "spring-cloud-producer")
public interface HelloRemote {
@RequestMapping(value = "/hello")
public String hello(@RequestParam(value = "name") String name);
}
复制代码
name:远程服务名,及spring.application.name配置的名称
此类中的方法和远程服务中contoller中的方法名和参数需保持一致。
五、web层调用远程服务
将HelloRemote注入到controller层,像普通方法同样去调用便可。
@RestController
public class ConsumerController {
@Autowired
HelloRemote HelloRemote;
@RequestMapping("/hello/{name}")
public String index(@PathVariable("name") String name) {
return HelloRemote.hello(name);
}
}
复制代码
到此,最简单的一个服务注册与调用的例子就完成了。
测试
简单调用
依次启动spring-cloud-eureka、spring-cloud-producer、spring-cloud-consumer三个项目
先输入:http://localhost:9000/hello?name=neo
检查spring-cloud-producer服务是否正常
返回:hello neo,this is first messge
说明spring-cloud-producer正常启动,提供的服务也正常。
浏览器中输入:http://localhost:9001/hello/neo
返回:hello neo,this is first messge
说明客户端已经成功的经过feign调用了远程服务hello,而且将结果返回到了浏览器。
经典介绍