springboot2.x整合springcloud简单微服务的实现

一、开发环境及工具

  • jdk:1.8
  • spring boot:2.0.4
  • spring cloud:Finchley.SR1
  • idea:2018

二、步骤

1.创建eurekaserver

(1)设置eurekaserver的pom文件


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.qstgroupId>
    <artifactId>eureka-serverartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>eureka-servername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.7version>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>


(2)配置eurekaserver的application.yml文件

server:
  port: 8761
eureka:
  instance:
    hostname: eureka-server
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      serviceUrl: http://localhost:8761/eureka/


(3)在eurekaserver的主配置类上添加@EnableEurekaServer注解

package com.qst.eurekaserver;

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


@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

}


2.创建provider

(1)配置provider的pom文件


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.qstgroupId>
    <artifactId>providerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>providername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
      
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.8version>
        dependency>

    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>


(2)配置provider的appliction.yml文件

server:
  port: 8001
eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      serviceUrl: http://localhost:8761/eureka/
spring:
  application:
    name: provider

(3)创建provider的user实体类

public class User implements Serializable {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}


(4)创建provider的controller

@RestController
public class UserController { 
    @RequestMapping(value = "/provider", method = RequestMethod.POST)
    public User changeUser(@RequestBody User user) { 
        System.out.println(user.toString());
        user.setName("津津");
        return user;
    }
}

3.创建consumer

(1)配置consumer的pom文件


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.4.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>com.qstgroupId>
    <artifactId>consumerartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>consumername>
    <description>Demo project for Spring Bootdescription>

    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Finchley.SR1spring-cloud.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.9.8version>
        dependency>
    dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>


(2)配置consumer的application.yml文件

spring:
  application:
    name: consumer
server:
  port: 8200

eureka:
  instance:
    prefer-ip-address: true
  client:
    service-url:
      serviceUrl: http://localhost:8761/eureka/

(3)配置consumer的RestTemplate配置文件

@Configuration
public class RestTemplateConfig extends WebMvcConfigurationSupport {

    @Bean
    @LoadBalanced //负载均衡
    RestTemplate restTemplate(){
        RestTemplate restTemplate = new RestTemplate();
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        //添加上GSON的转换器
        //messageConverters.add(7, new GsonHttpMessageConverter());
        return restTemplate;
    }
//

(4)在consumer的主配置类上添加@EnableDiscoveryClient注解

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

}


(5)创建consumer的实体类user(一定要添加属性的set方法!!!—> 反射中根据set方法为私有属性赋值 )

public class User implements Serializable {
    private String name;

    public User(String name) {
        this.name = name;
    }

    public User() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                '}';
    }
}

(6)创建consumer的controller方法

@RestController
public class UserController {
    @Autowired
    RestTemplate restTemplate;
    @GetMapping("/getUser")
    public User getUser() {
        User user = new User("津少");
        User newUser= restTemplate.postForObject("http://PROVIDER/provider", user, User.class);
        return newUser;
    }
}


4.测试

启动eurekaserver -----> 启动provider -----> 启动consumer -----> 在浏览器上输入localhost:8200/getUser ------> 观察provider的控制台和网页显示的结果

你可能感兴趣的:(java,web)