}
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 + “]”;
}
}
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插件,点击此处查看原因
=========================================================================
新建一个SpringBoot web项目用于Eureka注册中心
org.springframework.cloud
spring-cloud-starter-netflix-eureka-server
2.2.1.RELEASE
com.cloud
api
0.0.1-SNAPSHOT
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/查看服务启动情况
=======================================================================
新建SpringBoot web项目用于服务提供者
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.2.5.RELEASE
com.cloud
api
0.0.1-SNAPSHOT
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/
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);
}
}
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);
}
}
=======================================================================
新建SpringBoot web项目用与服务消费者
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.2.5.RELEASE
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
=======================================
新建SpringBoot web项目用与服务消费者
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
2.2.5.RELEASE
最后
分布式技术专题+面试解析+相关的手写和学习的笔记pdf
还有更多Java笔记分享如下:
[外链图片转存中…(img-2gXwLfWx-1714468149551)]