栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Cloud入门操作手册(Greenwich)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Cloud入门操作手册(Greenwich)

一、创建SpringBoot项目







删除SpringBoot的src文件夹

二、commons 通用项目 1.新建 maven 项目(commons)



项目结构

pom.xml



    
        springcloud1
        cn.tedu
        0.0.1-SNAPSHOT
    
    4.0.0

    sp01-commons

    

        
            com.fasterxml.jackson.module
            jackson-module-parameter-names
            2.9.8
        

        
            com.fasterxml.jackson.datatype
            jackson-datatype-jdk8
            2.9.8
        

        
            com.fasterxml.jackson.datatype
            jackson-datatype-jsr310
            2.9.8
        

        
            com.fasterxml.jackson.datatype
            jackson-datatype-guava
            2.9.8
        

        
        
            org.projectlombok
            lombok
        

        
            javax.servlet
            javax.servlet-api
            3.1.0
        

        
            org.slf4j
            slf4j-api
            1.7.26
        

        
            org.apache.commons
            commons-lang3
            3.9
        
    

1.添加实体类 pojo

Item

package cn.tedu.sp01.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Item {
	private Integer id;
	private String name;
	private Integer number;
}

User

package cn.tedu.sp01.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {
	private Integer id;
	private String username;
	private String password;
}

Order

package cn.tedu.sp01.pojo;

import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Order {
	private String id;		   //20210812USE745678YUGHJ9485Y
	private User user;		   //用户
	private List items;  //订单的商品列表
}
2.业务接口 service

ItemService

package cn.tedu.sp01.server;

import cn.tedu.sp01.pojo.Item;
import java.util.List;

public interface ItemService {
    //获取订单中的商品列表
    List getItems(String orderId);
    //减少商品库存
    void decreaseNumbers(List list);
}

UserService

package cn.tedu.sp01.server;

import cn.tedu.sp01.pojo.User;

public interface UserService {
    //获取用户
    User getUser(Integer id);
    //增加积分
    void addScore(Integer id, Integer score);
}

OrderService

package cn.tedu.sp01.server;

import cn.tedu.sp01.pojo.Order;

public interface OrderService {
    //获取订单
    Order getOrder(String orderId);
    //添加订单
    void addOrder(Order order);
}
3.工具类 util

cookieUtil

package cn.tedu.web.util;

import javax.servlet.http.cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class cookieUtil {

    
    public static void setcookie(HttpServletResponse response,
                                 String name, String value, String domain, String path, int maxAge) {
        cookie cookie = new cookie(name, value);
        if(domain != null) {
            cookie.setDomain(domain);
        }
        cookie.setPath(path);
        cookie.setMaxAge(maxAge);
        response.addcookie(cookie);
    }
    public static void setcookie(HttpServletResponse response, String name, String value, int maxAge) {
        setcookie(response, name, value, null, "/", maxAge);
    }
    public static void setcookie(HttpServletResponse response, String name, String value) {
        setcookie(response, name, value, null, "/", 3600);
    }
    public static void setcookie(HttpServletResponse response, String name) {
        setcookie(response, name, "", null, "/", 3600);
    }

    
    public static String getcookie(HttpServletRequest request, String name) {
        String value = null;
        cookie[] cookies = request.getcookies();
        if (null != cookies) {
            for (cookie cookie : cookies) {
                if (cookie.getName().equals(name)) {
                    value = cookie.getValue();
                }
            }
        }
        return value;
    }

    
    public static void removecookie(HttpServletResponse response, String name, String domain, String path) {
        setcookie(response, name, "", domain, path, 0);
    }
}

JsonUtil

package cn.tedu.web.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Writer;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.datatype.guava.GuavaModule;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class JsonUtil {
    private static ObjectMapper mapper;
    private static JsonInclude.Include DEFAULT_PROPERTY_INCLUSION = JsonInclude.Include.NON_DEFAULT;
    private static boolean IS_ENABLE_INDENT_OUTPUT = false;
    private static String CSV_DEFAULT_COLUMN_SEPARATOR = ",";
    static {
        try {
            initMapper();
            configPropertyInclusion();
            configIndentOutput();
            configCommon();
        } catch (Exception e) {
            log.error("jackson config error", e);
        }
    }

    private static void initMapper() {
        mapper = new ObjectMapper();
    }

    private static void configCommon() {
        config(mapper);
    }

    private static void configPropertyInclusion() {
        mapper.setSerializationInclusion(DEFAULT_PROPERTY_INCLUSION);
    }

    private static void configIndentOutput() {
        mapper.configure(SerializationFeature.INDENT_OUTPUT, IS_ENABLE_INDENT_OUTPUT);
    }

    private static void config(ObjectMapper objectMapper) {
        objectMapper.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        objectMapper.enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT);
        objectMapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
        objectMapper.enable(DeserializationFeature.FAIL_ON_READING_DUP_TREE_KEY);
        objectMapper.enable(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        objectMapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        objectMapper.enable(JsonParser.Feature.ALLOW_COMMENTS);
        objectMapper.disable(JsonGenerator.Feature.ESCAPE_NON_ASCII);
        objectMapper.enable(JsonGenerator.Feature.IGNORE_UNKNOWN);
        objectMapper.enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES);
        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        objectMapper.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        objectMapper.registerModule(new ParameterNamesModule());
        objectMapper.registerModule(new Jdk8Module());
        objectMapper.registerModule(new JavaTimeModule());
        objectMapper.registerModule(new GuavaModule());
    }
    public static void setSerializationInclusion(JsonInclude.Include inclusion) {
        DEFAULT_PROPERTY_INCLUSION = inclusion;
        configPropertyInclusion();
    }

    public static void setIndentOutput(boolean isEnable) {
        IS_ENABLE_INDENT_OUTPUT = isEnable;
        configIndentOutput();
    }

    public static  V from(URL url, Class c) {
        try {
            return mapper.readValue(url, c);
        } catch (IOException e) {
            log.error("jackson from error, url: {}, type: {}", url.getPath(), c, e);
            return null;
        }
    }

    public static  V from(InputStream inputStream, Class c) {
        try {
            return mapper.readValue(inputStream, c);
        } catch (IOException e) {
            log.error("jackson from error, type: {}", c, e);
            return null;
        }
    }

    public static  V from(File file, Class c) {
        try {
            return mapper.readValue(file, c);
        } catch (IOException e) {
            log.error("jackson from error, file path: {}, type: {}", file.getPath(), c, e);
            return null;
        }
    }

    public static  V from(Object jsonObj, Class c) {
        try {
            return mapper.readValue(jsonObj.toString(), c);
        } catch (IOException e) {
            log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), c, e);
            return null;
        }
    }

    public static  V from(String json, Class c) {
        try {
            return mapper.readValue(json, c);
        } catch (IOException e) {
            log.error("jackson from error, json: {}, type: {}", json, c, e);
            return null;
        }
    }

    public static  V from(URL url, TypeReference type) {
        try {
            return mapper.readValue(url, type);
        } catch (IOException e) {
            log.error("jackson from error, url: {}, type: {}", url.getPath(), type, e);
            return null;
        }
    }

    public static  V from(InputStream inputStream, TypeReference type) {
        try {
            return mapper.readValue(inputStream, type);
        } catch (IOException e) {
            log.error("jackson from error, type: {}", type, e);
            return null;
        }
    }

    public static  V from(File file, TypeReference type) {
        try {
            return mapper.readValue(file, type);
        } catch (IOException e) {
            log.error("jackson from error, file path: {}, type: {}", file.getPath(), type, e);
            return null;
        }
    }

    public static  V from(Object jsonObj, TypeReference type) {
        try {
            return mapper.readValue(jsonObj.toString(), type);
        } catch (IOException e) {
            log.error("jackson from error, json: {}, type: {}", jsonObj.toString(), type, e);
            return null;
        }
    }

    public static  V from(String json, TypeReference type) {
        try {
            return mapper.readValue(json, type);
        } catch (IOException e) {
            log.error("jackson from error, json: {}, type: {}", json, type, e);
            return null;
        }
    }

    public static  String to(List list) {
        try {
            return mapper.writevalueAsString(list);
        } catch (JsonProcessingException e) {
            log.error("jackson to error, obj: {}", list, e);
            return null;
        }
    }

    public static  String to(V v) {
        try {
            return mapper.writevalueAsString(v);
        } catch (JsonProcessingException e) {
            log.error("jackson to error, obj: {}", v, e);
            return null;
        }
    }

    public static  void toFile(String path, List list) {
        try (Writer writer = new FileWriter(new File(path), true)) {
            mapper.writer().writevalues(writer).writeAll(list);
            writer.flush();
        } catch (Exception e) {
            log.error("jackson to file error, path: {}, list: {}", path, list, e);
        }
    }

    public static  void toFile(String path, V v) {
        try (Writer writer = new FileWriter(new File(path), true)) {
            mapper.writer().writevalues(writer).write(v);
            writer.flush();
        } catch (Exception e) {
            log.error("jackson to file error, path: {}, obj: {}", path, v, e);
        }
    }

    public static String getString(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).toString();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get string error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static Integer getInt(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).intValue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get int error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static Long getLong(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).longValue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get long error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static Double getDouble(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).doublevalue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get double error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static BigInteger getBigInteger(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return new BigInteger(String.valueOf(0.00));
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).bigIntegerValue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get biginteger error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static BigDecimal getBigDecimal(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).decimalValue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get bigdecimal error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static boolean getBoolean(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return false;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).booleanValue();
            } else {
                return false;
            }
        } catch (IOException e) {
            log.error("jackson get boolean error, json: {}, key: {}", json, key, e);
            return false;
        }
    }

    public static byte[] getByte(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        try {
            JsonNode node = mapper.readTree(json);
            if (null != node) {
                return node.get(key).binaryValue();
            } else {
                return null;
            }
        } catch (IOException e) {
            log.error("jackson get byte error, json: {}, key: {}", json, key, e);
            return null;
        }
    }

    public static  ArrayList getList(String json, String key) {
        if (StringUtils.isEmpty(json)) {
            return null;
        }
        String string = getString(json, key);
        return from(string, new TypeReference>() {});
    }

    public static  String add(String json, String key, T value) {
        try {
            JsonNode node = mapper.readTree(json);
            add(node, key, value);
            return node.toString();
        } catch (IOException e) {
            log.error("jackson add error, json: {}, key: {}, value: {}", json, key, value, e);
            return json;
        }
    }

    private static  void add(JsonNode jsonNode, String key, T value) {
        if (value instanceof String) {
            ((ObjectNode) jsonNode).put(key, (String) value);
        } else if (value instanceof Short) {
            ((ObjectNode) jsonNode).put(key, (Short) value);
        } else if (value instanceof Integer) {
            ((ObjectNode) jsonNode).put(key, (Integer) value);
        } else if (value instanceof Long) {
            ((ObjectNode) jsonNode).put(key, (Long) value);
        } else if (value instanceof Float) {
            ((ObjectNode) jsonNode).put(key, (Float) value);
        } else if (value instanceof Double) {
            ((ObjectNode) jsonNode).put(key, (Double) value);
        } else if (value instanceof BigDecimal) {
            ((ObjectNode) jsonNode).put(key, (BigDecimal) value);
        } else if (value instanceof BigInteger) {
            ((ObjectNode) jsonNode).put(key, (BigInteger) value);
        } else if (value instanceof Boolean) {
            ((ObjectNode) jsonNode).put(key, (Boolean) value);
        } else if (value instanceof byte[]) {
            ((ObjectNode) jsonNode).put(key, (byte[]) value);
        } else {
            ((ObjectNode) jsonNode).put(key, to(value));
        }
    }

    public static String remove(String json, String key) {
        try {
            JsonNode node = mapper.readTree(json);
            ((ObjectNode) node).remove(key);
            return node.toString();
        } catch (IOException e) {
            log.error("jackson remove error, json: {}, key: {}", json, key, e);
            return json;
        }
    }

    public static  String update(String json, String key, T value) {
        try {
            JsonNode node = mapper.readTree(json);
            ((ObjectNode) node).remove(key);
            add(node, key, value);
            return node.toString();
        } catch (IOException e) {
            log.error("jackson update error, json: {}, key: {}, value: {}", json, key, value, e);
            return json;
        }
    }

    public static String format(String json) {
        try {
            JsonNode node = mapper.readTree(json);
            return mapper.writerWithDefaultPrettyPrinter().writevalueAsString(node);
        } catch (IOException e) {
            log.error("jackson format json error, json: {}", json, e);
            return json;
        }
    }

    public static boolean isJson(String json) {
        try {
            mapper.readTree(json);
            return true;
        } catch (Exception e) {
            log.error("jackson check json error, json: {}", json, e);
            return false;
        }
    }

    private static InputStream getResourceStream(String name) {
        return JsonUtil.class.getClassLoader().getResourceAsStream(name);
    }

    private static InputStreamReader getResourceReader(InputStream inputStream) {
        if (null == inputStream) {
            return null;
        }
        return new InputStreamReader(inputStream, StandardCharsets.UTF_8);
    }
}

JsonResult

package cn.tedu.web.util;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class JsonResult {
    
    public static final int SUCCESS = 200;

    
    public static final int NOT_LOGIN = 400;

    
    public static final int EXCEPTION = 401;

    
    public static final int SYS_ERROR = 402;

    
    public static final int PARAMS_ERROR = 403;

    
    public static final int NOT_SUPPORTED = 410;

    
    public static final int INVALID_AUTHCODE = 444;

    
    public static final int TOO_FREQUENT = 445;

    
    public static final int UNKNOWN_ERROR = 499;

    private int code;
    private String msg;
    private T data;

    public static JsonResult build() {
        return new JsonResult();
    }
    public static JsonResult build(int code) {
        return new JsonResult().code(code);
    }
    public static JsonResult build(int code, String msg) {
        return new JsonResult().code(code).msg(msg);
    }
    public static  JsonResult build(int code, T data) {
        return new JsonResult().code(code).data(data);
    }
    public static  JsonResult build(int code, String msg, T data) {
        return new JsonResult().code(code).msg(msg).data(data);
    }

    public JsonResult code(int code) {
        this.code = code;
        return this;
    }
    public JsonResult msg(String msg) {
        this.msg = msg;
        return this;
    }
    public JsonResult data(T data) {
        this.data = data;
        return this;
    }


    public static JsonResult ok() {
        return build(SUCCESS);
    }
    public static JsonResult ok(String msg) {
        return build(SUCCESS, msg);
    }
    public static  JsonResult ok(T data) {
        return build(SUCCESS, data);
    }
    public static JsonResult err() {
        return build(EXCEPTION);
    }
    public static JsonResult err(String msg) {
        return build(EXCEPTION, msg);
    }

    @Override
    public String toString() {
        return JsonUtil.to(this);
    }
}
三、item service 商品服务 1.新建 spring boot 起步项目

选择依赖项

  • 只选择 web


配置依赖 pom.xml

  • 注意要填加 sp01-commons 项目依赖
  • 注意 parent 标签中的内容需要改成父工程的


	
	
		springcloud1
		cn.tedu
		0.0.1-SNAPSHOT
	
	
	4.0.0

	sp02-itemservice
	0.0.1-SNAPSHOT
	sp02-itemservice
	Demo project for Spring Boot
	
	
		1.8
	
	
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
		
		
		
			cn.tedu
			sp01-commons
			0.0.1-SNAPSHOT
		
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


配置 application.yml

# 向注册中心注册的名字
spring:
  application:
    name: item-service

# item   8001
# user   8101
# order  8201
server:
  port: 8001

配置主程序

  • 默认代码,不需要修改
package cn.tedu.sp02;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sp02ItemserviceApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp02ItemserviceApplication.class, args);
	}
}
2.项目结构

3.编写代码 1.ItemServiceImpl
package cn.tedu.sp02.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.server.ItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ItemServiceImpl implements ItemService {

    @Override
    public List getItems(String orderId) {
        log.info("获取商品列表,orderId="+orderId);
        
        // Demo 数据
        ArrayList items = new ArrayList<>();
        items.add(new Item(1,"商品1",1));
        items.add(new Item(2,"商品2",4));
        items.add(new Item(3,"商品3",1));
        items.add(new Item(4,"商品4",2));
        items.add(new Item(5,"商品5",6));
        return items;
    }

    @Override
    public void decreaseNumbers(List items) {
        for (Item item : items) {
            log.info("减少库存:"+item);
        }
    }
}
2.ItemController
  • 如果存在 jackson-dataformat-xml 依赖,会根据请求协议头,可能返回 xml 或 json;可以强制返回 json:
    produces=MediaType.APPLICATION_JSON_UTF8_VALUE
package cn.tedu.sp02.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.server.ItemService;
import cn.tedu.web.util.JsonResult;
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.*;

import java.util.List;

@Slf4j
@RestController
public class ItemController {

    @Autowired
    private ItemService itemService;

    @Value("${server.port}")
    private int port;

    @GetMapping("/{orderId}")
    public JsonResult> getItems(@PathVariable String orderId) {
        log.info("server.port="+port+", orderId="+orderId);

        List items = itemService.getItems(orderId);
        return JsonResult.ok().msg("查询成功").data(items);
    }

    //@RequestBody 完整接收请求协议体数据
    @PostMapping("/decreaseNumber")
    public JsonResult decreaseNumber(@RequestBody List items) {
        itemService.decreaseNumbers(items);
        return JsonResult.ok().msg("减少库存成功");
    }

    @GetMapping("/favicon.ico")
    public void ico(){

    }
}
4.访问测试

根据orderid,查询商品
http://localhost:8001/35

减少商品库存
http://localhost:8001/decreaseNumber

使用postman,POST发送以下格式数据:
[{"id":1, "name":"abc", "number":23},{"id":2, "name":"def", "number":11}]

四、user service 用户服务 1.新建 spring boot 起步项目

选择依赖项

  • 只选择 web


配置依赖 pom.xml

  • 注意要填加 sp01-commons 项目依赖
  • 注意 parent 标签中的内容需要改成父工程的


	
	4.0.0
	
	
		springcloud1
		cn.tedu
		0.0.1-SNAPSHOT
	

	cn.tedu
	sp03-userservice
	0.0.1-SNAPSHOT
	sp03-userservice
	Demo project for Spring Boot

	
		1.8
	
	
	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
		
			cn.tedu
			sp01-commons
			0.0.1-SNAPSHOT
		
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


配置 application.yml

spring:
  application:
    name: user-service

# item    8001
# user    8101
# order   8201
server:
  port: 8101


sp:
  user-service:
    users: "[{"id":7, "username":"abc","password":"123"},
    		 {"id":8, "username":"def","password":"456"},
    		 {"id":9, "username":"ghi","password":"789"}]"

配置主程序

  • 默认代码,不需要修改
package cn.tedu.sp03;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sp03UserserviceApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp03UserserviceApplication.class, args);
	}
}
2.项目结构

3.编写代码 1.UserServiceImpl
package cn.tedu.sp03.service;

import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.UserService;
import cn.tedu.web.util.JsonUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    //注入 yml中配置的 demo 数据
    @Value("${sp.user-service.users}")
    private String userJson;

    @Override
    public User getUser(Integer id) {
        log.info("获取用户,id = "+id);
        //TypeReference 利用匿名内部类继承语法,写泛型类型:List
        //userJson  -->  List
        List list = JsonUtil.from(userJson, new TypeReference>() {});
        for (User u : list) {
            if (u.getId().equals(id)) {
                return u;
            }
        }
        //不是7,8,9用户,返回一个写死的用户数据
        return new User(id, "用户名:"+id, "密码:"+id);
    }

    @Override
    public void addScore(Integer id, Integer score) {
        //TODO 这里增加积分
        log.info("增加用户积分,id = "+id+",score = "+score);
    }
}
2.UserController
  • 如果存在 jackson-dataformat-xml 依赖,会根据请求协议头,可能返回 xml 或 json;可以强制返回 json:
    produces=MediaType.APPLICATION_JSON_UTF8_VALUE
package cn.tedu.sp03.controller;

import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.UserService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{userId}")
    public JsonResult getUser(@PathVariable Integer userId) {
        log.info("get user, userId="+userId);

        User user = userService.getUser(userId);
        return JsonResult.ok().msg("查询成功").data(user);
    }

    //http://localhost:8101/8/score?score=1000
    @GetMapping("/{userId}/score")
    public JsonResult addScore(
            @PathVariable Integer userId,
            Integer score) {
        userService.addScore(userId, score);
        return JsonResult.ok().msg("增加积分成功");
    }

    @GetMapping("/favicon.ico")
    public void ico(){

    }
}
4.访问测试

根据userid查询用户信息
http://localhost:8101/7

根据userid,为用户增加积分
http://localhost:8101/7/score?score=100

五、order service 订单服务 1.新建 spring boot 起步项目

选择依赖项

  • 只选择 web

配置依赖 pom.xml

  • 注意要填加 sp01-commons 项目依赖
  • 注意 parent 标签中的内容需要改成父工程的


	4.0.0

	
		springcloud1
		cn.tedu
		0.0.1-SNAPSHOT
	

	cn.tedu
	sp04-orderservice
	0.0.1-SNAPSHOT
	sp04-orderservice
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		

		
			cn.tedu
			sp01-commons
			0.0.1-SNAPSHOT
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


配置 application.yml

spring:
  application:
    name: order-service

# 8001  8101  8201
server:
  port: 8201

配置主程序

  • 默认代码,不需要修改
package cn.tedu.sp04;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Sp04OrderserviceApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp04OrderserviceApplication.class, args);
	}
}
2.项目结构

3.编写代码 1.OrderServiceImpl
package cn.tedu.sp04.service;

import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.server.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单,orderId = "+orderId);

        //TODO: 远程调用user-service,获取用户信息
        //TODO: 调用item-service,获取商品信息

        Order order = new Order();
        order.setId(orderId);
//        order.setItems(用户);
//        order.setItems(商品列表);
        return order;
    }

    @Override
    public void addOrder(Order order) {

        log.info("保存订单:"+order);

        //TODO: 调用item-service,减少商品库存
        //TODO: 调用user-service,增加用户积分
    }
}
2.OrderController
package cn.tedu.sp04.controller;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.pojo.User;
import cn.tedu.sp01.server.OrderService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    public JsonResult getOrder(@PathVariable String orderId) {
        log.info("get order, id="+orderId);

        Order order = orderService.getOrder(orderId);
        return JsonResult.ok().data(order);
    }

    @GetMapping("/add")
    public JsonResult addOrder() {
        //模拟post提交的数据
        Order order = new Order();
        order.setId("123abc");
        order.setUser(new User(8,null,null));
        order.setItems(
                Arrays.asList(
                        new Item[] {
                            new Item(1,"aaa",2),
                            new Item(2,"bbb",1),
                            new Item(3,"ccc",3),
                            new Item(4,"ddd",1),
                            new Item(5,"eee",5),
                        }
                )
        );
        orderService.addOrder(order);
        return JsonResult.ok().msg("保存订单成功");
    }
}
4.访问测试

根据orderid,获取订单
http://localhost:8201/123abc

保存订单,观察控制台日志输出
http://localhost:8201/add

六、eureka 注册与发现

  1. 创建eureka项目
  2. 配置依赖 pom.xml
  3. 配置 application.yml
  4. 主程序启用 eureka 服务器
  5. 启动,访问测试
1.创建 eureka server 项目:sp05-eureka

新建 spring boot 起步项目

选择依赖项

  • 只选择 eureka

配置依赖 pom.xml

  • 注意 parent 标签中的内容需要改成父工程的
  • 注意删掉springcloud版本号和dependencyManagement中的内容


	
	4.0.0

	
		springcloud1
		cn.tedu
		0.0.1-SNAPSHOT
	

	cn.tedu
	sp05-eureka
	0.0.1-SNAPSHOT
	sp05-eureka
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	


删除如下:

application.yml

spring:
  application:
    name: eureka-server

server:
  port: 2001

# 关闭自我保护模式
# 主机名
# 针对单台服务器,不向自己注册,不从自己拉取
eureka:
  server:
    enable-self-preservation: false
  instance:
    hostname: eureka1
  client:
    register-with-eureka: false
    fetch-registry: false

配置主程序

2.修改 hosts 文件,添加 eureka 域名映射

C:WindowsSystem32driversetchosts

添加内容:

127.0.0.1       eureka1
127.0.0.1       eureka2
3.启动,并访问测试

http://eureka1:2001

七、service provider 服务提供者

  • 修改 item-service、user-service、order-service,把微服务注册到 eureka 服务器

    1.pom.xml 添加eureka依赖
    2.application.yml 添加eureka注册配置
    3.主程序启用eureka客户端
    4.启动服务,在eureka中查看注册信息

1.pom.xml 添加 eureka 客户端依赖

	org.springframework.cloud
	spring-cloud-starter-netflix-eureka-client

2.application.yml 添加 eureka注册配置
eureka:
  client:
    service-url:
      defaultZone: http://eureka1:2001/eureka
  • eureka.instance.lease-renewal-interval-in-seconds

    心跳间隔时间,默认 30 秒

  • defaultZone,默认位置,可以修改为具体地理位置,比如:beiJing, shangHai, shenZhen 等,表示eureka 服务器的部署位置

  • eureka.client.registry-fetch-interval-seconds

    拉取注册信息间隔时间,默认 30 秒

3.主程序启用服务注册发现客户端

修改 item-service、user-service 和 order-service,
主程序添加 @EnableDiscoveryClient 注解

4.启动,并访问 eureka 查看注册信息

  • http://eureka1:2001

八、eureka 和 “服务提供者”的高可用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/333036.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号