1. 程式人生 > >搭建SpringCloud註冊中心(Eureka)之消費者和服務提供者

搭建SpringCloud註冊中心(Eureka)之消費者和服務提供者

一、前言

使用rpc遠端呼叫技術(SpringCloud)搭建如下圖,其中所用到的技術有maven、eureka、ribbon、SpringBoot等
這裡寫圖片描述

二、準備環境

作業系統:win7
JDK:1.8

三、搭建SpringCloud註冊中心(Eureka)之服務提供者

1) 在搭建Eureka(註冊中心)基礎下
2) 建立簡單Maven專案
這裡寫圖片描述
3) pom檔案新增所需依賴

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId
>
spring-boot-starter-parent</artifactId> <version>1.5.2.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> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId
>
spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</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>Dalston.RC1</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> <!-- 注意這裡添加了遠端庫,其中maven倉庫設定 settings.xml與pom.xml 優先順序三者的級先是 pom.xml > /home_dir/.m2/settings.xml > /maven_dir/conf/settings.xml --> <repositories> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories>

4)在src/mainresources/下新增application.yml檔案
注:這裡http://localhost:9999/eureka/是註冊中心地址

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9999/eureka/
server:
  port: 8762
spring:
  application:
    name: service-member

5)服務介面程式碼

@RestController
public class ServiceController {

    @RequestMapping("/getUserList")
    public List<String> getUserList() {
        List<String> listUser = new ArrayList<String>();
        listUser.add("zhangsan");
        listUser.add("lisi");
        return listUser;
    }

}

6)釋出服務程式碼,其中SpringBootApplication註解詳解

@SpringBootApplication(scanBasePackages = "com.springCloud.controller")
@EnableEurekaClient
public class ServiceStartApp {

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

其中我的目錄結構如下
這裡寫圖片描述
測試該服務結果如下
這裡寫圖片描述

三、搭建SpringCloud註冊中心(Eureka)之消費者

1) 建立簡單Maven專案,注意要選擇jar
2)pom檔案新增所需依賴

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-ribbon</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>Dalston.RC1</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>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

3)application.yml配置

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:9999/eureka/
server:
  port: 8763
spring:
  application:
    name: service-order

4)service程式碼

@Service
public class CustomerService {

    @Autowired
    RestTemplate restTemplate;

    public List<String> getOrderByUserList() {
        return restTemplate.getForObject("http://service-member/getUserList", List.class);
    }

}

5)Controller程式碼

@RestController
public class CustomerController {

    @Autowired
    CustomerService springCloudCustomer;

    @RequestMapping("/getUserList")
    public List<String> getUserList() {
        return springCloudCustomer.getOrderByUserList();
    }
}

6)啟動程式碼

@EnableEurekaClient
@SpringBootApplication(scanBasePackages = { "com.springCloud.controller", "com.springCloud.service" })
public class CustomerStartApp {

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

    @Bean
    @LoadBalanced //此註解是啟動負載均衡
    RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

其中程式碼目錄結構如下
這裡寫圖片描述
7)測試結果
這裡寫圖片描述
注意Eureka埠是9999,服務提供者是:8762,消費者是:8763