zoukankan      html  css  js  c++  java
  • 01. eureka

    01. eureka

    单eureka

    #Server

    server:
      port: 7001
    #eureka,注册中心
    eureka:
      instance:
        hostname: localhost #eureka服务端的实例名
      client:
        register-with-eureka: false #表示不向注册中心注册自己, 默认true
        fetch-registry: false #表示自己不向注册中心拿服务, 默认true
        service-url:
       	 #这是一个map,设置与eureka server交互的地址查询服务和注册服务都需要依赖这个地址
       	  #通过http://localhost:7001来启动可视化界面管理
          defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ 
         #固定格式由 http://+hostname+server-port+/eureka/ 组成
    
    
    

    这里末尾的/表示访问的是一个目录(默认访问default.html或是index.html),也可以不加,如果不加表示首先当作servlet处理,如果找不到就当作目录处理

    主启动类

    @EnableEurekaServer//表明该模块是eureka注册中心启动模块,接收其他微服务注册进来
    @SpringBootApplication
    public class MicrosoftEureka7001Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(MicrosoftEureka7001Application.class, args);
    	}
    }
    

    #provider

    application.yml

    #datasource
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/clouddb01?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
        username: root
        password: 12345
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          initial-size: 5 #初始连接数
          max-active: 10 #最大活动连接
          max-wait: 60000 #从池中取连接(没有闲置连接)的最大等待时间,-1表示无限等待
          min-idle: 5 #最小闲置数,小于min-idle连接池会主动创建新的连接
          time-between-eviction-runs-millis: 60000 #清理线程启动的间隔时间,当线程池中没有可用的连接启动清理线程
          min-evictable-idle-time-millis: 300000 #清理线程保持闲置最小时间
          validation-query: SELECT 1  #用于校验连接
          test-on-borrow: false #请求连接时是否校验,比较消耗性能,一般设置false
          test-on-return: false #归还连接时是否校验,比较消耗性能,一般设置false
          test-while-idle: true #清理线程通过validation-query来校验连接是否正常,如果不正常将从连接池中移除
          pool-prepared-statements: true #存储相同逻辑的sql到连接池的缓存中
          filters: stat,wall #监控统计web的statement(sql),以及防sql注入的wall
          web-stat-filter:
            enabled: true #开启监控uri,默认false
            url-pattern: /* #添加过滤规则
            exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid" #忽略过滤
          stat-view-servlet:
            enabled: true #开启视图管理界面,默认false
            url-pattern: /druid/* #视图管理界面uri
            login-username: druid #账号
            login-password: 12345 #密码
      #eureka
      application:
        name: microsoft-provider-8081 #服务名
    eureka:
      instance:
        instance-id: microsoft-prodiver8081 #修改eureka中status的名字
        prefer-ip-address: true #访问status路径可以显示ip
      client:
        service-url: #将客户端注册进eureka服务列表中
          defaultZone: http://localhost:7001/eureka/ #注册中心url
          #由于register-with-eureka和fetch-registry 默认是true可以不用显示的指明
          #表示当前服务不向注册中心获取服务
        fetch-registry: false
    #server
    server:
      port: 8081
    #mybatis
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    #actuator指定信息, 直接配置即可
    info:
      app:
        name: 我的第一个springboot项目
        version: 0.0.1
      data:
        author: chz
    
    

    controller

    //@Controller
    @RestController//前后分离返回json字符串
    @RequestMapping("/provider")
    public class DeptProviderController {
        @Autowired
        private DeptService deptService;
        //表示对eureka开启读取服务
        @Autowired
        private DiscoveryClient discoveryClient;
    
        //@RequestBody用于接收前端传过来的json传,且提交的方式必须是post
        @PostMapping(path = {"/add"})
        public boolean add(@RequestBody Dept dept) {
            return deptService.add(dept);
        }
    
        @GetMapping(path = "get/{id}")
        public Dept get(@PathVariable("id") Integer id) {
            return deptService.get(id);
        }
    
        @GetMapping(path = "/get")
        public List<Dept> list() {
            return deptService.list();
        }
    
        @GetMapping(path = "/discovery")
        public Object discovery() {
            List<String> services = discoveryClient.getServices();
            List<ServiceInstance> instances =
                    //对应eureka管理界面中Instances currently registered with Eureka的Application的信息
                    discoveryClient.getInstances("MICROSOFT-PROVIDER-8081");
            for (ServiceInstance instance : instances) {
                System.out.println("host===>" + instance.getHost());
                System.out.println("port===>" + instance.getPort());
                System.out.println("uri===>" + instance.getUri());
            }
            return discoveryClient;
        }
    }
    

    主启动类

    @EnableDiscoveryClient//服务发现,对于注册到服务中心的微服务,可以通过服务发现来获取服务信息
    @EnableEurekaClient//表明,本服务启动后会自动注入eureka服务中
    @SpringBootApplication
    public class MicrosoftProvider8081Application {
    	public static void main(String[] args) {
    		SpringApplication.run(MicrosoftProvider8081Application.class, args);
    	}
    }
    
    • instance-id

    • prefer-ip-address

    • 自定义status的info

    #consumer

    1. 只需要引入api的dependency

    2. yml

      server:
        port: 8082 #可以设置为80端口, 这样就可以不用输出端口
      spring:
        application:
          name: microsoft-consumer-8082
        thymeleaf:
          cache: false
      
    3. restTemplate

      /**
       * 配置ResTemplate,用于分布式通信
       */
      @Configuration
      public class ConfigBean {
          //执行http请求
          //springboot并没有帮我们把RestTemplate注入到ioc中所以要手动注入
          @Bean
          public RestTemplate getRestTemplate(){
              return new RestTemplate();
          }
      }
      
      1. controller
      @RestController
      @RequestMapping("/consumer")
      @SuppressWarnings("all")
      public class DeptConsumerController {
          private static final String REST_URL_PREFIX = "http://localhost:8081/provider";
      
          /**
           * (url,requestMap,ResponseBean.class)三个参数分别代表
           * REST请求地址,请求参数,http响应转换成的对象类型(provider方法返回的类型)
           */
          @Autowired
          private RestTemplate restTemplate;
      
          @GetMapping("/add")
          public Boolean add(Dept dept) {
              //postForObject对应post insert
              return restTemplate.
                      //中间的request可以是null
                      postForObject(REST_URL_PREFIX + "/add", dept, Boolean.class);
          }
      
          @GetMapping("/get")
          public List<Dept> list() {
              return restTemplate.
                     getForObject(REST_URL_PREFIX + "/get",List.class);
          }
      
          @GetMapping("/get/{id}")
          public Dept get(@PathVariable("id") Integer id) {
              //getForObject对应get
              return restTemplate.
                      getForObject(REST_URL_PREFIX + "/get/" + id, Dept.class);
          }
          //发现对应提供者的相应服务
          @GetMapping("/discovery")
          public Object dicovery(){
              return  restTemplate.getForObject(REST_URL_PREFIX+"/discovery",Object.class);
          }
      }
      

    eureka集群

    1. 添加多个eurekaserver

    1. 修改windows host ,添加
    127.0.0.1 eureka7001.com
    127.0.0.1 eureka7002.com
    127.0.0.1 eureka7003.com
    
    1. 修改7001 yml
    server:
      port: 7001
    #eureka,注册中心
    eureka:
      instance:
        hostname: eureka7001.com #eureka服务端的实例名
      client:
        register-with-eureka: false #表示不向注册中心注册自己, 默认true
        fetch-registry: false #表示自己不向注册中心拿服务, 默认true
        service-url:
          #      eureka集群
          defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
    
    
    1. 修改7002 yml
    server:
      port: 7002
    #eureka,注册中心
    eureka:
      instance:
        hostname: eureka7002.com #eureka服务端的实例名
      client:
        register-with-eureka: false #表示不向注册中心注册自己, 默认true
        fetch-registry: false #表示自己不向注册中心拿服务, 默认true
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7003.com:7003/eureka/
    
    
    1. 修改7003 yml
    server:
      port: 7002
    #eureka,注册中心
    eureka:
      instance:
        hostname: eureka7002.com #eureka服务端的实例名
      client:
        register-with-eureka: false #表示不向注册中心注册自己, 默认true
        fetch-registry: false #表示自己不向注册中心拿服务, 默认true
        service-url:
          defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/
    

    可以通过eureka7001.com:7001, eureka7002.com:7002, eureka7003.com:7003来访问

  • 相关阅读:
    Python小白学习之路(五)—【类和对象】【列表】【列表相关功能】
    Python小白学习之路(四)——第一次练习题
    Python小白学习之路(三)—【数字功能】【字符串功能】
    Python小白学习之路(二)—【Pycharm安装与配置】【创建项目】【运算符】【数据类型】
    Python初体验(一)—【配置环境变量】【变量】【input】【条件语句】【循环语句】
    HTML下直接调用Less文件
    继承的几种方式
    sublime从官网纯净版到插件完整版
    bower工具的简单使用
    sublime修改代码字体颜色
  • 原文地址:https://www.cnblogs.com/kikochz/p/12868754.html
Copyright © 2011-2022 走看看