Spring Cloud - 服务发现、注册、调用(三)

Spring Cloud - 服务发现、注册、调用

  • Spring Cloud - 服务发现、注册、调用(一)(eureka server)
  • Spring Cloud - 服务发现、注册、调用(二)(eureka client(provider))
  • Spring Cloud - 服务发现、注册、调用(三)(eureka client(consumer))
    代码参见https://github.com/happut/springbootdemo

eureka client (consumer)

下面我们编写下服务调用例子:

建立springboot项目,增加spring-cloud-starter-eureka-server起步依赖:



    4.0.0

    com.github.happut
    springboot-demo-eureka-server
    0.0.1-SNAPSHOT
    jar

    springboot-demo-eureka-server
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.8.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
        Edgware.RELEASE
    

    
        
            org.springframework.cloud
            spring-cloud-starter-eureka-server
        
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
            runtime
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




启动代码中增加@EnableEurekaClient以及RestTemplate

@SpringBootApplication
@EnableEurekaClient
public class SpringbootDemoEurekaClientApplication {

    @LoadBalanced
    @Bean
    public RestTemplate createRestTemplate() {
        return new RestTemplate();
    }

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

application.properties(yml请注意缩进)

server.port=1113

spring.application.name=hello-service

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka

这里需要注意下端口不要和主服务器冲突,否则无法启动。

spring.application.name代表服务名称,注意不要和服务提供者一致,否则接口调用的时候就懵逼了。

eureka.client.serviceUrl.defaultZone务必要和主服务器一致,否则不知道去哪里拉取服务。

在这里为了演示,我们尝试调用服务提供者的接口:

首先,如果不用spring cloud eureka,咱们一般这么写:

@RestController
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String index() {
        return restTemplate.getForObject("http://localhost:1112/hello", String.class);
    }
}

可以看到,我们的服务器提供者的地址被硬编码到代码中,这可不好,万一1112服务器down了,或者服务器迁移还得改代码。

当我们用了spring cloud框架后会怎样呢,代码这么写:

@RestController
public class HelloController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/hello")
    public String index() {
        return restTemplate.getForObject("http://hello-service-provider/hello", String.class);
    }
}

可以看到一些变化,地址不是ip端口,而是改为http://服务名称/接口路由。

服务名称!!!

服务名称!!!

服务名称!!!

不会告诉你我被这个坑了好几天

然后乐呵启动,看下主服务器中的变化:


image.png

我们可以简单测试下接口:


Spring Cloud - 服务发现、注册、调用(三)_第1张图片
image.png

接口也调用成功了哟,是1113服务通过主服务器调用的1112的接口,神奇吧。

你可能感兴趣的:(Spring Cloud - 服务发现、注册、调用(三))