springCloud入门文档(1)

}

public BaseResult(Integer code, String msg, Object data, Long count) {

this.code = code;

this.msg = msg;

this.data = data;

this.count = count;

}

public void markSuccess(String msg, Object data, Long count) {

this.code = 200;

this.msg = msg;

this.data = data;

this.count = count;

}

public void markSysError(String msg) {

this.code = 500;

this.msg = msg;

}

public void markWarning(String msg) {

this.code = 0;

this.msg = msg;

}

public boolean checkSuccess() {

return ObjectUtils.isEmpty(this.getCode()) && 200 == this.getCode();

}

public boolean checkSuccessWData() {

return this.checkSuccess() && ObjectUtils.isEmpty(this.getData());

}

public Integer getCode() {

return this.code;

}

public void setCode(Integer code) {

this.code = code;

}

public String getMsg() {

return this.msg;

}

public void setMsg(String msg) {

this.msg = msg;

}

public Object getData() {

return this.data;

}

public void setData(Object data) {

this.data = data;

}

public Long getCount() {

return this.count;

}

public void setCount(Long count) {

this.count = count;

}

public String toString() {

return “BaseResult [code=” + this.code + “, msg=” + this.msg + “, data=” + this.data + “, count=” + this.count + “]”;

}

}

2.service层


UserService

package com.cloud.api.provider.service;

import com.cloud.api.provider.po.User;

public interface UserService {

public User getUser(Integer userId,String userName);

}

注意:在打包之前去掉spring-boot-maven-plugin插件,点击此处查看原因

2.Eureka注册中心

=========================================================================

新建一个SpringBoot web项目用于Eureka注册中心

1.maven依赖


org.springframework.cloud

spring-cloud-starter-netflix-eureka-server

2.2.1.RELEASE

com.cloud

api

0.0.1-SNAPSHOT

2.配置properties


server.port=8080

#eureka.instance.hostname Eureka服务端实例名称

eureka.instance.hostname=eureka-server

#eureka.client.register-with-eureka 是否向注册中心注册自己

eureka.client.register-with-eureka=false

#eureka.client.fetch-registry 是否从Eureka上获取服务的注册信息,自己就是注册中心,本身职责就是维护服务实例,并不需要去检索服务

eureka.client.fetch-registry=false

#eureka.client.service-url.defaultZone 设置与Eureka Server交互的地址(查询服务、注册服务等)

eureka.client.service-url.defaultZone=http://localhost:${server.port}/eureka/

3.在主程序类上添加@EnableEurekaServer注解启用注册中心功能


package com.cloud.eureka;

import com.cloud.api.po.BaseBean;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication

@EnableEurekaServer //启用Eureka注册中心功能

public class EurekaApplication {

public static void main(String[] args) {

SpringApplication.run(EurekaApplication.class, args);

}

}

4.启动项目后访问http://localhost:8080/查看服务启动情况


在这里插入图片描述

3.provider

=======================================================================

新建SpringBoot web项目用于服务提供者

1.添加maven依赖


org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2.2.5.RELEASE

com.cloud

api

0.0.1-SNAPSHOT

2.配置properties


server.port=8081

spring.application.name=user-provider

#eureka.instance.prefer-ip-address 注册服务的时候使用服务的ip地址

eureka.instance.prefer-ip-address=true

eureka.client.service-url.defaultZone=http://localhost:8080/eureka/

3.service的实现类


UserServiceImpl

package com.cloud.provider.serviceImpl;

import com.cloud.api.provider.po.User;

import com.cloud.api.provider.service.UserService;

import org.springframework.stereotype.Service;

@Service

public class UserServiceImpl implements UserService {

@Override

public User getUser(Integer userId,String userName) {

return new User(userId,userName);

}

}

4.controller


UserController

package com.cloud.provider.controller;

import com.cloud.api.po.BaseResult;

import com.cloud.api.provider.po.User;

import com.cloud.api.provider.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

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

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

public class UserController {

@Autowired

private UserService userService;

@ResponseBody

@RequestMapping(value = “getUser”,method = RequestMethod.POST)

public BaseResult getUser(@RequestBody User user){

BaseResult baseResult=new BaseResult();

User u=userService.getUser(user.getId(),user.getUserName());

baseResult.markSuccess(“获取成功”,u,null);

return baseResult;

}

}

5.在主程序类上添加@EnableEurekaClient注解启动Eureka客户端功能,并启动项目


package com.cloud.provider;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication

@EnableEurekaClient 启用Eureka客户端功能

public class ProviderApplication {

public static void main(String[] args) {

SpringApplication.run(ProviderApplication.class, args);

}

}

6.启动项目,查看Eureka注册中心是否注册成功


在这里插入图片描述

4.consumer

=======================================================================

新建SpringBoot web项目用与服务消费者

1.添加maven依赖


org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2.2.5.RELEASE

最后

分布式技术专题+面试解析+相关的手写和学习的笔记pdf

还有更多Java笔记分享如下:

image

=======================================

新建SpringBoot web项目用与服务消费者

1.添加maven依赖


org.springframework.cloud

spring-cloud-starter-netflix-eureka-client

2.2.5.RELEASE

最后

分布式技术专题+面试解析+相关的手写和学习的笔记pdf

还有更多Java笔记分享如下:

[外链图片转存中…(img-2gXwLfWx-1714468149551)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 16
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值