SpringCloud 服务注册中心 服务提供者 服务消费者

1. 建立“服务注册中心”

创建一个基础的Spring Boot工程,并在pom.xml中引入需要的依赖内容:

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

在主类中通过@EnableEurekaServer注解启动一个服务注册中心。

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

在默认设置下,该服务注册中心也会将自己作为客户端来尝试注册它自己,所以我们需要禁用它的客户端注册行为。在application.yml中配置

server:
      port: 9999
eureka:
       instance:
           hostname: 127.0.0.1
       server:
            enableSelfPreservation: false
       client:
            registerWithEureka: false
            fetchRegistry: false
            serviceUrl:
                     defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

这里,服务注册中心的端口是9999

2. 建立“服务提供方”

下面创建提供服务的客户端,并向服务注册中心注册自己。

首先,创建一个基本的Spring Boot应用,在pom.xml中,加入如下配置:

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

其次,实现test1接口

@RestController
public class TestController {

    @GetMapping("test1")
    public Object getUser(String userName) {
        logger.info("access test");
        User user = null;
        try{
            user = userServiceImpl.getByUserName(userName);
        } catch (Exception e) {

        }
        return user;
    }

最后在主类中通过加上@EnableDiscoveryClient注解,该注解能激活Eureka中的DiscoveryClient实现,才能实现Controller中对服务信息的输出。

@EnableDiscoveryClient
@EnableTransactionManagement
@SpringBootApplication
public class Provider1 {


    public static void main(String[] args) {
        //第一个简单的应用,
        SpringApplication.run(Provider1.class,args);
    }

}

在完成了服务内容的实现之后,再继续对application.properties做一些配置工作,具体如下:

server.port=18081

eureka.client.serviceUrl.defaultZone=http://192.168.20.144:9999/eureka/

spring.application.name=provider1

通过spring.application.name属性,我们可以指定微服务的名称后续在调用的时候只需要使用该名称就可以进行服务的访问。

eureka.client.serviceUrl.defaultZone属性对应服务注册中心的配置内容,指定服务注册中心的位置。

这样就可以在注册中心localhost:9999中看到名称为provider1的服务了

3. 建立“服务消费者”(Feign)

创建一个Spring Boot工程,配置pom.xml,将上述的配置中的ribbon依赖替换成feign的依赖即可,具体如下:

<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>

在应用主类中通过@EnableFeignClients注解开启Feign功能,具体如下:

@EnableDiscoveryClient
@EnableFeignClients
@EnableScheduling
@SpringBootApplication
public class Consumer1 {

    public static void main(String[] args) {
        //第一个简单的应用,
        SpringApplication.run(Consumer1.class,args);
    }
}

定义服务的接口,具体如下:

@FeignClient(name="provider1",fallback = UserFeignClientServiceFallback.class)
@Service("userFeignClientService")
public interface UserFeignClientService{
    @GetMapping("test1")
    Object getUser(@RequestParam("userName") String userName);
}

  • 使用@FeignClient("provider1")注解来绑定该接口对应provider1服务
  • 使用@GetMapping声明服务调用的接口test1,不需要实现。

在web层中调用上面定义的ComputeClient,具体如下:

@RestController
public class TestController {
    @Autowired
    private UserFeignClientService userFeignClientService;
    
    @GetMapping("test")
    public Object test(String userName){
        logger.info("hello world!!");
        Object map = userFeignClientService.getUser(userName);
        return map;
    }

这样就可以通过@Autowired注解,实现了在注册中心发现服务并调用的过程。

application.properties中不用变,指定eureka服务注册中心即可,如:

server.port=19090

eureka.client.serviceUrl.defaultZone=http://192.168.20.144:9999/eureka/

spring.application.name=consumer1

以上,通过Feign以接口和注解配置的方式,轻松实现了对provider1服务的绑定,并且可以在本地应用中像本地服务一下的调用它,并且做到了客户端均衡负载。

posted @ 2018-11-07 17:23  benl_cs  阅读(228)  评论(0)    收藏  举报