四、写一个订单微服务调用会员服务的接口

2018-07-17  本文已影响593人  joy_蓝蜘蛛

订单微服务

需要用到技术说明
1.Rest:微服务之间接口的相互调用框架.feign也是微服务间接口的相互高地用框架
2.ribbon是实现负载均衡的
3.zuul接口网关
4.eureka服务注册

一、订单项目目录

image.png

二、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.joychen</groupId>
    <artifactId>euorder</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>euorder</name>
    <description>euorder</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.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>Finchley.M8</spring-cloud.version>
    </properties>

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

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


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

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

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

二、application.yml配制信息

spring.application.name这个名称是注册全Eureka当中的微服务名称。以后别的微服务要调用是通过服务名称来时行调用的。包括后面要配制负载均衡。也是跟据名称来。
server.port 这个是当前微服务启动的端口号
eureka.instance.instance-id:是当前微服务提供的实际地址
eureka.client.service-url.defaultZone:这个是当前微服务项目注测到注册中心的地址

spring:
  application:
    name: order-service
  cloud:
    config:
      discovery:
        enabled: true
        service-id: order-server
server:
  port: 7880
eureka:
  instance:
    hostname: localhost
    instance-id: ${spring.application.name}:${spring.cloud.client.hostname}:${server.port}
    prefer-ip-address: false
  client:
    service-url:
      defaultZone: http://localhost:7878/eureka/

三、在服务层调用会员微服务接口

image.png

return restTemplate.getForObject("http://manber-server/member",List.class);
在上面这个代码中http://后面的是在注册中心注册的名字。而不是ip地址。

package com.joychen.euorder.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderService {

        @Autowired
        private RestTemplate restTemplate;

        public List<String> getAllUsers() {
            return restTemplate.getForObject("http://manber-server/member",List.class);
        }

}

四、在控制层调用服务层

image.png
package com.joychen.euorder.controllers;

import com.joychen.euorder.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;


    @RequestMapping("/getAllUsers")
    public List<String> getAllUsersMember(){

        return orderService.getAllUsers();
    }


}

五、在EuorderApplication中启动

image.png
package com.joychen.euorder;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableEurekaClient
public class EuorderApplication {

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

    @Bean
    @LoadBalanced
    RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

六、项目启动

启动后会发现Eureka中多了一个微服务。


image.png

七、测试连接

image.png
http://localhost:7880/getAllUsers

这个连接中的用户信息是会员微服务中提供的。

上一篇下一篇

猜你喜欢

热点阅读