首页 > 编程语言 > 详细

SpringCloud Netflix相关代码

时间:2021-08-17 10:18:05      阅读:16      评论:0      收藏:0      [点我收藏+]

一、bean模块:

? DeptService(接口)

package com.gk.service;

import com.gk.bean.Dept;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@FeignClient(value = "MYSC-DEPT")
@Component
public interface DeptService {

    @RequestMapping("byId/{id}")
    public Dept salById(@PathVariable(value = "id") Long id);
    @RequestMapping("salList")
    public List<Dept> salByList();

    @RequestMapping("salByInfo")
    public Object getDiscovery();
}

二、eureka7001模块

1.springBoot启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaStart {
    public static void main( String[] args ){
        SpringApplication.run(EurekaStart.class,args);
    }
}

2、yaml配置文件

server:
  port: 7001

# eureka相关
eureka:
  instance:
    hostname: activate.navicat.com
  client:
    register-with-eureka: false #是否向eureka注册中心注册自己
    fetch-registry: false   #fetch-registry为false,表示注册中心是自己
    service-url:
      defaultZone: http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/

3、pom

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka-server -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka-server</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

三、eureka7002模块

启动springBoot类、pom在eureka7001模块基础上 都不变

yaml配置文件

server:
  port: 7002

# eureka相关
eureka:
  instance:
    hostname: activate.navicat2.com
  client:
    register-with-eureka: false #是否向eureka注册中心注册自己
    fetch-registry: false   #fetch-registry为false,表示注册中心是自己
    service-url:
      defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat3.com:7003/eureka/

四、服务提供者(mySc-dept)

1、pom依赖

<dependencies>
    <dependency>
        <groupId>com.gk</groupId>
        <artifactId>mySc-bean</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--spring热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>

2、yaml配置

server:
  port: 8000

spring:
  application:
    name: mySc-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sales?characterEncouding&useUnicode=true&user=root&password=root
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: root

#eureka相关
eureka:
  client:
    service-url:
     defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
  instance:
    instance-id: mySc-provider-8000
info: # 点击 Status 下的内容,提示的内容信息
  app.name: 张三的cloud
  company.name: 这是注定,这是命运

3、config包下DeptConfig配置

@Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {

}

4、controller层DeptController

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gk.bean.Dept;
import com.gk.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private DiscoveryClient client;//可以用来获取服务信息

    @RequestMapping("byId/{id}")
    public Dept salById(@PathVariable Long id){
        QueryWrapper<Dept> dw = new QueryWrapper<>();
        dw.eq("deptno",id);
        Dept dept = deptMapper.selectOne(dw);
        return dept;
    }
    @RequestMapping("salList")
    public List<Dept> salByList(){
        List<Dept> depts = deptMapper.selectList(null);
        return depts;
    }

    @RequestMapping("salByInfo")
    public Object getDiscovery(){
        //获取微服务列表清单
        List<String> list = client.getServices();
        System.out.println(list);
        System.out.println("================");
        //通过微服务的Application(注册在eureka中的实例) 获取某个具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("MYSC-DEPT");//获取mySc-dept模块的服务信息
        for (ServiceInstance s : instances) {
            System.out.println(
                    s.getHost() + "\n"+
                    s.getInstanceId() + "\n"+
                    s.getScheme() + "\n"+
                    s.getServiceId() + "\n"+
                    s.getMetadata() + "\n"+
                    s.getPort() + "\n"+
                    s.getUri()+"\n"+
                    "==============="
            );
        }
        return instances;
    }
}

5、mapper接口DeptMapper

@Repository
public interface DeptMapper extends BaseMapper<Dept> {
}

6、springBoot启动类

@SpringBootApplication
@EnableEurekaClient
public class MyScDeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyScDeptApplication.class,args);
    }
}

五、服务提供者2和服务提供者3(mySc-dept-provider-8002)

在第一个提供者之上没太对变化

yaml配置

server:
  port: 8002

spring:
  application:
    name: mySc-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: root

#eureka相关
eureka:
  client:
    service-url:
     defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
  instance:
    instance-id: mySc-provider-8002
    ip-address: true  # 显示服务器的ip地址
info: # 点击 Status 下的内容,提示的内容信息
  app.name: 张三的cloud2
  company.name: 这是注定,这是命运2
server:
  port: 8003

spring:
  application:
    name: mySc-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: root

#eureka相关
eureka:
  client:
    service-url:
     defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
  instance:
    instance-id: mySc-dept3
info: # 点击 Status 下的内容,提示的内容信息
  app.name: 张三的cloud3
  company.name: 这是注定,这是命运3

六、服务消费者()

1、pom依赖

<dependencies>
    <!--ribbon负载均衡-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-ribbon</artifactId>
        <version>1.4.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>com.gk</groupId>
        <artifactId>mySc-bean</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--spring热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
</dependencies>

2、yaml配置

server:
  port: 8001

eureka:
  client:
    register-with-eureka: false  #不让在eureka注册中心注册自己
    service-url:
      defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/

3、config配置(DeptConfig)

@Configuration
public class DeptConfig {
    //配置负载均衡实现RestTemplate
    @Bean
    // IRule: 负载均衡策略(接口)以下为他的实现类
    //RoundRobinRule:轮循(默认)
    //RandomRule: 随机访问
    //AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
    //RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
    @LoadBalanced //使用ribbon实现负载均衡
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4、controller层

@RestController
public class DeptController {
    //请求前缀地址
//    private final String PREFIX_URL="http://localhost:8000/";
    private final String PREFIX_URL="http://MYSC-DEPT/";

    @Autowired
    private RestTemplate restTemplate;
    @RequestMapping("salById/{id}")
    public Dept salById(@PathVariable Long id){

        return restTemplate.getForObject(PREFIX_URL+"byId/"+id,Dept.class);
    }
    @RequestMapping("salByList")
    public List<Dept> salByList(){
        return restTemplate.getForObject(PREFIX_URL + "salList",List.class);
    }
}

5、springBoot启动类

@SpringBootApplication
@EnableEurekaClient
public class MyScConsumer {
    public static void main(String[] args) {
        SpringApplication.run(MyScConsumer.class,args);
    }
}

七、feign(负载均衡)

1、pom

<dependencies>
    <dependency>
        <groupId>com.gk</groupId>
        <artifactId>mySc-bean</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--spring热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-feign</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>
</dependencies>

2、yaml配置

server:
  port: 8005

spring:
  application:
    name: mySc-dept-feign
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sales2?characterEncouding&useUnicode=true&user=root&password=root
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: root

#eureka相关
eureka:
  client:
    service-url:
     defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
  instance:
    instance-id: mySc-feign-8005
info: # 点击 Status 下的内容,提示的内容信息
  app.name: 张三的cloud2
  company.name: 这是注定,这是命运2

3、config层

@Configuration
@ComponentScan("com.gk")
public class DeptConfig {
    //配置负载均衡实现RestTemplate
    @Bean
    // IRule: 负载均衡策略(接口)一下为他的实现类
    //RoundRobinRule:轮循(默认)
    //RandomRule: 随机访问
    //AvailabilityFilteringRule: 过滤掉崩溃、访问故障、跳闸 的服务,对剩下的服务进行轮循
    //RetryRule: 先按照轮循进行访问,如果访问失败,会在规定的时间内 进行重试
    @LoadBalanced //使用ribbon实现
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

4、controller层

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;
    @RequestMapping("salById/{id}")
    public Dept salById(@PathVariable(value="id") Long id){

        return deptService.salById(id);
    }
    @RequestMapping("salByList")
    public List<Dept> salByList(){
        return deptService.salByList();
    }
}

5、springBoot启动类

@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients(basePackages={"com.gk"})
public class FeignScConsumer {
    public static void main(String[] args) {
        SpringApplication.run(FeignScConsumer.class,args);
    }
}

八、Hystrix(服务熔断)

1、pom

<dependencies>
    <dependency>
        <groupId>com.gk</groupId>
        <artifactId>mySc-bean</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
    </dependency>
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--spring热部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-hystrix</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
</dependencies>

2、config层

@Configuration
@MapperScan("com.gk.mapper")
public class DeptConfig {

}

3、controller层

@RestController
public class DeptController {
    @Autowired
    private DeptMapper deptMapper;

    @RequestMapping("byId/{id}")
    @HystrixCommand(fallbackMethod="salById2")//方法异常后调用salById2方法
    public Dept salById(@PathVariable Long id){
        QueryWrapper<Dept> dw = new QueryWrapper<>();
        dw.eq("deptno",id);
        Dept dept = deptMapper.selectOne(dw);
        if (dept==null){
            throw new RuntimeException("查询结果为空...");
        }
        return dept;
    }

    public Dept salById2(@PathVariable Long id){
        return new Dept(id,"没有当前这个id=>"+id,"没有这个数据库");
    }
}

4、mapper

@Repository
public interface DeptMapper extends BaseMapper<Dept> {

}

5、springBoot启动类

@SpringBootApplication
@EnableEurekaClient
@EnableCircuitBreaker  //添加熔断支持
public class MyHystrixScDeptApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyHystrixScDeptApplication.class,args);
    }
}

6、配置文件yaml

server:
  port: 8004

spring:
  application:
    name: mySc-dept
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/sales3?characterEncouding&useUnicode=true&user=root&password=root
    driver-class-name: com.mysql.jdbc.Driver
    data-username: root
    data-password: root

#eureka相关
eureka:
  client:
    service-url:
     defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
  instance:
    instance-id: mySc-provider-hystrix-8004
info: # 点击 Status 下的内容,提示的内容信息
  app.name: 张三的cloud3
  company.name: 这是注定,这是命运3

九、zuul(服务路由网关)

1、pom

<dependencies>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zuul</artifactId>
        <version>1.4.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
        <version>1.3.4.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2、yaml配置

server:
  port: 9527

# spring 配置服务名字
spring:
  application:
    name: mySc-zuul-9527

#eureka
eureka:
  instance:
    instance-id: mySc-zuul-9527
    prefer-ip-address: true # 显示id地址
  client:
    service-url:
      defaultZone: http://activate.navicat.com:7001/eureka/,http://activate.navicat2.com:7002/eureka/,http://activate.navicat3.com:7003/eureka/
info:
  app.name: zhangsan.com
  app.address: www.疯子.com
zuul:
  routes:
    dept.serviceId: mysc-dept
    dept.path: /dept/**
  ignored-services: "*" # 不允许任何一个路径进行访问,只允许使用 dept(http://www.gk.com:9527/dept/) 进行访问
  prefix: /gk  #设置公共的访问前缀 :http://www.gk.com:9527/gk/dept/

3、springBoot启动类

@SpringBootApplication
@EnableZuulProxy
public class MyZuulApplication {
    public static void main( String[] args ){
        SpringApplication.run(MyZuulApplication.class,args);
    }
}

SpringCloud Netflix相关代码

原文:https://www.cnblogs.com/gekun154/p/15150575.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!