1. 程式人生 > >SpringBoot整合Eureka搭建微服務

SpringBoot整合Eureka搭建微服務

new ica context actor reg pre cloud packaging lse

1.創建一個services項目,添加三個子模塊client(客戶端)、service(服務端)、registry(註冊中心)

1.1 創建一個services項目

技術分享圖片

技術分享圖片

1.2 添加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> <packaging>pom</packaging> <modules> <module>client</module> <module>service</module> <module>registry</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1
.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.micro</groupId> <artifactId>services</artifactId> <version>0.0.1-SNAPSHOT</version> <name>services</name> <description>services</description> <properties> <java.version>1.8
</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> <version>1.4.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>

1.3 添加子模塊client(客戶端)

技術分享圖片

技術分享圖片

1.4 添加子模塊service(服務端)

技術分享圖片

1.5 添加子模塊registry(註冊中心)

技術分享圖片

技術分享圖片

2.搭建registry(註冊中心)

2.1 application.yml 配置

server:
  port: 8080

eureka:
  instance:
    hostname: localhost
  client: 
    registerWithEureka: false
    fetchRegistry: false

2.2 編寫啟動程序

package com.services.registry;

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

@SpringBootApplication
@EnableEurekaServer
public class RegistryApplication {

    public static void main(String[] args) {

        SpringApplication.run(RegistryApplication.class);
    }
}

3.搭建service(服務端)

3.1 application.yml 配置

server:
  port: 8081
spring:
  application:
    name: service
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/

3.2 編寫啟動程序

package com.services.service;

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

@SpringBootApplication
@EnableDiscoveryClient
public class ServiceApplication {

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

}

3.3 編寫服務

package com.services.service;

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

@RestController
public class HelloWorldController {
    @RequestMapping("hello/{name}")
    public String hello(@PathVariable String name) {
        return name + " say hello";
    }

}

4.搭建client(客戶端)

4.1 application.yml 配置

server:
  port: 8082
spring:
  application:
    name: client
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
app:
  service-url: http://localhost:8081/

4.2 編寫啟動程序

package com.services.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableDiscoveryClient
public class ClientApplication {

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

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

4.3 編寫service

package com.services.client;

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

@Service
public class CallHelloService {

    @Value("${app.service-url}")
    private String appServiceUrl;

    @Autowired
    private RestTemplate restTemplate;

    public String callHello(String name) {
        // 是一個http client
        ResponseEntity result = restTemplate.postForEntity(appServiceUrl + "hello/" + name, null, String.class);
        return result.getBody().toString();
    }
}

4.4 編寫controller

package com.services.client;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CallHelloController {

    @Autowired
    private CallHelloService callHelloService;

    @GetMapping("hello")
    public String hello(String name) {
        String result = callHelloService.callHello(name);
        return result;
    }

}

5.啟動registry-service-client模塊

5.1 registry

http://localhost:8080/

技術分享圖片

技術分享圖片

5.2 service

技術分享圖片

刷新網頁 http://localhost:8080/

技術分享圖片

5.3 client

技術分享圖片

刷新網頁 http://localhost:8080/

技術分享圖片

client 訪問 http://localhost:8082/hello?name=tao

技術分享圖片

源碼地址: https://github.com/80905949/SpringCloud.git

SpringBoot整合Eureka搭建微服務