springcloud學習筆記(整合eureka+feign獨立API)

記錄一下基礎的入門配置,方便學習理解。

推薦原學習文章地址:https://blog.csdn.net/forezp/article/list/6?t=1&

備註:版本號不一樣,很多maven依賴的id也不一樣,剛開始做測試用的是springboot2.1.3,springcloud是Green,出了很多問題,後來就降低成1.5,edg版本了。

Point:積分系統服務提供者、常規的springboot三層。service層和model層分離出來丟在了point-share,pom直接添加share的依賴

point-share:積分系統公共服務(主要是模型和service層)、分離出單獨maven項目供其他服務調用

order:訂單系統(添加point-share依賴)消費者

1:eureka服務:

pom:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>eureka</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>eureka</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
    </properties>

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

啓動類:

@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {

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

}

配置文件:

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
spring:
  application:
    name: eureka-server

 

2:Point服務提供者

pom:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>point</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>point</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
        <mybatis-spring-boot>1.3.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
        <access.ver>1.0.1</access.ver>
        <fastjson.version>1.2.47</fastjson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

        <!-- MySQL 連接驅動依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>
        <!-- SpringBoot Mybatis 依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <!-- lombok依賴 可以減少大量的模塊代碼-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--Slf4j 依賴-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!-- logback 依賴 是slf4j的實現-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- access 接口直接使用resultObj對象-->
        <dependency>
            <groupId>com.bjj</groupId>
            <artifactId>access</artifactId>
            <version>${access.ver}</version>
        </dependency>
        <!-- Druid數據庫連接池組件 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>point-share</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </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>

配置文件

server:
  port: 8882
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: point-server
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
    username: root
    password:
    driverClassName: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000

#mybatis:
#  typeAliasesPackage: com.example.point.model
#  mapperLocations: classpath:mapper/*.xml
#開啓輸出sql日誌
logging:
  level:
    com:
      example:
        point:
          dao: DEBUG

啓動類:


@MapperScan("com.example.point.dao")
@EnableEurekaClient
@SpringBootApplication
public class PointApplication {

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

}

添加三層:

controller方法

package com.example.point.web;

import com.example.pointshare.feign.PointService;
import com.example.pointshare.model.Point;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * Created by [email protected] on 2019/5/14
 */
@RestController
@RequestMapping("/point")
@Slf4j
public class PointController {

    @Value("${server.port}")
    String point;

    @Autowired
    private PointService pointService;

    @RequestMapping(value="/sayHi",method= RequestMethod.GET)
    public void sayHi(@RequestParam(value="name") String name){
        log.info("point sayHi in name: {},point: {}",name,point);
        pointService.sayHi(name);
    }

    @RequestMapping(value="/findById/{id}",method= RequestMethod.GET)
    public Point findById(@PathVariable(value="id") String id){
        log.info("point findById in:{} ",id);
        Point point=pointService.findById(id);
        return point;
    }
    @RequestMapping(value="/findByIdAndName",method= RequestMethod.POST)
    public Point findByIdAndName(@RequestParam(value="id") String id,@RequestParam(value="name") String name){
        log.info("point findByIdAndName in:{},{} ",id,name);
        Point point=pointService.findByIdAndName(id,name);
        return point;
    }


    @RequestMapping(value="/update",method= RequestMethod.POST)
    public void update(@RequestBody Point point){
        log.info("point update in:{}",point);
        pointService.update(point);
    }
}

 service實現類(service分離出去了,後續補充):

package com.example.point.service;

import com.example.point.dao.PointDao;
import com.example.pointshare.feign.PointService;
import com.example.pointshare.model.Point;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by [email protected] on 2019/5/14
 */
@Slf4j
@Service
public class PointServiceImpl implements PointService {

    @Autowired
    PointDao pointDao;

    @Override
    public void sayHi(String name) {
        log.info("point feignImpl sayHi in:{} ",name);
    }

    @Override
    public Point findById(String id) {
        return pointDao.findById(id);
    }

    @Override
    public Point findByIdAndName(String id, String name) {
        return pointDao.findByIdAndName(id,name);
    }

    @Override
    public void update(Point point) {
        point.setUpdateTime(new Date());
        pointDao.update(point);
    }
}




dao層(直接方便測試就用了註解sql,沒有mapper文件):

package com.example.point.dao;

import com.example.pointshare.model.Point;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

/**
 * Created by [email protected] on 2019/5/15
 */
//@Mapper 啓動目錄已經添加了@MapperScan("com.example.point.dao"),這裏可以省略
public interface PointDao {


    @Select("select * from t_point where id=#{id}")
    //公用同一個resultMap可以添加mapper文件寫映射關係
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "point", column = "point"),
            @Result(property = "updateTime", column = "update_time")
    })
    Point findById(String id);


    @Select("select * from t_point where id=#{id} and user_id=#{name}")
    @Results(value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "point", column = "point"),
            @Result(property = "updateTime", column = "update_time")
    })
    Point findByIdAndName(@Param("id") String id, @Param("name") String name);

    @UpdateProvider(type = PointSql.class, method = "update")
    void update(Point point);

    class PointSql {
        public String update(Point point) {
            return new SQL() {{
                UPDATE("t_point");
                //條件寫法.
                if (point.getPoints() != 0) {
                    SET("points=points+#{points}");
                }
                if (point.getUpdateTime() != null) {
                    SET("update_time=#{updateTime}");
                }
                WHERE("id=#{id}");
            }}.toString();
        }
    }
}

 3:point-share(feign公共API)

pom:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.21.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>point-share</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>point-share</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
        <mybatis-spring-boot>1.3.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
        <access.ver>1.0.1</access.ver>
        <fastjson.version>1.2.47</fastjson.version>
    </properties>

    <dependencies>

        <!--<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-feign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- lombok依賴 可以減少大量的模塊代碼-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--Slf4j 依賴-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!-- logback 依賴 是slf4j的實現-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </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>-->

            <!-- 編譯插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

service:

package com.example.pointshare.feign;

import com.example.pointshare.model.Point;
import feign.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

/**
 * Created by [email protected] on 2019/5/13
 */
@FeignClient(value = "point-server",fallback = PointService.PointFeignHystric.class,configuration = PointService.FeignConfiguration.class)
public interface PointService {

    @RequestMapping(value="/point/sayHi",method= RequestMethod.GET)
    void sayHi(@RequestParam(value = "name") String name);

    @RequestMapping(value="/point/findById/{id}",method= RequestMethod.GET)
    Point findById(@PathVariable("id") String id);

    @RequestMapping(value="/point/findByIdAndName",method= RequestMethod.POST)//, consumes = MediaType.APPLICATION_JSON_VALUE
    Point findByIdAndName(@RequestParam("id") String id, @RequestParam("name") String name);

   @RequestMapping(value="/point/update",method= RequestMethod.POST)
   //@RequestLine("POST /point/update")
    void update(@RequestBody Point point);

    @Component
    @Slf4j
    public class PointFeignHystric implements PointService {
        @Override
        public void sayHi(String name) {
            log.info("sayHi feign error");
        }

        @Override
        public Point findById(String id) {
            return null;
        }

        @Override
        public Point findByIdAndName(String id, String name) {
            return null;
        }

        @Override
        public void update(Point point) {

        }
    }

    /**
     * Created by [email protected] on 2019/5/15
     */
    @Configuration
    public class FeignConfiguration {
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    }

}

 model:

package com.example.pointshare.model;

import lombok.Data;
import lombok.ToString;

import java.util.Date;

/**
 * Created by [email protected] on 2019/5/13
 */
@Data
@ToString
public class Point {

    private String id;

    private String userId;

    private int points;

    private Date updateTime;
}




4:order消費者

pom:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>order</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>order</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
        <mybatis-spring-boot>1.3.0</mybatis-spring-boot>
        <mysql-connector>5.1.39</mysql-connector>
        <access.ver>1.0.1</access.ver>
        <fastjson.version>1.2.47</fastjson.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</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-feign</artifactId>
        </dependency>

        <!-- MySQL 連接驅動依賴 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-connector}</version>
        </dependency>
        <!-- SpringBoot Mybatis 依賴 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis-spring-boot}</version>
        </dependency>
        <!-- lombok依賴 可以減少大量的模塊代碼-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!--Slf4j 依賴-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!-- logback 依賴 是slf4j的實現-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!-- access 接口直接使用resultObj對象-->
        <dependency>
            <groupId>com.bjj</groupId>
            <artifactId>access</artifactId>
            <version>${access.ver}</version>
        </dependency>
        <!-- Druid數據庫連接池組件 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.18</version>
        </dependency>
        <!-- fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>${fastjson.version}</version>
        </dependency>


        <dependency>
            <groupId>com.example</groupId>
            <artifactId>point-share</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </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>

配置文件:

server:
  port: 8881
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
spring:
  application:
    name: order-server
  datasource:
    url: jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8
    username: root
    password:
    driverClassName: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxPoolPreparedStatementPerConnectionSize: 20
    filters: stat,wall
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000


#日誌
logging:
  level:
    com:
      example:
        pointshare:
          #開啓輸出feign
          feign:
            PointService: DEBUG

#開啓斷路由
feign:
  hystrix:
    enabled: true

啓動類:

package com.example.order;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;

@EnableEurekaClient
@EnableFeignClients(basePackages = "com.example.pointshare.feign")
@SpringBootApplication(scanBasePackages = {"com.example"})
public class OrderApplication {

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

}

controller:

package com.example.order.web;

import com.example.order.service.OrderService;
import com.example.pointshare.model.Point;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by [email protected] on 2019/5/14
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value="/sayHi",method= RequestMethod.GET)
    public void sayHi(@RequestParam(value="name") String name){
        log.info("order sayHi in:{} ",name);
        orderService.sayHi(name);
    }
    @RequestMapping(value="/findById/{id}",method= RequestMethod.GET)
    public Point findById(@PathVariable(value="id") String id){
        log.info("order findById in:{} ",id);
        return orderService.findById(id);
    }

    @RequestMapping(value="/findByIdAndName",method= RequestMethod.POST)
    public Point findByIdAndName(@RequestParam(value="id") String id,@RequestParam(value="name") String name){
        log.info("order findByIdAndName in:{},{} ",id,name);
        return orderService.findByIdAndName(id,name);
    }

    @RequestMapping(value="/update",method= RequestMethod.POST)
    public void update(@RequestBody Point point){
        log.info("order update in:{}",point);
        orderService.update(point);
    }
}

service:

package com.example.order.service;

import com.example.pointshare.model.Point;

/**
 * Created by [email protected] on 2019/5/15
 */
public interface OrderService {
    void sayHi(String name);

    Point findById(String id);

    Point findByIdAndName(String id, String name);

    void update(Point point);
}

service實現類:

package com.example.order.service.impl;

import com.example.order.service.OrderService;
import com.example.pointshare.feign.PointService;
import com.example.pointshare.model.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by [email protected] on 2019/5/15
 */
@Service
public class OrderServiceImpl implements OrderService {


    @Autowired
    private PointService pointService;

    @Override
    public void sayHi(String name) {
        pointService.sayHi(name);
    }

    @Override
    public Point findById(String id) {
        return pointService.findById(id);
    }

    @Override
    public Point findByIdAndName(String id, String name) {
        return pointService.findByIdAndName(id,name);
    }

    @Override
    public void update(Point point) {
        pointService.update(point);
    }

}






代碼:

https://github.com/huiyunfei/spring-cloud.git(後續其他中間件測試可能會修改代碼,以本文檔貼出來的代碼爲準)

其中point可以啓動多端口多實例,feign直接就已經實現了負載均衡。斷路由hystric在feignclient裏邊配置了。包括feign的日誌記錄。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章