springCloud服务提供与调用


由于项目从单体向微服务拆分,所以准备SpringCloud里面的基础组件好好复习一遍!

这几就从Spring Cloud 的Feign 开始,构建一个eureka服务注册中心,一个服务端注册服务,客户端去调用服务的简单案例。

角色共有: 注册中心,服务提供方,服务消费方,注册中心eureka单机版的。具体流程就是 先启动注册中心eureka,然后服务提供者生产服务并注册到服务中心去,消费方从服务中心获取服务并执行。

#注册中心 eureka#

1.pom 依赖


<dependencies>
       <dependency>
           <groupId>org.springframework.cloud</groupId>
           <artifactId>spring-cloud-starter-eureka-server</artifactId>
       </dependency>
       <dependency>
           <groupId>org.springframework.boot</groupId>
           <artifactId>spring-boot-starter-test</artifactId>
           <scope>test</scope>
       </dependency>
   </dependencies>

2.添加注解 @EnableEurekaServer

package com.neo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @author coderpwh
 * @create 2019-06-14 22:52
 * @desc ${DESCRIPTION}
 **/
@SpringBootApplication
@EnableEurekaServer
public class SpringCloudEurekaApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudEurekaApplication.class, args);
    }
}

** 3.配置文件**

spring.application.name=spring-cloud-eureka
server.port=8000
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
  • eureka.client.register-with-eureka :表示是否将自己注册到Eureka Server,默认为true。
  • eureka.client.fetch-registry :表示是否从Eureka Server获取注册信息,默认为true。
  • eureka.client.serviceUrl.defaultZone :设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8000/eureka ;多个地址可使用 , 分隔。

生产方

1.pom 依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

2.添加注解:@EnableDiscoveryClient

package com.neo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class ProducerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ProducerApplication.class, args);
	}

}

3.配置文件

spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

4.提供 hello服务

package com.neo.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author coderpwh
 * @create 2019-06-14 23:36
 * @desc ${DESCRIPTION}
 **/
@RestController
public class HelloController {

   /* @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is first messge";
    }*/

    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is producer 2  send first messge";
    }
}

添加@EnableDiscoveryClient注解后,项目就具有了服务注册的功能。启动工程后,就可以在注册中心的页面看到SPRING-CLOUD-PRODUCER服务。

消费方

1.pom 依赖如上


<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-feign</artifactId>
	</dependency>
	<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>

** 2.配置文件**

spring.application.name=spring-cloud-consumer
server.port=9001
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

3.启动类

@EnableDiscoveryClient:启用服务注册与发现
@EnableFeignClients :启用feign进行远程调用


package com.neo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;


/**
 * @author coderpwh
 * @create 2019-06-16 14:35
 * @desc ${DESCRIPTION}
 **/

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

Feign是一个声明式Web Service客户端。使用Feign能让编写Web Service客户端更加简单, 它的使用方法是定义一个接口,然后在上面添加注解,同时也支持JAX-RS标准的注解。Feign也支持可拔插式的编码器和解码器。Spring Cloud对Feign进行了封装,使其支持了Spring MVC标准注解和HttpMessageConverters。Feign可以与Eureka和Ribbon组合使用以支持负载均衡

4.feign调用实现

package com.neo.remote;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author coderpwh
 * @create 2019-06-16 14:42
 * @desc ${DESCRIPTION}
 **/
@FeignClient(name="spring-cloud-producer")
public interface HelloRemote {

    @RequestMapping(value = "/hello")
    public   String hello(@RequestParam(value = "name") String name);

}

其中 name=”spring-cloud-producer”
是远程服务名,与spring.application.name配置一样
参数必须保持一致

5.controller 远程服务调用,和调用普通方法一样。

package com.neo.controller;

import com.neo.remote.HelloRemote;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author coderpwh
 * @create 2019-06-16 14:50
 * @desc ${DESCRIPTION}
 **/
@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,并且将结果返回到了浏览器。

负载均衡

以上面spring-cloud-producer为例子修改,将其中的controller改动如下:

@RestController
public class HelloController {
	
    @RequestMapping("/hello")
    public String index(@RequestParam String name) {
        return "hello "+name+",this is producer 2  send first messge";
    }
}

配置文件

spring.application.name=spring-cloud-producer
server.port=9003
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/

打包启动后,在eureka就会发现两个服务提供者,如下图:


文章作者: coderpwh
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 coderpwh !
  目录