92.【SpringCloud NetFilx】(三)

简介: 92.【SpringCloud NetFilx】

4.编写Service层的接口以及实现类

DeptService

package com.jsxs.service;
import com.jsxs.pojo.Dept;
import java.util.List;
public interface DeptService {
    public boolean addDept(Dept dept);
    public Dept queryById(Long id);
    public List<Dept> all();
}

DeptServiceImpl

package com.jsxs.service;
import com.jsxs.dao.DeptDao;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
@Service
public class DeptServiceImpl implements DeptService{
    @Resource
    private DeptDao deptDao;
    @Override
    public boolean addDept(Dept dept) {
        return deptDao.addDept(dept);
    }
    @Override
    public Dept queryById(Long id) {
        return deptDao.queryById(id);
    }
    @Override
    public List<Dept> all() {
        return deptDao.all();
    }
}

5.编写Cotroller层业务

package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import com.jsxs.service.DeptService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
//  提供Restful服务
@RestController
public class DeptController {
    @Resource
    private DeptService deptService;
    @PostMapping("/dept/add")
    public Boolean addDept(Dept dept){
        boolean flag = deptService.addDept(dept);
        return flag;
    }
    @GetMapping("/dept/queryById/{id}")
    public Dept queryById(@PathVariable("id") Long id){
        Dept dept = deptService.queryById(id);
        return dept;
    }
    @GetMapping("/dept/all")
    public List<Dept> all(){
        List<Dept> all = deptService.all();
        return all;
    }
}

6.创建子工程SpringCloud-consumer-dept-80 (普通Maven)

1.Maven依赖

边导入依赖 边看依赖的加载情况

<?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">
    <parent>
        <artifactId>SpringCloud</artifactId>
        <groupId>org.jsxs</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>SpringCloud-consumer-dept-80</artifactId>
    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.jsxs</groupId>
            <artifactId>SpringCloud-api</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <!--        热部署工具-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

2.application.yaml

80 端口不会在web网址上显示 会自动隐藏。通常客户端的端口就是 80

server:
  port: 80

3.注入RestTemplate到Spring容器中去 config/configBean.java

⭐新思路: 就是利用@Bean注入现成的类

package com.jsxs.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration  //  相当于application.xml文件
public class ConfigBean {
    //  把RestTemplate注入到Spring中去
    @Bean
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4.编写Controller层 controller/DeptCustomerController.java

  • 注意: 这里我们只是引入了实体类的数据,并没有引入service接口
  • 思考: 消费者界面不应该存在Dao层也不应该存在Service层,该如何使用服务呢
  • RestTemplate … 我们直接调用就行,但需要注入到Spring中
  • 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
  • getForObject(服务层URL,返回类型.class)
  • postForObject(服务层URL,参数,返回类型.class)
package com.jsxs.controller;
import com.jsxs.pojo.Dept;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.List;
@RestController
public class DeptConsumerController {
    //注意: 这里我们只是引入了实体类的数据,并没有引入service接口
    //思考: 消费者界面不应该存在Dao层也不应该存在Service层,该如何使用服务呢
    //RestTemplate  ... 我们直接调用就行,但需要注入到Spring中
    @Resource
    // (URI url, 实体 map,Class<T> responseType) 地址---- 实体 ----返回类型.class
    private RestTemplate restTemplate;   // 提供多种便捷访问远程   访问http服务的方法,简单的Rest
    //  设置服务层的前缀为常量
    private static final String REST_URL_PREFIX="http://localhost:8081";
    //  根据id进行数据的查找
  // 用户层的这个访问路径可以随意写,没有必要和服务层的路径完全一致
    @RequestMapping("/consumer/dept/get/{id}")
    public Dept get(@PathVariable("id") Long deptno){
    // 这里返回的是: 服务层的路径
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/queryById/"+deptno,Dept.class);
    }
    //  添加数据
    @RequestMapping("/consumer/dept/add")
    public boolean add(Dept dept){
        return restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Boolean.class);
    }
    //  查找全部数据
    @RequestMapping("/consumer/dept/all")
    public List<Dept> all(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/all",List.class);
    }
}

5.编写启动类

package com.jsxs;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DeptConsumer_80 {
    public static void main(String[] args) {
        SpringApplication.run(DeptConsumer_80.class,args);
    }
}

6.启动测试

先启动服务层的,然后再启动用户层的

添加的时候会报错: 吉士先生认为可能是请求状态的错误,Get 和 Post

(五)、Eureka服务注册中心

1.什么是Eureka

  • Netflix在涉及Eureka时,遵循的就是AP原则。
  • Eureka是Netflix的一个子模块,也是核心模块之一。Eureka是基于REST的服务,用于定位服务,以实现云端中间件层服务发现和故障转移,服务注册与发现对于微服务来说是非常重要的,有了服务注册与发现,只需要使用服务的标识符,就可以访问到服务,而不需要修改服务调用的配置文件了,功能类似于Dubbo的注册中心,比如Zookeeper。

2.原理理解

(1).Eureka架构和Dubbo架构的对比

Eureka基本的架构

  • Springcloud 封装了Netflix公司开发的Eureka模块来实现服务注册与发现 (对比Zookeeper).
  • Eureka采用了C-S的架构设计,EurekaServer作为服务注册功能的服务器,他是服务注册中心.而系统中的其他微服务,使用Eureka的客户端连接到EurekaServer并维持心跳(每隔三十秒请求链接一次)连接。这样系统的维护人员就可以通过EurekaServer来监控系统中各个微服务是否正常运行,Springcloud 的一些其他模块 (比如Zuul) 就可以通过EurekaServer来发现系统中的其他微服务,并执行相关的逻辑.
  • 和Dubbo架构对比.
(2).Eureka的两个组件
  • Eureka 包含两个组件:Eureka Server 和 Eureka Client.
  • Eureka Server 提供服务注册,各个节点启动后,回在EurekaServer中进行注册,这样Eureka Server中的服务注册表中将会储存所有课用服务节点的信息,服务节点的信息可以在界面中直观的看到。
  • Eureka Client 是一个Java客户端,用于简化EurekaServer的交互,客户端同时也具备一个内置的,使用轮询负载算法的负载均衡器。在应用启动后,将会向EurekaServer发送心跳 (默认周期为30秒) 。如果Eureka Server在多个心跳周期内没有接收到某个节点的心跳,Eureka Server将会从服务注册表中把这个服务节点移除掉 (默认周期为90s)
(3).Eureka的三大角色
  • Eureka Server:提供服务的注册与发现
  • Service Provider:服务生产方,将自身服务注册到Eureka中,从而使服务消费方能狗找到
  • Service Consumer:服务消费方,从Eureka中获取注册服务列表,从而找到消费服务
相关文章
|
5月前
|
消息中间件 NoSQL Java
Spring Cloud项目实战Spring Cloud视频教程 含源码
Spring Cloud项目实战Spring Cloud视频教程 含源码
96 1
|
16天前
|
负载均衡 Java API
【Spring Cloud生态】Spring Cloud Gateway基本配置
【Spring Cloud生态】Spring Cloud Gateway基本配置
29 0
|
2月前
|
Java Spring
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
【Azure Spring Cloud】Spring Cloud Azure 4.0 调用Key Vault遇见认证错误 AADSTS90002: Tenant not found.
|
2月前
|
Java Spring 容器
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
【Azure Spring Cloud】在Azure Spring Apps上看见 App Memory Usage 和 jvm.menory.use 的指标的疑问及OOM
|
2月前
|
存储 Java Spring
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
【Azure Spring Cloud】Azure Spring Cloud服务,如何获取应用程序日志文件呢?
|
2月前
|
SQL Java 数据库连接
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
【Azure Spring Cloud】Azure Spring Cloud connect to SQL using MSI
|
2月前
|
Java 开发工具 Spring
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
【Azure Spring Cloud】使用azure-spring-boot-starter-storage来上传文件报错: java.net.UnknownHostException: xxxxxxxx.blob.core.windows.net: Name or service not known
|
2月前
|
NoSQL Java Redis
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
【Azure Spring Cloud】Java Spring Cloud 应用部署到Azure上后,发现大量的 java.lang.NullPointerException: null at io.lettuce.core.protocol.CommandHandler.writeSingleCommand(CommandHandler.java:426) at ... 异常
|
2月前
|
Java Spring
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
【Azure 应用服务】记一次Azure Spring Cloud 的部署错误 (az spring-cloud app deploy -g dev -s testdemo -n demo -p ./hellospring-0.0.1-SNAPSHOT.jar --->>> Failed to wait for deployment instances to be ready)
|
2月前
|
Java Maven Python
【Azure Spring Cloud】部署Azure spring cloud 失败
【Azure Spring Cloud】部署Azure spring cloud 失败